1

I can create node programmatically with following snippet

 use \Drupal\node\Entity\Node;

// Create node object with attached file.
$node = Node::create([
  'type'        => 'article',
  'title'       => 'Druplicon test',

])->save();

but it only returned "1" on successful,I want to return the created node object . how can I achieve that?

1 Answer 1

8

It's Entity::save() that returns the integer, so:

$node = Node::create([
  'type'        => 'article',
  'title'       => 'Druplicon test',
]);

$node->save();

return $node;
2
  • but there is no nid in $node, I want to get nid of created node.
    – Yuseferi
    Commented Jul 20, 2016 at 12:41
  • 2
    $node->id() after $node->save() will get you that
    – Clive
    Commented Jul 20, 2016 at 12:41

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.