4

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?

1 Answer 1

5

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.
1
  • 1
    Good answer...the clone is unnecessary though
    – Clive
    Jul 4, 2013 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.