I'm attempting to programmatically generate nodes of a custom content type from a form plugin. For most fields I don't have any trouble at all. For example for the title field I can set the value like so.

$node->title->value = t('My title');

However there is also a list(text) field that allows for multiple values. And I'm not sure how to go about saving values of field with multiple values. I'm assuming if there was only one value allowed I could do it just like I set the title in the previous example, but how do I deal with it when the field is an array? As a test I performed a die statement of the field on a node that already had values in that field to take a look at what it looks like and this was generated:

Array ( [0] => Array ( [value] => test_3 ) [1] => Array ( [value] => test_4 ) ) 1

But I'm not sure what to do with that. How would I set the field value of a list field with multiple values allowed?

1 Answer 1


You can replace the complete content of a field by assigning an array:

$node->field_multivalue = [
  ['value' => '1'],
  ['value' => '2'],
  ['value' => '3'],

If you want add another value to the field you can use the field like a php array:

$node->field_multivalue[] = ['value' => '4'];

You can omit the main property of a field:

$node->field_multivalue[] = '5';

Now you have 5 values in the field.

  • 8
    Another option: $node->field_multivalue->appendItem(5);
    – Berdir
    Commented Dec 23, 2016 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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