问题描述
我正在尝试禁用几个字段并通过 hook_page_alter() 将它们设为只读.我能够检查用户是否正在查看页面编辑部分(表单编辑)
I am trying to disable couple of fields and make them readonly via hook_page_alter(). I was able to do check if user is viewing the page edit section (the form edit)
$page['content']['system_main']['#node_edit_form'] == TRUE)
然后当我尝试禁用几个字段时,我发现选择列表可以通过以下代码禁用:
then when I tried to disable couple of fields, I found that select list can be disabled by this code:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#attributes']['disabled'] = TRUE;
但是如果我使用下面的代码就不行了:
but if I use the following code it doesn't work:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
我还发现我不能使用相同的代码来禁用文本区域字段:
I also found that I can not use the same code to disable a text area field:
$page['content']['system_main']['field_my_text_area']['und']['#attributes']['disabled'] = TRUE;
上面的代码没有禁用文本区域,但是同样的代码可以禁用选择列表!
The above code doesn't disable the text area, but the same code can disable the select list!
然后我尝试 hook_form_alter() 来做同样的事情,我能够禁用字段,当我从 $page 数组中检查呈现的数组时,我看到它显示:
Then I tried hook_form_alter() to do the same thing, and I was able to disable fields and when I checked the rendered array from $page array, I saw that it shows:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
但是当我在 hook_page_alter() 中设置相同的代码时,它不起作用.看起来其他东西会覆盖它,我认为 hook_page_alter() 是最后一个更改标记的地方.
but when I set the same code in hook_page_alter(), it didn't work. Looks like something else will override it, I thought that hook_page_alter() is the last place to change markup.
知道在 drupal 7 中的 hook_page_alter() 中禁用/只读任何类型字段的最佳方法是什么吗?
Any idea what is the best way to disable/readonly any kind of field, inside hook_page_alter() in drupal 7?
谢谢
推荐答案
适用于文本字段^
$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;
这篇关于如何在 Drupal 7 中禁用字段或使其只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!