8.5.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.0.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.1.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.2.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.3.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.4.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
8.6.x field.api.php hook_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context)
7.x field.api.php hook_field_widget_form_alter(&$element, &$form_state, $context)

Alter forms for field widgets provided by other modules.


$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



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().


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


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


Dave Reid’s picture

Note 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.

jfrederick’s picture

Update: I am using Drupal 7.18, and this hook is present and working, and great.

griz’s picture

That's because 18 > 8.

griz’s picture

Also you need to apply security updates :P

NaX’s picture

When 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.

 * 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
dizi.joao’s picture

I´m using drupal 7.22, have a lot of hooks in my module, all working, but this one doesnt hook. That´s my code:

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


gonzaguer’s picture

may be dump response but
did you flushed the cache? it worked for me. (D7.22)

bjmiller121’s picture

There doesn't seem to be a way to add a submit handler within this hook. I've tried just about everything and the only handler I can seem to get working is an "#element_validate" handler.

for example, this submit handler never gets called:

function MY_MODULE_field_widget_form_alter(&$element, &$form_state, &$context) {
  $element[0]['field_name'] = array(
    '#type' => 'checkbox',
    '#title' => 'Field Lable',
    '#default_value' => FALSE,
    '#submit' => array('MY_MODULE_widget_form_submit'),

function MY_MODULE_widget_form_submit($form, &$form_state) {
  print "it works!";
danithaca’s picture

I don't think there's a way to add submit handler to the widget form. However, I could use "hook_form_alter" and just change the element and add form handler instead. It's perhaps not a good idea to alter the widget element and handle submission.

SocialNicheGuru’s picture

I printed out the fields and none of the fieldsets provided by modules are included. Am I missing something?

ngocketit’s picture

Does this hook work for fields with multiple items as want to alter the widget item forms?. For example, when user clicks "Add more", I want the form returned from Ajax to be altered. It doesn't seem to work for me with file_generic widget. Any ideas?

blacklabel_tom’s picture

Just a small thing I've learned today. If you want to add a validate handler to a specific field using this hook you need to add it in #element_validate (https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...)

Just in case anyone else runs into the issue where I want to add validation to a field in a field collection attached to a paragraph so hook_form_alter isn't going to be possible to use.



nortmas’s picture

Helped thx! :)