【问题标题】:Drupal 7: How to alter image field widget "alt" or title" labelDrupal 7:如何更改图像字段小部件“alt”或标题“标签
【发布时间】:2013-08-23 18:03:36
【问题描述】:

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

这两个钩子我都试过了:

hook_field_widget_form_alter
hook_form_alter

我找不到成功更改标签所需的位置。有人可以指导我以适当的方式来解决这个问题并改变它们吗?如果它有所作为,我会从自定义模块中获得这些。通过主题击中它们对我来说也很好。

有什么想法吗?

【问题讨论】:

    标签: drupal-7 widget hook-form-alter


    【解决方案1】:

    您必须为小部件表单添加一个额外的处理功能

    您还可以将 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;
    }
    

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

    【讨论】:

    • 很好,如果您希望标题高于 alt,如果您使用 title 作为标题和 alt 作为图像的描述/标题,您也可以更改权重 - 您可能想要这样做.例如:$element['title']['#weight'] = 1;和 $element['alt']['#weight'] = 2;
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2016-05-14
    相关资源
    最近更新 更多