Problem with hook_form_alter: Cannot unset string offsets
Hi. I am hooking a node form. The content type has a cck node reference field, and the form has a text field to select the node. In some cases, the user should not be allowed to select...the value should be forced. So I decide which field name to use based on the content type, and then change the text field to a hidden field like this: $form[$var] = array( '#type' => 'hidden', '#value' => $nid, ); The form comes up just fine. The problem is, when it is submitted, I receive: *Fatal error*: Cannot unset string offsets in *sites\all\modules\cck\content.module* on line *1248* Thanks, Jeff
Le lundi 28 juin 2010 à 10:46 -0400, Jeff Greenberg a écrit :
Hi. I am hooking a node form. The content type has a cck node reference field, and the form has a text field to select the node. In some cases, the user should not be allowed to select...the value should be forced. So I decide which field name to use based on the content type, and then change the text field to a hidden field like this:
$form[$var] = array(
'#type' => 'hidden', '#value' => $nid, );
The form comes up just fine. The problem is, when it is submitted, I receive:
Fatal error: Cannot unset string offsets in sites\all\modules\cck \content.module on line 1248
Thanks,
Jeff
Some content fields have a complex structure you should take care of. Another note, you might want to use '#type' => 'value' instead of 'hidden', which will be saved into form cache but not rendered at all in the final HTML. Pierre.
Hi Pierre, I'll do that regarding the type, thanks. So, by 'take care of'... the original field is an autocomplete text field, and I'm thinking you mean that I should disassemble it (or at least unset it) first, rather than have array keys still present with the 'value' type leftover from the text field? Jeff
Some content fields have a complex structure you should take care of.
Another note, you might want to use '#type' => 'value' instead of 'hidden', which will be saved into form cache but not rendered at all in the final HTML.
Pierre.
Le lundi 28 juin 2010 à 10:59 -0400, Jeff Greenberg a écrit :
Hi Pierre, I'll do that regarding the type, thanks. So, by 'take care of'... the original field is an autocomplete text field, and I'm thinking you mean that I should disassemble it (or at least unset it) first, rather than have array keys still present with the 'value' type leftover from the text field?
Jeff
I did not meant that, but yes, you also should do that! I mean CCK fields are sometime a complex form structure. As a start, any multiple content field sub form is itself an array of this particular field type single value sub form, if it's configured as multiple. Even the single value sub form, for some particular types, can be a complex structure of form elements, that is not the case with node reference (as I could remember, but I could be wrong). In this case, you have to set an array of value form elements that matches the current existing values (or the new one you want to add). Pierre.
Whats the value of $var? Because it's a CCK field you may need to use $form[$var]['#type'] = 'hidden'; $form[$var]['#value'] = $nid; to avoid clearing elements CCK expects to find. (Side note: I would use 'value' instead of 'hidden') Nevets On 6/28/2010 9:46 AM, Jeff Greenberg wrote:
Hi. I am hooking a node form. The content type has a cck node reference field, and the form has a text field to select the node. In some cases, the user should not be allowed to select...the value should be forced. So I decide which field name to use based on the content type, and then change the text field to a hidden field like this:
$form[$var] = array(
'#type' => 'hidden', '#value' => $nid, );
The form comes up just fine. The problem is, when it is submitted, I receive:
*Fatal error*: Cannot unset string offsets in *sites\all\modules\cck\content.module* on line *1248*
Thanks,
Jeff
Well, that's an interesting point, and negates my previous comment. Since I'm starting with $form[$var] = array( I won't have any artifacts from the original field, will I? So wouldn't the field then be composed of only those two parameters, #type and #value? That should be all it needs. Changed it to 'value' from 'hidden', unfortunately there was no side benefit of correcting it. When I change it from $form[$var] = array( to $form[$var]['#type'] = etc. I no longer get the from, but get the error Only variables can be passed by reference Jeff On 6/28/2010 10:57 AM, Steve Ringwood wrote:
Whats the value of $var?
Because it's a CCK field you may need to use $form[$var]['#type'] = 'hidden'; $form[$var]['#value'] = $nid; to avoid clearing elements CCK expects to find.
(Side note: I would use 'value' instead of 'hidden')
Nevets
Ah, found the problem. http://drupal.org/node/726282 Apparently the form field contents of a cck field are Not what one expects at the time of hook_form_alter :-(
participants (3)
-
Jeff Greenberg -
Pierre Rineau -
Steve Ringwood