I am trying to create a custom widget for the entity reference field type which will be a hidden field with its value set to the current page's node id. This is my code:

function my_example_field_widget_info() {
  return array(
    'my_example_hidden_nid' => array(
      'label' => t('Hidden nid field'),
      'field types' => array('entityreference'),

/* implements hook_field_widget_form*/
function my_example_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element['hidden_nid'] = array(
    '#description'=>t('The nid of the Photo node which product line item refers to'),
  return $element;

I first tried this as a textfield, and saw that the form field was being properly populated.

This field is attached to a Drupal Commerce Line Item, to produce some custom functionality. However, when I clicked Add to Cart, and the Line Item was saved, this entityreference field was not saved into the database. (Since I am adding a widget to a previously defined field type, I did not use any hook_field_schema()'s)

How can I fix this?

1 Answer 1


For one, in your hook_field_widget_form, the $element should be built with a Union (like $element += array(...)) and not replaced. Refer this example at the api page.

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.