Module Development

Adding Settings for Field Widgets for Drupal 7

Check your version

This tutorial covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

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.


 * 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;