I have successfully created node programmatically using Drupal 8 Node::create and be able to select list (integer) field correctly as following, but list (text) field is not getting selected with the same process. Any help appreciated.
(Manage Fields)
LABEL MACHINE NAME FIELD TYPE
===========================
Expires (in Days) field_expires_in_days_ List (integer) -> NOT WORKING
Item Type field_item_type List (text) - Working
Allowed value list for 'field_expires_in_days_
=========
30|30
60|60
90|90
Allowed value list for 'field_item_type'
========================
tipsheet|Tip Sheet
fyi|FYI
video|Video
(Controller)
========
$newListItem = array(); //prepare new item
$newListItem["title"] = "Title 111";
$newListItem["summary"] = "Some description";
$newListItem["itemType"] = "tipsheet";
$newListItem["expiresInDays"] = "30";
$node = Node::create([
'type' => 'custom_content_type',
'status' => 1,
'title' => $listItem['title'],
'body' => $listItem['summary'],
'field_item_type ' => $listItem['itemType'],
'field_expires_in_days_' => $listItem['expiresInDays'],
]);
$node->save();
Comments
fixed
I left space in the field_item_type while creating node. Got immediate reply on stackoverflow in hours.
list text field update issue
I have list text field and can't seem to get the value set using the code below... $node->set works everything else, text, integer,decimal, entity ref fields
$node->set('field_list_text', 'Approved');
While saving the list values
While saving the list values programmatically. The value should be in below format :
$node->set('field_item_type',array(array('value'=>$listItem['itemType'])));
$node->set('field_expires_in_days_',array(array('value'=>$listItem['expiresInDays'])));