Ale*_*xey 1 drupal drupal-7 drupal-forms
我有一个带有图像字段的内容类型。用户可以创建内容并上传图像。我想不会允许用户更改/删除图像,一旦它被上传,但仍显示节点编辑表单上的图像。所以我只需要禁用/删除图像字段中的“删除”按钮。我尝试了以下(通过 hook_form_alter),但没有奏效:
$form['field_image']['#disabled'] = TRUE;
Run Code Online (Sandbox Code Playgroud)
下面的工作,但它完全隐藏了图像元素,这不是我所追求的:
$form['field_image']['#access'] = FALSE;
Run Code Online (Sandbox Code Playgroud)
请帮助找到解决方法。
您必须使用hook_field_widget_form_alter函数并在其中使用 dpm() 查找变量详细信息,然后使用来自Forms API的属性更改按钮。
但我建议让小部件字段只在编辑表单上读取,而不是删除删除按钮。
// Hide remove button from an image field
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
// If this is an image field type
if ($context['field']['field_name'] == 'MY_FIELD_NAME') {
// 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
$element[$key]['#process'][] = 'MYMODULE_image_field_widget_process';
}
}
}
function MYMODULE_image_field_widget_process($element, &$form_state, $form) {
//dpm($element);
// Hide the remove button
$element['remove_button']['#type'] = 'hidden';
// Return the altered element
return $element;
}
Run Code Online (Sandbox Code Playgroud)
有用的问题:
| 归档时间: |
|
| 查看次数: |
4560 次 |
| 最近记录: |