2

I have a field which I don't want to be displayed for certain users, however I still want a value to be entered for that field, which is extrapolated from other fields they enter data in.

In order to do this, I'm setting the field as hidden in hook_form_alter:

$form['field_target']['#type'] = 'hidden';

and then setting the value of the field from within a form_validate function based on another field, such as:

$form_state['values']['field_target']['und']['0'] = 
    $form_state['values']['field_source']['und']['0'];

And this works fine, unless I force a form validation error on field_source. When I correct the validation error and submit again, I get a white screen with PHP Error:

PHP Fatal error:  Cannot create references to/from string offsets 
    nor overloaded objects in includes/common.inc on line 6288

This error happens even if I comment out the setting of field_target. However I can fix this error by removing the [#type'] = 'hidden' line and everything works fine, except that the field shows for all users.

Is there any way to fix my above approach, or is there a better way to achieve what I'm trying to do?

3

Ok it seems I can get around this error by setting:

$form['field_target']['#access'] = FALSE;

instead of ['#type'] = 'hidden'; and Drupal no longer freaks out.

If anybody has a better idea though I'll glady accept a different answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.