如何在 Drupal 7 中禁用字段或使其只读

发布于 2024-10-26 14:08:28 字数 1054 浏览 7 评论 0原文

我正在尝试禁用几个字段并通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

臻嫒无言 2024-11-02 14:08:28

它适用于文本字段^

$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;

It works for text fields^

$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;
蘸点软妹酱 2024-11-02 14:08:28

就像文档中所述,

您可以使用属性 :

$form['#attributes'] = array('disabled' => TRUE);

Like it said in the docs

You can use attributes :

$form['#attributes'] = array('disabled' => TRUE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文