0
votes

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.

I have field field_to_a which is an entityrefence field on content type B that references content type A.

I am using hook_form_alter() in which i am using a line like $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;: 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.
  • When I am using NOT $form['field_to_a']['#access'] = FALSE;: I have $form_state['input']['field_to_a'] properly and node is saved without any errors.

When I am using any other field I can use ['#access'] = FALSE and not loosing input in $form_state for that field.

Is there any solution or workaround for this thing? Is it a bug or normal behavior?

Thanks

1
hook_field_attach_form() needs to be used as hook_form_alter() looses default valuesuser3563097

1 Answers

0
votes

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']

It may helps you