1

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();
2
  • Are you creating a new node or new bundle?
    – Ajay Reddy
    May 4, 2018 at 5:17
  • Generic answer: Create whatever you want to create in the UI. Load it and do var_dump($entity->toArray()), those are the raw values, and whatever you get there you can also set on those entities.
    – Berdir
    May 4, 2018 at 6:35

1 Answer 1

5

If you are creating a node with reference field, see below code:

 use \Drupal\node\Entity\Node;
 $node = Node::create([
   'type' => 'post',
   'field_post' => [
     'value' => 'test',
     'format' => 'basic_html',
   ],
   'field_pattern' => [
      'target_id' => $ID_REFERENCE_ENTITY, // Here just provide referenced entity id.
   ],
   'field_visibility' => 1, 
   'user_id' => $uid
 ]);
 $node->save();

If you are are not creating node then just replace your field_pattern like below:

'field_pattern' => [
   'target_id' => $ID_REFERENCE_ENTITY, // Here just provide referenced entity id.
],
4
  • Thank you for your interest and response on this. Im creating a Post content node. Its not a regular node/content type, but a Post Type from OpenSocial profile. I have to follow my code to create one else I would get error since its not a Node. What is the example value for the $ID_REFERENCE_ENTITY? May 4, 2018 at 5:47
  • Basically, the entity reference is just another content. In web ui, we select the title to provide value for the entity reference field. But im not sure with $ID_REFERENCE_ENTITY, where to get this value? May 4, 2018 at 5:49
  • $ID_REFERENCE_ENTITY will the ID of your referenced entity. Suppose you are referring a taxonomy in that field, that $ID_REFERENCE_ENTITY would be TID of that taxonomy. Hope this helps..
    – Ajay Reddy
    May 4, 2018 at 5:50
  • Oh that simple! It works like a charm! Perfect! Thank you so much! May 4, 2018 at 5:52

Your Answer

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

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