I have the following content type called stars with the following fields:

Distance (this is an added text field)

Here's what I have to save the node, but how do I add the distance field?

$node = new stdClass();
$node->type = "stars";
$node->status = 0;
$node->uid = $user->uid;
$node->title = 'This is the title';
$node->sticky = 0;
$node->language = 'en';

// Let's add standard body field
$node->body['und'][0]['format'] = 'filtered_html';
$node->body['und'][0]['value'] = 'Body info';

$node->revision = 0;


You'd need to set the value for the Distance field just like you have already for the Body field. Lets assume that the name of your Distance field is field_star_distance. (To find the name of your field, check if you have the Field UI module enabled and head over to the Manage fields page for your content type)

// Your code
$node->{'field_star_distance'}[LANGUAGE_NONE][0]['value'] = 100;
// Your code

The general skeleton for setting the value for a field of an entity is:

$entity->{$field_name}[$language][$delta][$field_property] = $value

To understand the different field properties for a given field (like value in our example here), you need to look up its schema (see hook_field_schema() and see text_field_schema())

To see if $delta can be > 1, you need to check the cardinality of the field instance.

| improve this answer | |

$node->field_name[LANGUAGE_NONE][0]['value']= $name;

where 'field_name[LANGUAGE_NONE][0]['value'] 'is the name of the extra added node field.you can get this name by inspect property of browser.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.