Module Development

Adding Settings for Field Widgets for Drupal 7

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Sprout Video

In order to allow for maximum flexibility in our widget we can add widget settings that apply to each individual instance of our field. By implementing hook_field_widget_settings_form() and then refactoring some of our existing code we can make it possible for a site administrator to set a custom prefix value for the label field, which the Field API will store for as part of the field instance's settings and we can use it when creating our widget.

Implementations of hook_field_widget_settings_form() return a Form API array that represents the element or elements that you would like to add to the widget settings form. Values are automatically serialized and saved as part of the field's instance configuration and can be accessed by the passed in $instance array's $instance['widget']['settings'] key.

Example:


/**
 * Implements hook_field_widget_settings_form().
 */
function rgb_field_widget_settings_form($field, $instance) {
  $element = array(
    'rgb_label_text' => array(
      '#type' => 'textfield',
      '#title' => t('Alternate label text'),
      '#description' => t('If an alternate label text is provided it will be used in place of the default "Color" title for the label field.'),
      '#default_value' => isset($instance['widget']['settings']['rgb_label_text']) ? $instance['widget']['settings']['rgb_label_text'] : '',
    ),
  );

  return $element;
}

Additional resources

Legacy Module Development