8.2.x field.api.php hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.0.x field.api.php hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.1.x field.api.php hook_field_widget_WIDGET_TYPE_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
7.x field.api.php hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context)

Alter widget forms for a specific widget provided by another module.

Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a specific widget form, rather than using hook_field_widget_form_alter() and checking the widget type.


$element: The field widget form element as constructed by hook_field_widget_form().

$form_state: An associative array containing the current state of the form.

$context: An associative array containing the following key-value pairs, matching the arguments received by hook_field_widget_form():

  • "form": The form structure where widgets are being attached to. This might be a full form structure, or a sub-element of a larger form.
  • "field": The field structure.
  • "instance": The field instance structure.
  • "langcode": The language associated with $items.
  • "items": Array of default values for this field.
  • "delta": The order of this item in the array of subelements (0, 1, 2, etc).

See also



Related topics


modules/field/field.api.php, line 956
Hooks provided by the Field module.


function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) {
  // Code here will only act on widgets of type WIDGET_TYPE.  For example,
  // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
  // widgets of type 'mymodule_autocomplete'.
  $element['#autocomplete_path'] = 'mymodule/autocomplete_path';


wjaspers’s picture

The documentation is a little under-descriptive here.
I've discovered you can't simply target the widget you'd expect by searching (for instance) the form for the field #type where used.
Instead, try to be mindful of the widget type definition, by the module that creates it.

So, the following will NOT work:

 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 * Alters textfields to make them sparkly!
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
function mymodule_field_widget_textfield_form_alter(&$element, &$form_state, $context) {

But the following does work:

 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 * Alters textfields to make them sparkly!
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
function mymodule_field_widget_text_textfield_form_alter(&$element, &$form_state, $context) {

Hope this helps others out.

markie’s picture

Just as an aside, if your widget type isn't working, you may want to see how it is defined by the module. If you look at the text.module, you'll see that the textfield is in fact defined as "text_textfield"

 * Implements hook_field_widget_info().
function text_field_widget_info() {
  return array(
    'text_textfield' => array(   // <--------
      'label' => t('Text field'),
      'field types' => array('text'),
      'settings' => array('size' => 60),
    'text_textarea' => array(
      'label' => t('Text area (multiple rows)'),
      'field types' => array('text_long'),
      'settings' => array('rows' => 5),
    'text_textarea_with_summary' => array(
      'label' => t('Text area with a summary'),
      'field types' => array('text_with_summary'),
      'settings' => array('rows' => 20, 'summary_rows' => 5),
szantog’s picture

Yes, you are right.
Eg. need to do something with geofield widget, see geofield_field_widget_info() search for the element: $widgets['geofield_openlayers']
then do

function mymodule_field_widget_geofield_openlayers_form_alter(&$element, &$form_state, $context) {
zonesny’s picture

IMPORTANT: It should be mentioned that if another module calls hook_form_alter(), it may override your implementation of the above-referenced function. This may seem obvious, but I experienced this with the WYSIWYG Module in Drupal 7.20. After disabling WYSIWYG, the override worked as expected.

Rob230’s picture

You can use hook_module_implements_alter() to tell Drupal to call your module's hook after WYSIWYG. The default is alphabetic.

ron_s’s picture

Use field_info_widget_types() to display all the current widget types available to your site:

If you have the Devel module installed, you can add ...

dpm(field_info_widget_types(), 'available widget types') 

somewhere in your code, and you'll see the results.

Bill Choy’s picture

You can list the widgets on your page with the more generic hook_field_widget_form_alter.

function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {

Ashish.Dalvi’s picture

 * Implements hook_field_widget_info().
function image_field_widget_info() {
  return array(
    'image_image' => array( // image_image is image widget type name.
      'label' => t('Image'),
      'field types' => array('image'),
      'settings' => array(
        'progress_indicator' => 'throbber',
        'preview_image_style' => 'thumbnail',
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
        'default value' => FIELD_BEHAVIOR_NONE,