I am implementing hook_form_FORM_ID_alter(). I have managed to setup for, and be able to collect custom values from the fields. When I submit the form, the node is created without my custom fields (i.e field_1 an field_2).

function mymodule_form_AYG_node_form_alter(&$form, &$form_state, $form_id){
  $options_first = _mymodule_get_first_dropdown_options();

  $selected = isset($form_state['values']['field_1']) ? $form_state['values']['field_1'] : key($options_first);

  $form['field_1'] = array(
    '#weight'=> -15,
    '#type' => 'select',
    '#title' => 'field 1',
    '#options' => $options_first,
    '#default_value' => $selected,    
    '#ajax' => array(
      'callback' => 'mymodule_dependent_dropdown_callback',
      // 'wrapper' => 'dropdown-second-replace',

   $form['field_2'] = array(
    '#weight'=> -14,
    '#type' => 'select',
    '#title' => 'field:',

    '#prefix' => '<div id="dropdown-second-replace">',
    '#suffix' => '</div>',

    '#options' => _mymodule_get_second_dropdown_options($selected),
    '#default_value' => isset($form_state['values']['field_2']) ? $form_state['values']['field_2'] : '',

  $form['#validate'][] = 'mymodule_form_validate';
  $form['#submit'][] = 'mymodule_form_submit';

  return $form;

function mymodule_form_submit(&$form, $form_state) {    
  // I can see values for the custom fields here. 
  // How do I store them in the node?

I have installed the Entity API module, and tried the proposed solution. It gives me an error.

function mymodule_form_submit(&$form, $form_state) {
  $node = $form_state['node']; 
  $node = node_load(arg(1));

  krumo($node);  // Getting FALSE here

  $obj = entity_metadata_wrapper('node', $node);
  $obj->field_1 = $form_state['values']['field_1'];
  $obj->field_2 = $form_state['values']['field_2'];

This is the error:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 735 of E:\wamp\www\Classified\sites\all\modules\entity\includes\entity.wrapper.inc).
The website encountered an unexpected error. Please try again later.

  • 2
    Why not just use a standard Field API field? Commented Sep 13, 2013 at 4:15

3 Answers 3


The only thing your form alter has done it added fields to a form. But you need somewhere to store the values of those fields.

When you add a field to an entity (node) using the Field UI module Drupal creates 2 database tables. One to store the field values and one to store older revisions of that fields value. Without these tables you don't have anywhere to store the values.

Here are steps to add that field your node bundle:

  1. Make sure the Field UI module is enabled.
  2. From the admin toolbar visit Structure > Content types > AYG (the name of your node type) > Manage fields.
  3. On this page add your new fields and configure as required.

Now your new fields will show on your node creation / edit forms and you will have tables to store the values of those fields.


My preferred way of saving field values to a node is by using the entity_metadata_wrapper() function that comes with the Entity module. It makes it easier for you to set or get values to and from a node object, or any other entity.

There is a good explanation of entity_metadata_wrapper() here.

So in your case, with the Entity module installed, you'd do something like:

// get the node object 
$nid = $form['nid']['#value'];
$node = node_load($nid);

// create the helper object 
$obj = entity_metadata_wrapper('node', $node);

// add the field data
$obj->field_1 = $form_state['values']['field_1'];
$obj->field_2 = $form_state['values']['field_2'];

// save the data
  • thanks for the quick reply Web. i have tried but came up with error. i dont know how to put code in reply but i have edited the actual question with code and error please have a look. I am a biginner so please ignore anything stupid :). thanks
    – Waqas
    Commented Nov 19, 2012 at 13:49
  • Are you sure the data you are saving is in the correct format of the field type? What type of field are you using and what type of data are you trying to save? For example, if you are using an integer field type, you cannot save a string value, etc. Commented Nov 19, 2012 at 14:07
  • krumo($form); [nid]['#value'] is null. its just a general form anf fields are getting vocabulary terms as their values from the database.
    – Waqas
    Commented Nov 19, 2012 at 14:21
  • You just need to put the node id into node_load(). Perhaps you can take this from arg(1), assuming the URL is node/%node/edit Commented Nov 19, 2012 at 14:34
  • 2
    I think there's a misunderstanding here...from the code in the OP it doesn't look like these are field API fields, but rather custom fields that are being added to the form. @Waqas That's not how entities work in Drupal - if you want data to be stored against the node, you need to add a field using the Field UI (or API if you're comfortable with that), or add a custom table and save the data to it in hook_node_insert()/hook_node_update() and load it back on to the node object with hook_node_load()
    – Clive
    Commented Nov 19, 2012 at 18:33

For saving custom form element values, you have to implement hook_node_insert() in your module. I hope this tutorial will help you to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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