如何在Drupal 7中禁用某个字段或使其无效 [英] How to disable a field or make it readonly in Drupal 7
问题描述
我正在尝试禁用几个字段,并通过hook_page_alter()使它们被readonly。我能够检查用户是否正在查看页面编辑部分(表单编辑)
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中禁用某个字段或使其无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!