function hook_field_widget_WIDGET_TYPE_form_alter

You are here

7 field.api.php hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context)
8 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.

Parameters

$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

hook_field_widget_form()

hook_field_widget_form_alter()

Related topics

File

modules/field/field.api.php, line 946

Code

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';
}

Comments

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:

<?php
/**
* 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:

<?php
/**
* 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.

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),
    ),
  );
}

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

<?php
function mymodule_field_widget_geofield_openlayers_form_alter(&$element, &$form_state, $context) {
  ...
}
?>

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.

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