Score:-1

Set "select" field on a node programatically

in flag

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?

sonfd avatar
in flag
A select element is a widget, not a field type. What type of field is it? (Entity Reference, Text, etc)
Rick avatar
us flag
It's an entity reference field.
No Sssweat avatar
ua flag
Then you need to assign it the ref ID.
Rick avatar
us flag
Ahh instead of value I should assign refid?
Score:-1
in flag

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

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.