0
votes

Currently I am building an installation profile for Drupal 7. I make multiple fields and create instances for the fields at a specific content type.

Now I add a new node to the content type but I don't know how to add data into the fields I generated for this content type. This has to happen all in the installation profile so in code and no explanation for Drupal it self.

The node is generated like this:

$node          = new stdClass();
$node->title   = 'Test title';
node_save($node);

This is not the whole code of course but is just to give you an idea. Currently I add the type, status, uid, title, promote, created, timestamp, sticky, format, language, teaser, body and revision to the $node. Now I want to add my custom field data, anybody has an idea on how to do this?

1

1 Answers

0
votes

Something like...

$node = new stdClass();
$node->type = 'article';
$node->title = 'Test title';
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
// Other properties...

// Single cardinality
$node->field_some_text_field[$node->language][0]['value'] = 'Some value';

// Multiple cardinality
$node->field_some_entity_reference_field[$node->language][]['target_id'] = 123;
$node->field_some_entity_reference_field[$node->language][]['target_id'] = 456;

node_save($node);