How do I hide a CCK Nodereference input widget in

2019-03-05 13:57发布

问题:

I like simplifying the node form. One of my tricks in the past has been to conditionally hide CCK elements on new node creation when I want to enforce some kind of default. One of my favorite tricks is to whisk away things put in place by the Prepopulate module. Unfortunately for me, it's recent move to an #after_build-based mechanism seems to be creating all kinds of collisions in how I can manipulate the widget.

This is what I used to do in hook_form_alter():

  $form['field_my_nodereference_field'][0]['#type'] = 'hidden';
  $form['field_my_nodereference_field'][0]['#value'] = $form['field_my_nodereference_field'][0]['#default_value']['nid'];
  $form['field_my_nodereference_field'][0]['#parents'] = array('field_my_nodereference_field', 0, 'nid');

But when I try to play this game in #after_build, I run into errors with the hidden type's validation, or the nodereference_autocomplete_validation. I have resorted to conditionally adding a CSS file. This makes me sad.

回答1:

Hidden is not enough. Try this one:

$form['field_my_nodereference_field'][0]['#type'] = 'nodereference_hidden';

when the type is a CCK field you have to pass this format _hidden

for instance for a simple text field I used

$form['field_srt'][0]['#type'] = 'text_hidden';

or for a filefield field I used

$form['field_myfile'][0]['#type'] = 'filefield_hidden';