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
Figured it out! Of course,
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"):
It's all in the node.pages file apparently.
Richard
Thanks alot for sharing the
Thanks alot for sharing the solution, Richard! I`ve struggled on this issue for about an hour or so, before finding your post.
What's the $node_form
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())
Here $node_form is your node object
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 :)
Thanks )
Thanks , this helped me out a lot !!!!
Thanks
Thank you very much. It helped a lot!
-- Pratip Ghosh
Thank you :)
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 :)
Works for me :)
Thanks it Works for me :)
My form is empty
I call the same lines of code in D7, but get an empty form:
my DPM call produces this:
Where are my fields?
There should be three: Title, Body, and PIN
thanks.
you probably have to load
you probably have to load another module
I think, you have wrong
I think, you have wrong form_id. It should be inspection_signoff_node_form, not inspection-signoff-node-form.
Creating node form with
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?
Same issue - solved
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
Focusing on Business Applications but heck we do anything Drupal
Help
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
re: Help
@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.
A working example
Just a working example summarizing all comments above:
Here is the code to display the Edit Form in Node View page:
Notes:
$vars['node']
to prevent probable problems caused by the fact that$vars
is passed by reference; though it might not be necessary.<?phpmodule_load_include('inc
The form will be contained in $node_form.
That's weird
That's weird, @jaypan solves means for me but it didn't work
however the other purpose
work for me
This works for me
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