Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am wracking my brain to try to figure out how to retrieve a custom content type's edit form programatically.

For example, I created a content type called "address", and have this:

$form = drupal_get_form("address_node_form");

But it does not work. I get back this warning:

Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'node_form' was given in drupal_retrieve_form() (line 771 of /home/richardp/public_html/drupal7/d7/includes/form.inc).

I know that in D6 you also had to include an object where you specify the type, but I have tried this and it still doesn't work.

When I try simply $x = node_add("page"); I get a white screen of death, presumably because I ran into a PHP error.

Am I supposed to be including a file first or something like that?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Here is the code to display the Edit Form in Node View page:

<?php
// modules/custom/custom.module

function custom_preprocess_node(&$vars) {
    module_load_include('inc', 'node', 'node.pages');
    $node = $vars['node'];
    $form = drupal_get_form('mytype_node_form', $node);
    $vars['content']['edit_form'] = $form;
}
?>

Notes:

  1. You need to replace custom with your module's name and mytype with the machine-readable name of your content-type.
  2. I just used a clone of $vars['node'] to prevent probable problems caused by the fact that $vars is passed by reference; though it might not be necessary.
share|improve this answer
1  
Good answer...the clone is unnecessary though –  Clive Jul 4 '13 at 10:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.