Check your version

This video 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: 

Adding Settings for Field Widgets

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:00
    DRUPAL 7 FIELD API
  • 0:01
    Adding Settings for Field Widgets
  • 0:02
    with Joe Shindelar
  • 0:07
    We can further customize our field widget
  • 0:10
    by allowing site administrators additional settings they can
  • 0:13
    control when attaching an instance of the rdb field to a bundle.
  • 0:17
    In this lesson, we'll look at implementing
  • 0:19
    hook_field_widget_settings_form, which allows us to add one or more
  • 0:23
    form elements to the Field Instance configuration form.
  • 0:27
    These elements can be used to collect additional settings, which

Adding Settings for Field Widgets

Loading...

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;
}
Downloads: 
Log in or sign up to download companion files.
Additional resources: