I want to retrieve the array version of a node form using drupal_retrieve_form. I wrote this quick test function to see if it was going to work:

global $user;
$node = array('type' => 'page', 'uid' = $user->uid, 'name' => $user->name);
$form = drupal_retrieve_form('page_node_form', $node);
print_r($form);

When I look at the contents of the array that is displayed, the elements from the generic node are in the array but none of the additional cck fields and taxonomy form elements are there. I assume the nodapi is not getting the additional form fields from hook_form. I have been trying to trace the node form building process but have been getting a bit lost. I am thinking that I might need more/different information in the $node argument but I don't know what that might be.

Is there an argument that I should be passing drupal_retrieve_form that I am missing?

Is this not what drupal_retrieve_form is for? Should I use another function?

Ultimately, I want to create a admin UI that allows the user to map a data import format to fields in a selected node form (and ulimately use drupal_execute to run the import). So I need the form array from a selected node type to build the mapping form.

Comments

In case anyone ever stumbles

In case anyone ever stumbles upon this looking for the answer, before the formapi gets changed; this seems like it is going to work:

global $user;
$node = array('type' => 'page', 'uid' = $user->uid, 'name' => $user->name);
$form = drupal_retrieve_form('page_node_form', $node);
$completeform = drupal_prepare_form('page_node_form', $form);
print_r($completeform);

Everything from form_alter was missing. Calling drupal_prepare_form adds that all in. I was thinking it was stuff from hook_form, then I started looking at node_api 'prepare' before realizing, of course, those form fields are being added by form_alter...

strange. Not working for me

strange. Not working for me now. (drupal 5.2)
If I put

$form = drupal_prepare_form('book_node_form', $form);<code> $form is empty, and if I use just
<code>drupal_retrieve_form('book_node_form', $node);

some elements of form are generated twice and form automaticly "submits" on page load.

check parameters for drupal_retrieve_form

You are passing wrong arguments to drupal_retrieve_form, instead of $node pass $form and you will be good=)

--
kindest regards
Maciej Perlinski
http://www.meant4.com
maciej.perlinski@meant4.com

--
kindest regards
Maciej Perlinski
http://www.meant4.com
maciej.perlinski@meant4.com

The following code works for

The following code successfully creates the form for me and removes form errors (unable to reference node and invalid date), mentioned in a comment further down the page.

$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => 'page');
$form = drupal_retrieve_form('page_node_form', $node);
drupal_prepare_form('page_node_form', $form);
return drupal_render_form('page_node_form', $form);

I figure that my errors were generated by running drupal_get_form() on a $form array that had already been processed.

HOWEVER
When it came to submitting this form it simply did not work. I wanted this method so that I could manipulate the $form while it is an array. In the end I settled on creating my own version of the drupal_get_form function without the drupal_render_form() at the end of it. Like this

my_module_drupal_get_form($form_id) {

...content of drupal_get_form() with return $form instead of return drupal_render_form($args[0], $form);

}

And to use it I called this:
$form = my_module_drupal_get_form('page_node_form', $node);

then I can manipulate the $form array before ending with this:

return drupal_render_form('page_node_form', $form);

Passing Phase Web Development

CCK form array. How to load it?

Hello, thanks for the code but it didn't work for me. I wrote a form in a custom module and I wanted to combine it with a CCK form. I wanted to show my custom form first, and then, show the CCK form by retrieving the CCK form array.

I used the following functions:

<?php

$type
= 'custom_cck_type';
$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
$form = drupal_retrieve_form($type .'_node_form', $node);

?>

I was only getting the generic node fields (title, authoring information, publishing options) as you mentioned in your original post.

I wonder how can you retrive the complete form? How can you load all the fields for a CCK form?

I looked up node_add($type = NULL) and it seems that when you use <?php drupal_set_title(t('Submit @name', array('@name' => $types[$type]->name))); ?> the complete form loads. But this doesn't seem to be very clear.

Can someone provide advice on this?

This might help

Edit: I'm working in Drupal 5.7

I'm working on a similar problem right now. I want to add additional (and multiple) node types to other content similar to comments. Adding links, tidbits of knowledge and other info to the 'place' is what I'm after.

For an example I have a CCK content type called 'place' and I've added another CCK content type called 'fact' and another called 'link'. I'd like forms added to the 'place' for 'link's and 'fact's just like a comment entry form.

I was pulling my hair out trying to use drupal_get_form() to output those add forms to show up. I'm a PHP developer but fairly new to Drupal and I have a lot of problems with the documentation. drupal_get_form() doesn't say anything about needing that extra $vars parameter, it only asks for '$form_id' (which to me sounds like something other than calling the form 'type'). After reading a few other threads I got to this one and found what I needed. Thanks to the examples I just got the form display to work.

I call the following code at the bottom of my 'place' template file (node-place.tpl.php) and it loads the add/fact and add/link forms perfectly.

<?php
$myform
= '';
$type = 'fact';
$vars = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
$myform = drupal_get_form($type .'_node_form', $vars);
print(
$myform);

$type = 'link';
$vars = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
$myform = drupal_get_form($type .'_node_form', $vars);
print(
$myform);
?>

I still don't have a full grasp on this and only lucked up on getting it to work. Theming these forms by making them collapsible blocks etc. will be my next choir. The final part will be getting these added items to associate to the correct node by adding a reference field and auto populating it with the correct node id so the user never sees a popup. Afterwards I will hopefully be able to generate a block for facts, links and other data associated with each place.

While slightly OT I had a

While slightly OT I had a problem with drupal_retrieve_form() and drupal_prepare_form() in that widgets on the form (nodereference and AJAX date field) came up with 'cannot reference' and 'invalid date' respectively. I did a print_r of this form and the content type form at node/add/content-type to compare them and they were exactly the same. Weird. So anyway I tried drupal_get_form() and it works with no errors.

Also note above that drupal_prepare_form() does not return any value so $form = drupal_prepare_form() will result in an empty variable $form.

Passing Phase Web Development

loading CCK & other form elements

<?php
$type
= "page"; // use any content type

$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);

$form = drupal_retrieve_form($type .'_node_form', $node);

drupal_prepare_form($type .'_node_form', $form); // does not returns anything - modifies $form object

// print_r($form); // you can check all fields (including CCK ones )in $form object

drupal_render_form($type .'_node_form',$form)
?>

This should help.!
Vinay Yadav
PHP / Drupal Developer
http://www.vinayras.com

www.eDrupal.com - Indian Drupal Community

Vinay Yadav
PHP / Drupal Developer
http://www.vinayras.com

what I had to do to get this to work

Posting this in case it helps someone out. I needed to display a CCK input form in a block. My form contains date fields using the javascript popup calendar. The popup calendar wasn't working in the block but did work on the node/add page.

In order to get it working in the block, it turned out that the appropriate javascript files weren't included in my page. I added these lines to my head section and it worked:

<!-- added for datepicker -->
<script type="text/javascript" src="/sites/all/modules/date/date/date_popup/lib/ui.datepicker.js?"></script>
<link type="text/css" rel="stylesheet" media="all" href="/sites/all/modules/date/date/date.css?" />
<link type="text/css" rel="stylesheet" media="all" href="/sites/all/modules/date/date/date_popup/themes/datepicker.css?" />
<link- type="text/css" rel="stylesheet" media="all" href="/sites/all/modules/date/date/date_popup/themes/timeentry.css?" />
<link type="text/css" rel="stylesheet" media="all" href="/sites/all/modules/views/css/views.css?" />

I'm using this code to display my form:

               global $user;
                $type= 'reserveform';
                $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
                /* */
                $form=drupal_get_form($type .'_node_form', $node);
                print $form;

It also works if I use the drupal_prepare_form method referenced above. Hope it is useful for someone.

Progress

I managed to solve the issue of the warning: 'warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'node_form' was given...'. You need to include the following line:

require("modules/node/node.pages.inc");

Presumably that file isn't always included, and the callback function points to that.

Here's the code that works for me:

$type = 'page'; //Or whatever content type

require("modules/node/node.pages.inc");
  global $user;
 
  $form_id = $type.'_node_form';
  $form_state = array();
  $node = array('type' => $type, 'uid' => $user->uid, 'name' => $user->name);
  $form = drupal_retrieve_form($form_id, $form_state, $node);
  drupal_prepare_form($form_id, $form, $form_state);

Having said that, drupal_render_form($form_id, $form); is only returning part of the form. I wonder if this is because another file is missing an include? (CCK?)

my code (drupal 5)

I found this thread helpful, if only partially... I don't have the energy to fully document what worked and didn't work for me, but I thought I would post the code that I finally got to work for my particular site:

<?php
   
global $user;
   
$type = 'mystory'; // my content type
   
$data = array(
       
'uid' => $user->uid,
       
'name' => $user->name,
       
'type' => $type,
    );
   
$form = drupal_retrieve_form($type .'_node_form', $data);
   
drupal_prepare_form($type .'_node_form', $form); //Let all form hooks run - let drupal get all the form elements defined in the content type
   
$form['field_first_name']['0']['value']['#value'] = 'Henry'; //preset "Henry" as the first name
   
$form['#action'] = '/node/add/'.$type//for some reason handling the form this way makes drupal forget what to do with it - remind drupal to actually create the node
    //print_r($form); // you can check all fields (including CCK ones) in $form object
   
print drupal_render_form($type .'_node_form',$form);
?>

http://drupal.org/node/671692#comment-2436538

My issue is/was that I want to modify forms in page-node-#.tpl.php files - the main obstacle/resolution was that I needed to make explicit the $form['#action'] upon form submission.

I suspect that what I've done is somewhat nonstandard; hopefully it works...