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',


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


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

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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