Drupal create node with body programmatically

2019-08-25 18:10发布

问题:

I am trying to create nodes in Drupal 7 using a php script I then execute using Drush.

While I am able to create a basic node with a title, I am not able to set the body for some reason.

I have tried two different approaches using different advice I found on other forums.

In the first case, setting node elements directly:

...
$node->title = 'Your node title';
$node->body[$node->language][0]['value'] = "<p>this is a test</p>";
$node->body[$node->language][0]['summary'] = "body summary;
$node->body[$node->language][0]['format'] = 'full_html';

In the second cases, using Entity Wrappers:

$node_wrapper = entity_metadata_wrapper('node', $node);
$node_wrapper->body->set(array('value' => '<p>New content</p>', 'format' => 'full_html'));

In both cases I am saving the node like follows:

$node = node_submit($node);
node_save($node);

And in both cases I get a new node published, but the body never gets set or displays.

How do I correctly set the body of a new node I am saving?

回答1:

To create a node using a wrapper (requires entity module) try the code below:

$entity_type = 'node';
$entity = entity_create($entity_type, array('type' => 'article'));
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$wrapper->title = 'title';
$wrapper->body->value = 'body value';
$wrapper->body->summary = 'summary';
$wrapper->body->format = 'full_html';
$wrapper->save();

In Сергей Филимонов's example, he doesn't call node_object_prepare($node) (requires node->type), which sets some defaults (is commenting enabled, is the node promoted to the front page, sets the author, ...), so there are differences between the approaches.

$entity = entity_create($entity_type, array('type' => 'article'));  

can be replaced with

$entity = new stdClass();
$entity->type = 'article';


回答2:

I see two problems here

  1. language
  2. Bundle type

If it's a new node use LANGUAGE_NONE or your site language.

For new object $node->language will be empty and you'll get notice:

Notice: Undefined property: stdClass::$language

This code works for me:

$node = new stdClass();
$node->title = 'Your node title';
$node->type = 'article';
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0]['value'] = '<p>this is a test</p>';
$node->body[$node->language][0]['summary'] = 'body summary';
$node->body[$node->language][0]['format'] = 'full_html';
$node = node_submit($node);
node_save($node);

Always set correct node bundle type here $node->type. It's the machine name of node content type.

So go to admin/content page and take a look at row with your new node:

  • empty type column - problem with bundle.
  • Undefined language () - problem with language.

However you can try to load you node with node_load() function, print it with var_dump() and take a look at your fields, may be the problem with node output.



回答3:

Agree with Сергей and just wanted to add that node_object_prepare() should also be called:

https://api.drupal.org/api/drupal/modules%21node%21node.module/function/node_object_prepare/7.x

$node = new stdClass();
$node->type = 'article';
node_object_prepare($node);

And then set other values, title, body...