n How to create nodes programmatically in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

In Drupal 8, the node is an entity. The Drupal core class Node will provide us to create or update the nodes in Drupal 8. The below is an example of creating a node Drupal 8.

Example : 


use Drupal\node\Entity\Node;

* add Node
$node = Node::create(['type' => $contentType]);
    $node->langcode = "fr";
    $node->uid = 1;
    $node->promote = 0;
    $node->sticky = 0;
    $node->title= $title;
    $node->body = $body;
    $node->field_1 = $field_1;

$nid = $node->id(); // Get Nid from the node object.


Add translate Node


     * add translate Node
    $transNode = Node::load($nid);
    $transNode = $transNode->addTranslation("en");
    $transNode->title = $title_EN;
    $transNode->body= $body_EN;
    $transNode->field_1 = $field_1_EN;


Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.