| 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_alter()
Related topics
File
- modules/
field/ field.api.php, line 930
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';
}
Login or register to post comments
Comments
The documentation is a little
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.
Widget type defined in hook_field_widget_info()
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
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
<?phpfunction mymodule_field_widget_geofield_openlayers_form_alter(&$element, &$form_state, $context) {
...
}
?>