简体   繁体   English

当在hook_form_alter()中对字段使用['#access']时,我失去了'$ form field default value'和$ form_state输入

[英]When using ['#access'] in hook_form_alter() for a field than I am loosing '$form field default value' and $form_state input

When using an EntityReference field and in hook_form_alter() hiding that field with ['access'] than after submitting form $form_state input is empty for that field. 当使用EntityReference字段并且在hook_form_alter()中使用['access']隐藏该字段时,比在提交表单$ form_state之后,该字段的输入为空。

I have field field_to_a which is an entityrefence field on content type B that references content type A. 我有field_to_a字段,它是内容类型B上的一个实体引用字段,它引用内容类型A。

I am using hook_form_alter() in which i am using a line like $form['field_to_a']['#access'] = FALSE; 我正在使用hook_form_alter(),其中我正在使用$form['field_to_a']['#access'] = FALSE; and also setting this field to a certain value 并将此字段设置为某个值

When I am pressing submit button (my Save button) on node add: 当我在节点上按下提交按钮(我的保存按钮)时,添加:

  • When I am using $form['field_to_a']['#access'] = FALSE; 当我使用$form['field_to_a']['#access'] = FALSE; : I am loosing $form_state['input']['field_to_a'] (does not exist) and $form['field_to_a']['und'][0]['target_id']['#default_value'] (empty) and I have PHP errors. :我丢失了$ form_state ['input'] ['field_to_a'](不存在)和$form['field_to_a']['und'][0]['target_id']['#default_value'] (空),并且出现PHP错误。
  • When I am using NOT $form['field_to_a']['#access'] = FALSE; 当我使用NOT $form['field_to_a']['#access'] = FALSE; : I have $form_state['input']['field_to_a'] properly and node is saved without any errors. :我有正确的$ form_state ['input'] ['field_to_a'],并且节点已保存,没有任何错误。

When I am using any other field I can use ['#access'] = FALSE and not loosing input in $form_state for that field. 当我使用任何其他字段时,我可以使用['#access'] = FALSE而不丢失该字段在$ form_state中的输入。

Is there any solution or workaround for this thing? 这个东西有解决方案吗? Is it a bug or normal behavior? 是错误还是正常行为?

Thanks 谢谢

First check log what you have received. 首先检查日志,您收到了什么。

try this : 尝试这个 :

function hook_submit($form, &$form_state) {
    watchdog("Your Form data ", '<pre>'.print_r($form_state, true).'</pre>'); 
}

Then check your log report. 然后检查您的日志报告。 you will receive your value in $form_state['values'] 您将在$form_state['values']收到您的值

It may helps you 可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM