Drupal 7:如何更改图像字段小部件"alt"或标题"标签

Jni*_*ola 3 widget drupal-7 hook-form-alter

我正在尝试更改节点添加表单上的图像小部件中的"alt"和标题"标签".

Drupal 7 Alt和Title字段用于编辑

我试过这两个钩子:

hook_field_widget_form_alter
hook_form_alter
Run Code Online (Sandbox Code Playgroud)

我无法找到我需要去哪里成功改变标签.有人可以指导我以适当的方式加入这个并改变它们吗?如果它有所作为,我正在从自定义模块中击中它们.通过主题击中它们对我来说也没问题.

任何人的想法?

The*_*mis 16

您必须为窗体小部件窗体添加额外的Proccess函数.

您还可以将dpm($ element)Devel模块一起使用,以查找有关可用键,选项等的更多详细信息.

// 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;
}
Run Code Online (Sandbox Code Playgroud)

看到类似的问题:https://drupal.stackexchange.com/questions/32861/how-do-i-alter-image-field-code-without-hacking-core