2013-08-23 53 views
2

我试图更改”ALT“和”标题“标签在图像小部件上的节点添加窗体。Drupal 7:如何更改图像字段小部件“ALT”或标题“标签

Drupal 7 Alt and Title fields for editing

我已经试过这两个挂钩:

hook_field_widget_form_alter 
hook_form_alter 

我无法找到地方,我需要去改变成功的标签。请有人请指导我以适当的方式勾住并改变它们吗?如果它有所作为,我会从定制模块中点击这些。通过主题击打他们对我来说也不错。

任何想法的人?

回答

14

您必须为widget窗体添加额外的Proccess功能

您还可以使用DPM($元素)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

+0

合作。谢谢! – Jnicola

+0

谢谢!好的样板解决方案。 – oknate

+0

不错,如果你希望标题在ALT之上,如果你使用标题作为标题和ALT作为图像的描述/标题,你也可以改变权重 - 你可能想要这样做。例如:$ element ['title'] ['#weight'] = 1;和$ element ['alt'] ['#weight'] = 2; – klidifia

相关问题