How do I hide a CCK Nodereference input widget in #after_build?

…衆ロ難τιáo~ 提交于 2019-12-02 20:01:09

问题


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


来源:https://stackoverflow.com/questions/3611381/how-do-i-hide-a-cck-nodereference-input-widget-in-after-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!