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
    Jul 20, 2016 at 12:41
  • 2
    $node->id() after $node->save() will get you that
    – Clive
    Jul 20, 2016 at 12:41

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.