How to set custom field value for node in Drupal 7?

I am trying to use the following code to add a new node from an external script:

define('DRUPAL_ROOT', getcwd()); include_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); $node = new stdClass(); $node->title = "Your node title"; $node->body = "The body text of your node."; $node->type = 'rasskazi'; $node->created = time(); $node->changed = $node->created; $node->status = 1; // Published? $node->promote = 0; // Display on front page? $node->sticky = 0; // Display top of page? $node->format = 1; // Filtered HTML? $node->uid = 1; // Content owner uid (author)? $node->language = 'en'; node_submit($node); node_save($node); 

But how to set a custom field value (for example, "sup_id")?

+4
source share
2 answers

Like this:

 $node->field_sup_id[LANGUAGE_NONE] = array( 0 => array('value' => $the_id) ); 

If your field has several capacities, you can add additional elements, for example:

 $node->field_sup_id[LANGUAGE_NONE] = array( 0 => array('value' => $the_id), 1 => array('value' => $other_id) ); 

And you can use the language element of the array to determine which language this particular field value refers to:

 $lang = $node->language; // Or 'en', 'fr', etc. $node->field_sup_id[$lang] = array( 0 => array('value' => $the_id), 1 => array('value' => $other_id) ); 

Add them before calling node_save() , and the contents of the field will be added / updated when you call it.

+7
source

Clive is in place. You can also use the shorthand array syntax, which is better denoted by IMHO ... for example.

 $node->field_sup_id[LANGUAGE_NONE][0]['value'] = "2499"; 
0
source

Source: https://habr.com/ru/post/1383133/


All Articles