Drupal 7: How to alter image field widget "alt" or title" label

6.6k views Asked by At

I am trying to change the "alt" and title" labels in the Image Widget on the node add form.

Drupal 7 Alt and Title fields for editing

I have tried both of these hooks:

hook_field_widget_form_alter
hook_form_alter

I was unable to find where I needed to go to successfully alter the label. Could some one please direct me to the appropriate way to hook into this and alter them? I'm hitting these from a custom module if it makes a difference. Hitting them via the theme would be fine for me as well.

Any ideas anyone?

1

There are 1 answers

1
TheodorosPloumis On BEST ANSWER

You have to add an extra Proccess function for the widget form.

You can also use dpm($element) with Devel module to find more details about available keys, options etc.

// Alter image Title for field_top_image instance
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  // If this is an image field type of instance 'field_image_top'
  if ($context['field']['field_name'] == 'field_image_top') {
    // Loop through the element children (there will always be at least one).
    foreach (element_children($element) as $key => $child) {
      // Add the new process function to the element
      //dpm($element);
      $element[$key]['#process'][] = 'MYMODULE_image_field_widget_process';
    }
  }
}

function MYMODULE_image_field_widget_process($element, &$form_state, $form) {
  // Change the title field label and description
  //dpm($element);
  $element['title']['#title'] = 'NEW TITLE';
  $element['title']['#description'] = 'SOME NEW DESCRIPTION HERE.';

  // Return the altered element
  return $element;
}

See a similar issue: https://drupal.stackexchange.com/questions/32861/how-do-i-alter-image-field-code-without-hacking-core