How do you programmatically retrieve and display the data entry form for a CCK Type from a module in Drupal 6?

I have two CCK Types (for now) with a parent-child relationship between them.

I need a page that displays the data entry forms for both types.

When the user goes to the data entry page, the parent type entry form should display until the user hits the submit button. Then the data entry form for the child type should display under it to allow the user to enter a detail record. All fields for both types need to remain editable at all times.

I was thinking that I could just do something simple like call drupal_get_form($form_id) for the CCK Data Entry Form. But that just returns an empty form (no input fields).

So the question breaks down to what is the correct method to get the form definition for a CCK content type?

Eventually, this entire form is going to be very heavily AJAX based with every field automatically updating the server as the user tabs out of the fields and the forms will be spread over a series of tabs that the user can jump back and forth between. There will also be at least 4 different CCK Content Types that get populated.

For now, I'm just trying to figure out how to display a form and will worry about the rest after that. :-)

Thanks,
Shawn.

Comments

shawn_smiley’s picture

Here is the code I'm using to retrieve and display a form:

function vsdataentrytest_dataentryform() {
  $form_id = 'vs_trip_node_form';
  $page_html = drupal_get_form($form_id);
  
  return $page_html;
}

The method vsdataentrytest_dataentryform() is called in a menu hook for my module.

I got the form_id by adding a hook_form_alter() method to my module that prints out the form_id of all forms on the page and then went to the create content form for my CCK Content Type.

The above code does work if I reference a non-CCK form such as the site search form. Just not with CCK forms.

Thanks,

Shawn

mattyoung’s picture

By looking at node_add(), I see

$output = drupal_get_form($type .'_node_form', $node);

It looks like the content create form id is content-type_node_form, so try just append _node_form to your cck type should give you the form id?

You can also get the form id from looking at the Page Source, look for

<input type="hidden" name="form_id" id="edit-YOUR FORM ID HERE" value="YOUR FORM ID HERE" />

shawn_smiley’s picture

Thanks for the reply matt.

I was already using the '_node_form' suffix on my CCK type name, but I also just tried with passing in a node reference as well. But it still doesn't work.

Here is my updated code:

function vsdataentrytest_dataentryform() {
  $node = new StdClass();
  //$node->type = '';
  //$node->type = '';
  //$node->type = 'vs_site';
  $node->type = 'page';
    
  //$form_id = 'search_theme_form'; // Works (site search form)
  //$form_id = 'user_register'; // Works (user registration form)
  //$form_id = 'vs_site_node_form'; // Does not work (custom CCK type)
  $form_id = 'page_node_form'; // Does not work (built-in page type)
  
  $page_html = drupal_get_form($form_id, $node);
  
  return $page_html;
}

When I reference one of the CCK Types (either my custom type of the included 'page' type) using this approach I also receive the following error message:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'node_form' was given in E:\projects\vitalsigns\DrupalTesting\includes\form.inc on line 366.

Any other suggestions?

In looking through the code for drupal_get_form() in CVS, it seems to me like this should work.

Thanks,

Shawn

rkarwa’s picture

I'm trying to do more or less the same thing. I also have multiple CCK types that I'm trying to display on a single data entry page using a custom module. Each type would have a separate submit button. My CCK types do not have any sort of relationship between each other.

Eventually I want the data entry page to also display graphs of existing data right next to the data entry form.

I've tried the same mechanism that you've mentioned (drupal_get_form) and am getting the same 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/rkarwa/Sites/drupal/includes/form.inc on line 366.

I am considering moving away from CCK and defining my own node types and forms in a custom module to solve this problem.

I would appreciate any insight.

Thank you,
Raj

rkarwa’s picture

After searching around a bit.. I found a comment on another article that helped me out: Thanks Lukas!. For anyone else that might need some help on how to merge multiple cck add node forms on a single page, heres the basic code that I've added to my custom module's .module file:

.
.
.
/**
 * Call back from the menu
 */
function mymodule_view() {
    module_load_include('inc', 'node', 'node.pages');
    
    // Add 1st add node form
    $first_node = new stdClass();
    $first_node->type = 'first_content_name';
    $output = drupal_get_form('first_content_name_node_form', $first_node);
    
    // Append 2nd add node form
    $second_node = new stdClass();
    $second_node->type = 'second_content_name';
    $output .= drupal_get_form('second_content_name_node_form', $second_node);

    return $output;
}
.
.
.
sunil-kumar’s picture

Thanks for help, it is working fine....

lesauf’s picture

Thank you.