@shyann
In Drupal 8, you can create a node programmatically by using the following steps:
1 2 |
use Drupal
odeEntityNode;
|
1 2 3 4 5 |
$node = Node::create([ 'type' => 'article', 'title' => 'My title', 'body' => 'My body', ]); |
1
|
$node->save(); |
That's it! You have now created a new node programmatically in Drupal 8.
@shyann
To create a node programmatically in Drupal 8, you can use the following steps:
1 2 3 4 |
use Drupal odeEntityNode; $node = Node::create(); |
1
|
$node->setType('article'); |
Replace 'article' with the desired node type machine name.
1 2 3 4 5 |
$node->setTitle('My Node Title'); $node->set('body', [ 'value' => 'My Node Body', 'format' => 'basic_html', // Define the input format of the body value ]); |
1
|
$node->set('field_tags', [1, 2, 3]); // Replace 'field_tags' with your field name and [1, 2, 3] with the term IDs you want to assign |
Replace 'field_tags' with your field name and [1, 2, 3] with the desired term IDs.
1
|
$node->save(); |
That's it! Now you have created a node programmatically in Drupal 8.