0

I am trying to reference other nodes on the user entity on field_clinic but everything else is saving except that multiple value field. What could i be doing wrong. The field_clinic accepts multiple values. Below is my code:

$clinics = [

        array('value' => 132),
        array('value' => 133),
        array('value' => 134),
    ];



    // if the user exsists update basestation
    $UserEntity = User::create([
        'name' => 'Final',
        'roles' => array('subscriber'),
        'field_age' => '2',
        'field_city' => 12,
        'field_clinic' => $clinics,
        'field_base_statition' =>  $bs_nid


    ]);

    $UserEntity->save();

The clinics array contains the node ids that I am trying to reference. Please help

2

The way you're setting the values may be failing to work if you've got the wrong keys in your array - setting any field from code requires you to know how that specific field type stores its data in the entity.

If you're not sure about that key, using the devel module you can simply save one of those users filling the field_clinic field with appropriate values then switch to the devel tab for that user and inspect what keys are actually being used to store the values in that field (probably one named target_id).

According to what you find there, fixing your code may be just matter of changing this:

$clinics = [
    array('value' => 132),
    array('value' => 133),
    array('value' => 134),
];

to this:

$clinics = [
    ['target_id' => 132],
    ['target_id' => 133],
    ['target_id' => 134],
];

(disregard the difference between declaring an array using the [] or the array(), I used the former in my edited code just for coherency)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.