0

I can't seem to find the answer to this anywhere.

Programmatically, I need to add new values to a custom field of a new node instance I just created via API. I am taking input via a form and want to add that data to the custom field in the new node. I know the columns of the custom field.

How can I do that, or where is a good reference?

(the custom field type is address, if that matters)

1

If you are using a custom form which is invoked by a menu callback, you could try this.

Assuming that you have a postal address field type (provided by the addressfield module) which as 5 sub-fields - country, address 1, address 2, city and postal code in your content type.

/**
 * This will be invokded by a menu callback
*/
function yourmodule_custom_form($form, &$form_state){

    /* your form elements definition here */

    $form['#validate'][] = 'yourmodule_custom_form_validate';       
    $form['#submit'][] = 'yourmodule_custom_form_submit';

    return $form;
}
/**
 * Validation attached to the form
*/  
function yourmodule_custom_form_validate($form, &$form_state){
    // do validation here
}
/**
 * Submit handler attached to the form
*/
function yourmodule_custom_form_submit($form, &$form_state){
    global $user;
    $values = $form_state['values'];

    # Create a node object, and add node properties.
    $node = new stdClass();
    $node->type = 'your_content_type';
    $node->uid = $user->uid;
    $node->created = strtotime("now");
    $node->changed = strtotime("now");
    $node->status = 1;
    $node->comment = 0;
    $node->promote = 0;
    $node->moderate = 0;
    $node->sticky = 0;
    $node->language = LANGUAGE_NONE;

    $field_address = $values['field_address'][LANGUAGE_NONE][0];

    $node->field_address_country = $field_address['country'];
    $node->field_address_thoroughfare = $field_address['thoroughfare'];
    $node->field_address_premise = $field_address['premise'];
    $node->field_address_locality = $field_address['locality'];
    $node->field_address_postal_code = $field_address['postal_code'];

    node_object_prepare($node);
    # Save the node.
    # The node will also get published, if $node->status was set to 1.
    node_save($node);
    $nid = $node->nid;
    # if you want to redirect to a page after submission, use this
    $form_state['redirect'] = 'path/to/redirect';
    drupal_redirect_form($form_state);
}

I did not test the code. It is just for example. You may need to amend it by yourself. You can inspect your address field name by printing out $form.

dpm($form); // if you have devel module enabled
echo '<pre>'; print_r($form); echo '</pre>'; // if you don't have devel moduel

Also note that when you create a new field, Drupal creates two new tables starting with field_data_field and field_revision_field. If you name the field "address", you will see two new tables field_data_field_address and field_revision_field_address, you could look up into those tables.

  • Sithu, I am presenting a regular form to gather the data. How would this hook be called after the user submits the form? – vintorg Nov 12 '12 at 5:09
  • I may have answered my question. Do I use hook_form() and test the node type and call my custom form? – vintorg Nov 12 '12 at 5:18
  • Are you rendering a custom form which is invoked by a menu page callback? Can you please expand your question? – Sithu Nov 12 '12 at 5:44
  • Yes, it is a custom form invoked by a menu callback. I need to do some extra validating and processing on the input data before it is added to the custom field. – vintorg Nov 12 '12 at 7:40
  • I've updated my answer. Please check. – Sithu Nov 12 '12 at 8:04
0

Sithu, thanks for your help. That worked. In the end, Drupal made it easier than I was thinking it would be. I'll post my code and do a walk-through when I get some time.

  • Glad to hear that. But this is not an answer. You should just leave a comment. – Sithu Dec 5 '12 at 3:02

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.