如何在 Drupal 7 中禁用字段或使其只读
我正在尝试禁用几个字段并通过 hook_page_alter() 将它们设置为只读。我能够检查用户是否正在查看页面编辑部分(表单编辑)
$page['content']['system_main']['#node_edit_form'] == TRUE)
,然后当我尝试禁用几个字段时,我发现可以通过此代码禁用选择列表:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#attributes']['disabled'] = TRUE;
但如果我使用以下代码,它不会不起作用:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
我还发现我不能使用相同的代码来禁用文本区域字段:
$page['content']['system_main']['field_my_text_area']['und']['#attributes']['disabled'] = TRUE;
上面的代码不会禁用文本区域,但相同的代码可以禁用选择列表!
然后我尝试 hook_form_alter() 做同样的事情,并且我能够禁用字段,当我从 $page 数组检查渲染的数组时,我看到它显示:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
但是当我在 hook_page_alter() 中设置相同的代码时,它没用。看起来其他东西会覆盖它,我认为 hook_page_alter() 是更改标记的最后一个地方。
知道在 drupal 7 中的 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!
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;
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.
Any idea what is the best way to disable/readonly any kind of field, inside hook_page_alter() in drupal 7?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它适用于文本字段^
It works for text fields^
就像文档中所述,
您可以使用属性 :
Like it said in the docs
You can use attributes :