-1

I am creating nodes programmatically on a specific content type. The content type has a "select field" that I am trying to set value with and cannot do it. Below is my code:

 $node = \Drupal::entityTypeManager()->getStorage('node')->create(array(
      'type'  => 'level3',
      'title' => $file_name,
      field_level2_title => [
        'value' => 'Assessment'
      ],
      field_level3_title => $file_name,
      field_level3_readmore => [
        'uri' => $file_uri,
        'title' => $file_name
      ],
    ));

The select field name is "field_level2_title and I am unable to assign this. Any help?

4
  • 1
    A select element is a widget, not a field type. What type of field is it? (Entity Reference, Text, etc)
    – sonfd
    Jul 6, 2021 at 22:34
  • It's an entity reference field.
    – Rick
    Jul 6, 2021 at 23:26
  • 1
    Then you need to assign it the ref ID.
    – No Sssweat
    Jul 6, 2021 at 23:38
  • Ahh instead of value I should assign refid?
    – Rick
    Jul 6, 2021 at 23:47

1 Answer 1

-1

Thanks for the comments above. the solution worked for me is as below:

field_level2_title => [ 'target_id' => $ref_id_field ]

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.