| 7 field.api.php | hook_field_widget_form_alter(&$element, &$form_state, $context) |
| 8 field.api.php | hook_field_widget_form_alter(&$element, &$form_state, $context) |
Alter forms for field widgets provided by other modules.
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 to which widgets are being attached. This may 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_WIDGET_TYPE_form_alter()
Related topics
1 function implements hook_field_widget_form_alter()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- field_test_field_widget_form_alter in modules/
field/ tests/ field_test.module - Implements hook_field_widget_form_alter().
File
- modules/
field/ field.api.php, line 906
Code
function hook_field_widget_form_alter(&$element, &$form_state, $context) {
// Add a css class to widget form elements for all fields of type mytype.
if ($context['field']['type'] == 'mytype') {
// Be sure not to overwrite existing attributes.
$element['#attributes']['class'][] = 'myclass';
}
}
Comments
Requires Drupal 7.8 or higher
PermalinkNote that this hook was introduced in Drupal 7.8 from http://drupal.org/node/1204230. It is not invoked if you are using a version of Drupal lower than 7.8.
was backported to 7.18
PermalinkUpdate: I am using Drupal 7.18, and this hook is present and working, and great.
That's because 18 > 8.
PermalinkThat's because 18 > 8.
Also you need to apply
PermalinkAlso you need to apply security updates :P
My Example how to change a file image fieldset to be collapsible
PermalinkWhen using a multi image field the form can get rather long. Below is how I changed the fieldset wrapper to be collapsible.
Its not perfect, because I was forced to always set the fields set to be un-collapsed by default. This is because the ajax re-renders the fieldset and then screen position jumps.
<?php
/**
* Implements hook_field_widget_form_alter() to alter image fieldsets to be collapsible.
*
* Code example taken from system_element_info();
*/
function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'image') {
$element['#collapsible'] = TRUE;
// Ajax upload seems to re-render the fieldset as collapsed causing the screen to jump, better to leave the default as not collapsed
$element['#collapsed'] = FALSE;
if (!isset($element['#pre_render'])) {
$element['#pre_render'] = array();
}
$element['#pre_render'][] = 'form_pre_render_fieldset';
if (!isset($element['#process'])) {
$element['#process'] = array();
}
$element['#process'][] = 'form_process_fieldset';
// $element['#process'][] = 'ajax_process_form'; // Dont know if this is needed, seems to work without it
}
}
?>