Just installed D7 and 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?

Any help would be greatly appreciated!
Richard

Comments

richardp’s picture

Figured it out!

Of course, after hours wasted, I figure it out like 5 minutes after posting. I did indeed have to have an include. In order to load a form for a custom content type, and display it on the page, you have to do this:

(my type is called "address"):

  module_load_include('inc', 'node', 'node.pages');    
  $form = drupal_get_form("address_node_form", $node_form);
  $rtn .= drupal_render($form);

It's all in the node.pages file apparently.

Richard

inductor’s picture

Thanks alot for sharing the solution, Richard! I`ve struggled on this issue for about an hour or so, before finding your post.

nicpeck’s picture

What's the $node_form variable?

EDIT: I think I just did the same thing as the OP and answered my own question just after asking it- It seems to work if $node_form is the node object (as in what you get when you call node_load())

kalidasan’s picture

Look at the below example codes :

module_load_include('inc', 'node', 'node.pages');
$node_form = new stdClass;
$node_form->type = 'yourContentType';
$node_form->language = LANGUAGE_NONE;
$form = drupal_get_form('yourContentType_node_form', $node_form);
return $form;

I think, it will give you the clear picture :)

PushaMD’s picture

Thanks , this helped me out a lot !!!!

pratip.ghosh’s picture

Thank you very much. It helped a lot!

-- Pratip Ghosh

cristi_b’s picture

I am new to Drupal , and I was afraid that i will encounter problems which will take a lot of time. But people like you make this job a lot more easier and fun . Thank you very much , this helped me very much :)

atul.bhosale’s picture

Thanks it Works for me :)

dmattie’s picture

I call the same lines of code in D7, but get an empty form:

module_load_include('inc', 'node', 'node.pages');   
$form = drupal_get_form("inspection-signoff-node-form");
$rtn  = drupal_render($form);
dpm($rtn);

my DPM call produces this:

<form action="/?q=node/17" method="post" id="inspection-signoff-node-form" accept-charset="UTF-8"><div><input type="hidden" name="form_build_id" value="form-wCvqp4VmWmTPBS1kKMKR6pgZkQmFa30i7lDD5egHC0Q" />
<input type="hidden" name="form_token" value="2b2qO8NOHh5S4gmFW1HCjfV9-ajr7y-J9DxW9oh2IiM" />
<input type="hidden" name="form_id" value="inspection-signoff-node-form" />
<fieldset class="collapsible collapsed form-wrapper" id="edit-captcha--2"><legend><span class="fieldset-legend">CAPTCHA: no challenge enabled</span></legend><div class="fieldset-wrapper"><a href="/?q=admin/config/people/captcha/captcha/captcha_point/inspection-signoff-node-form&amp;destination=node/17">Place a CAPTCHA here for untrusted users.</a></div></fieldset>
</div></form>

Where are my fields?
There should be three: Title, Body, and PIN

thanks.

AgaPe’s picture

you probably have to load another module

tomas.teicher’s picture

I think, you have wrong form_id. It should be inspection_signoff_node_form, not inspection-signoff-node-form.

tomas.teicher’s picture

Creating node form with drupal_get_form works for me untill I have custom field for uploading images. After I click on Upload, instead of loading images with ajax it throws me many many errors. (I use Drupal 7)

Have anybody similar experience?

blainelang’s picture

I had the same issue with a custom content type and needed to add this in a HOOK_form_{formtype}_alter

form_load_include($form_state, 'inc', 'node', 'node.pages');

Even though in the code to display the form, I had already done the include

function recast_add_response($node) {
  global $user;

  if( !function_exists("node_form")) {
    module_load_include('inc', 'node', 'node.pages');
  }

  if( !function_exists("field_collection_item_form_validate")) {
    module_load_include('inc', 'field_collection', 'field_collection.pages');
  }

  $node = new StdClass();
  $node->uid = $user->uid;
  $node->name = $user->name;
  $node->type = 'recast_request';
  $node->language = '';
  node_object_prepare($node);

  $form = drupal_get_form('recast_request_node_form', $node);

  $output = drupal_render($form);
  return $output;

}


function recast_form_recast_request_node_form_alter(&$form, &$form_state, $form_id) {

  form_load_include($form_state, 'inc', 'node', 'node.pages');

  .....

}

Focusing on Business Applications but heck we do anything Drupal

adamhoke’s picture

Hi Richard,

I am trying to do the same thing you did. but I don't see where you should add that solution code? what file should you place it in?

Thanks,

-Adam

richardp’s picture

@adam,

The code is meant to be placed in your custom module, on a particular page you are building which requires you to display the form. In my case, I was working on my module, Arrange Fields (http://drupal.org/project/arrange_fields), and needed to programatically load and display a form within my module.

pendashteh’s picture

Just a working example summarizing all comments above:

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

// modules/custom/custom.module

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

Notes:

  • You need to replace custom with your module's name and mytype with the machine-readable name of your content-type.
  • 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.
Jaypan’s picture

module_load_include('inc', 'node', 'node.pages');
$content_type = 'some_content_type';
$node_form = node_add($content_type);

The form will be contained in $node_form.

reign85’s picture

That's weird, @jaypan solves means for me but it didn't work

however the other purpose

$node_form = new stdClass;
$node_form->type = 'actualites';
$node_form->language = LANGUAGE_NONE;
return drupal_get_form('type_content_node_form',$node_form);

work for me

usmanjutt84’s picture

Thanks Jaypan, your lines works for me as charm.
I have wasted a lot of time to get content type form on a node. Thanks again