Posted by alexeyl on April 8, 2008 at 2:36pm
Hi!
I need to alter CCK content type node form page. So I'd like to be able to render this form myself and surround it with my additions.
Here is how I do this:
$form = node_form(node_load($node_id));
$output .= drupal_render($form);
node id is correct and form is populated. But errors occur while rendering it:
* warning: implode() [function.implode]: Invalid arguments passed in xxx\includes\form.inc on line 548.
* warning: implode() [function.implode]: Invalid arguments passed in xxx\includes\form.inc on line 548.
What that could mean? how can I get this CCK form rendered?
Thank you!
Comments
It might help if you said
It might help if you said what you are trying to achieve, why/how are you trying to modify the form. Depending on your goal you probably want to theme the form and/or use hook_form_alter().
Thanks for your
Thanks for your response.
Currently I trigger this form as node/<id>/edit. But I want to output this form somewhere else inside of another page, that contains another valuable related information. Is this possible?
Also about hook_form_alter - how could I know exact id of the form from CCK? It will be just name of the type, right?
Thanks, Alex.
...
Why are you breaking the process into two steps? You're saying "and surround it with my additions", but for merely surrounding it you don't need access to $form itself.
Here's a way to do it:
$node = node_load($node_id);
$output = "my prefix";
$output .= drupal_get_form($node->type .'_node_form', $node);
$output .= "my suffix";
It's "xyz_node_form". "xyz" stands for the the node-type.
===
And if you _do_ need access to $form, do something along of:
$node = node_load($node_id);
$form_id = $node->type .'_node_form';
$form = drupal_retrieve_form($form_id, $node);
//... manipulate $form ...
drupal_process_form($form_id, $form);
$output = drupal_render_form($form_id, $form);
You shouldn't pass to drupal_render() a form that wan't built yet (by drupal_process_form()), because various values need initialization.
Great! You made my day!
Great! You made my day! ;)
Thanks for your help!
Thanks
Very cool... I try it for node creation but without success. I have nothing in $output :( and this error:
* warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'node_form' was given in /Users/simon/Sites/site.com/includes/form.inc on line 371.* warning: Missing argument 3 for drupal_process_form(), called in /Users/simon/Sites/site.com/sites/all/modules/weekli_event_add/site_event_add.module on line 130 and defined in /Users/simon/Sites/site.com/includes/form.inc on line 396.
Here my code:
<?phpglobal $user;
//Get cck form
$node_type = 'location';
$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
$form_id = $node_type . '_node_form';
$form = drupal_retrieve_form($form_id, $node);
//... manipulate $form ...
drupal_process_form($form_id, $form);
$output .= drupal_render_form($form_id, $form);
return $output;
?>
EDIT all work now
After some test and adapte the code to D6 i have no more error. The form is returned but I have any field only:
- Revision information
- Authoring information
- Publishing options
The code:
<?phpglobal $user;
require_once("modules/node/node.pages.inc");
//include_once(drupal_get_path('module', 'node') .'/content_types.inc');
//include_once(drupal_get_path('module', 'content') .'/includes/content.admin.inc');
$node_type = 'location';
$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
$form_id = $node_type . '_node_form';
$form = drupal_retrieve_form($form_id, &$form_state,$node);
//... manipulate $form ...
// prepare the $form
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form,$form_state);
$output .= drupal_render_form($form_id, $form);
return $output;
?>
EDIT all work with this code (stupid error in variable name):
<?phprequire_once("modules/node/node.pages.inc");
$node_type = 'location';
//$node = node_load(2);
$node = array('type' => $node_type,'uid' => $GLOBALS['user']->uid, 'name' => $GLOBALS['user']->uid);
$form_state = array();
$form_id = $node_type . '_node_form';
// create a basic $node array
$form = drupal_retrieve_form($form_id, $form_state,$node);
//... manipulate $form ...
//prepare the $form
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form,$form_state);
$output .= drupal_render_form($form_id, $form);
return $output;
?>