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

烈酒焚心 提交于 2019-12-09 18:47:46

问题


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

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:


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



来源:https://stackoverflow.com/questions/18409085/drupal-7-how-to-alter-image-field-widget-alt-or-title-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!