Create nodes programmatically

Fast Track to Drupal 8 Coding – Back to intro
Previous lesson - Create a custom page with arguments
Next lesson – Delete nodes programmatically

Lesson Goal

Create a custom function to create nodes programmatically.

Implementation Method

Drupal Version Method
Drupal 7 custom function
Drupal 8 custom function

Drupal 7 method

The following code snippet can be used wherever there is a requirement to add a new node, being sure to replace <title> and <node> with their actual values.

global $user;
  $node = new stdClass();
  $node->title = "<title>";
  $node->type = "<node>";
  node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
  $node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
  $node->uid = $user->uid;
  $node->status = 1; //(1 or 0): published or not
  $node->promote = 0; //(1 or 0): promoted to front page
  $node = node_submit($node); // Prepare node for saving
  node_save($node);
  drupal_set_message( "Node with nid " . $node->nid . " saved!\n");

Drupal 8 method

The following code snippet can be used wherever there is a requirement to add a new node, being sure to replace <node_type>, <title>, <body_text>, and <uid> with their actual values.

use Drupal\node\Entity\Node;
$node = Node::create(['type' => '<node_type>']);
$node->set('title', '<title>');

//Body can now be an array with a value and a format.
//If body field exists.
$body = [
'value' => '<body_text>',
'format' => 'basic_html',
];
$node->set('body', $body);
$node->set('uid', <uid>);
$node->status = 1;
$node->enforceIsNew();
$node->save();
drupal_set_message( "Node with nid " . $node->id() . " saved!\n");

Contact supportStill need assistance? Contact Acquia Support

Acquia: Think Ahead

53 State Street, 10th Floor
Boston, MA 02109
United States
Phone: 888-922-7842

Map: Google Maps
View other locations