drupal_execute() is great for processing a form and drupal_get_form() is great for generating the html for a form, but what if you'd like to load the $form object to modify or embed it?
<?php
// include drupal's node include file
require("modules/node/node.pages.inc");
// create an empty $form_state array
$form_state = array();
// define the content type of the form you'd like to load
$nodeType = 'MYCCKNODETYPE';
// create a string of the $form_id
$form_id = $nodeType . '_node_form';
// create a basic $node array
$node = array('type' => $nodeType, 'uid' => $GLOBALS['user']->uid, 'name' => $GLOBALS['user']->uid);
// load the $form
$form = drupal_retrieve_form($form_id, $form_state, $node);
// prepare the $form
drupal_prepare_form($form_id, $form, $form_state);
// show the form structure, debug
echo "<PRE>";
print_r($form);
echo "</PRE>";
?>










CCK Fields missing
This is great! But the CCK fields for the content type are missing. Any thoughts?
awesome
Pretty sure you have no idea how much headache you just saved me with this. I had all the code and in the right order, but no form_state array. Just never occurred to me to create a blank one. THANK YOU.
Christo
This helped me a lot!
Thank you!
$form object with populated fields of node
Thanks for sharing this example, but would you happen to know how to return the form fields populated with the values of an existing node? I thought this is what the third parameter of drupal_retrieve_form() was for. I passed a fully loaded node object, but alas the form fields come back empty.
How to populate a form from another node
Hi,
After a big headache of research, I finally got into the node module and analysed how node_add function worked (node.pages.inc).
Here is the solution I created:
<?phpmodule_load_include('inc', 'node', 'node.pages');
$type = 'my_new_node_type';
$node = node_load($nid);
if ($node) {
unset ($node->nid);
unset ($node->status);
unset ($node->created);
unset ($node->changed);
unset ($node->vid);
$node->type = $type;
$types = node_get_types();
drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)));
return drupal_get_form('offre_node_form', $node);
}
?>
I hope this might help someone!
Fred
I used Fred's solution and it
I used Fred's solution and it wroked for me in drupal 6.x. I did:
// Include the node pages file
module_load_include('inc', 'node', 'node.pages');
// Get the form id
$form_id = 'page_node_form';
// Get and prepare the node
$node = node_load($nid);
node_object_prepare($node); // Invoke hook_nodapi and hook_node
if ($node) {
$node->type = 'page';
return drupal_get_form('page_node_form', $node);
}
same issue
i concur.. passing a populated $node object doesn't seem to do much.