I have this following code that creates a node in the page.
$data = array(
'type' => 'post',
'field_post' => [
'value' => 'test',
'format' => 'basic_html',
],
'field_visibility' => 1,
'user_id' => $uid
);
$node = Drupal::entityManager()
->getStorage('post')
->create($data);
$node->save();
I have another field field_pattern
that is an entity reference for another content type. How can I properly set it inside my code?
Since with creating the content from the site when dealing with Entity reference field selects autocomplete to Title, then I tried to test this but fails. Meaning no value is set to entity reference.
$title = $entity->label();
$data = array(
'type' => 'post',
'field_post' => [
'value' => 'test',
'format' => 'basic_html',
],
'field_pattern' => $title, // this is the entity reference field
'field_visibility' => 1,
'user_id' => $uid
);
$node = Drupal::entityManager()
->getStorage('post')
->create($data);
$node->save();