Created
April 12, 2017 09:26
-
-
Save gargsuchi/863a556c732b6fab89c99f0bdaa4d2f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains lotus.module. | |
*/ | |
use Drupal\node\Entity\Node; | |
/** | |
* Helper function to create a new node. | |
*/ | |
function create_new_node(){ | |
$node = Node::create(['type' => 'YOUR_NODE_TYPE']); | |
$node->set('title', 'YOUR_TITLE'); | |
//Body can now be an array with a value and a format. | |
//If body field exists. | |
$body = [ | |
'value' => 'YOUR_BODY_TEXT', | |
'format' => 'basic_html', | |
]; | |
$node->set('body', $body); | |
$node->set('uid', YOUR_UID); | |
$node->status = 1; | |
$node->enforceIsNew(); | |
$node->save(); | |
drupal_set_message("Node with nid " . $node->id() . " saved!\n"); | |
} |
I got this error InvalidArgumentException: Field body is unknown.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Makes sense. Did not realize that this gist is linked.