Hi,
How can I use drupal_get_form for retrieving a custom content type? Somehow I don't succeed in that.
What I'd like to do is this:
I created two content types: "document" and "item". The idea is to add general fields to the document type and specific fields to one or more items. By merging document and items together in a view a complete document is created.
The merging is done in a node-document.tpl.php file by using this code:
// load the context-node's 'metadata'
global $current_view;
// * define the context-node's NID as the argument
$current_view->args[0]=$node->nid;
// * select the name of the view to embed as $view1
$view1 = views_get_view('Items');
// * define this section for CSS
print '<div class="Items">';
// * display a subtitle for the view section
//print '<h2>' . t($node->title . ' Items') . '</h2>';
// send $args to the View's Argument Handler and display $view1 in the context-node
print (views_build_view('embed', $view1, $current_view->args, false, false));
print '</div>';
Now I'd like to add new Items directly in the view by trying to pull in a new item form, so I added this line to the tpl.php file:
print drupal_get_form('item_node_form');
This doesn't work:
warning: Missing argument 1 for node_form() in C:\xampp\htdocs\drupal\modules\node\node.module on line 2025.
This however:
print drupal_get_form('user_login_block');
DOES work.
I tried several different form_id's but somehow I keep on getting the error message. Am I using a wrong form_id? Or isn't this possible at all? Hope someone can give me a little help.
Rick
Comments
...
Change it to:
You probably have some select box in 'item', perhaps a nodereference, which links it with its parent document. You can make life easier for the user by populating it in $new_item_node before handing it to drupal_get_form.
Thanks, this works
Hi Mooffie,
Thanks for yor reply. This is working now!
You're right, I'm using a selectbox for a nodereference in Item. Actually I allow a multiple select so that one item can be linked to different documents if needed.
I was also thinking about automatically assigning the current node id (of the document being viewed) to the nodereferencefield in the attached Item form. So I tried this:
No success again... When I submit the form Drupal is complaining that the "Part of" field is required (of course I didn't select one manually on purpose) The nodereference field that I use has the title "Part of" and Drupal creates a "field_part_of". I'm not sure what name to use for the nodereferencefield. It seems that I can only populate default fields like "title" and "body".
I also tried this:
In this case no complaints from Drupal, but the reference doesn't work either when displaying the douments-with-items view. It only succeeds when I manually select a reference in the dropdownlist in Item. This seems to bee simple, but I think I don't understand completely... The noderef isn't added to the MySQL table either. What's going wrong here?
...
Do:
I've just tried this code, it works. It doesn't matter whether your field is 'multiple' or single, and whether it's a selectbox or an autocomplete --the syntax for all is the same.
There are two bugs here: (1) the name of the field is 'field_part of', not 'field_node_referentie'; (2) $new_item_node is an array, not an object, so you shouldn't use the '->' syntax.
(If, and only if, all works for you, you may wish to hide the selectbox.)
Hi Mooffie, Thanks, this
Hi Mooffie,
Thanks, this works! I should work on my PHP knowledge (which is almost zero now)... but I'm learning!
Hiding nodereference field
Ok, now I have this node reference working in my module:
This works without problems. As the user doesn't need to select a node reference manually in this case, I'd like to hide the node reference field, just like you proposed. So I tried this (and several variations of it) :
A title field can be hidden this way, so why not a node reference field? But in this case an error is displayed: "This post can't be referenced."
Apparently my experiments aren't accepted...
How can I hide the node reference field? Probably I have to do this another way...
(I also created a separate post with this question, but because it started at this point I put it here too)
...
First, where does $form come from? out of the blue?
(I could give you an answer outright, but that won't be instructive.)
That's fine, but the friendly thing to do is to provide a little link in one of the posts to the other one. We don't want people to waste their time replying to an already answered question.)
Cross-referencing
See http://drupal.org/node/253315 for the separate post
Thanks for the input mooffie - gratefully awaiting some more help with the above node reference issue...
Sorry for the lack of info
Sorry for the lack of info and double post... This is some more background info (hope it's sufficient):
I added an Item form at the end of a view showing Documents and Items that are attached to the Document:
print drupal_get_form('item_node_form', $new_item_node);in a node-document.tpl.php file. That way I can add new items directly in the displayed view.Now in the 'mymodule' I created, I call mymodule_form_alter and if the form is an Item form I want to have it point to the currently shown Document (so it has to point to the current nid).
Because the nodereference is prefilled this way, the user shouldn't have to select a nodereference manually and the field can be hidden.
It appeared that I tried to assign values on the wrong level in the array:
First I did this:
And an error was shown: "This post can't be referenced."
but it should be this:
Now the nodereference field is hidden in the Item form and it points to the current nid of the displayed Document. This solved the issue. Hope this is helpful for bestknight too.
(This solves the same issue mentioned here: http://drupal.org/node/253315 too. Sorry for double posting)
...
Yep.
Note that you don't have to implement hook_form_alter() to modify $form. It's possible to break drupal_get_form() into its three components (retrieve/process/render), then you can touch $form directly.
This also works perfectly on
This also works perfectly on Drupal 7.
And it fixed my migraine along the way...
Similar situation - no solution yet
A similar case with the one faced by rdgouw above but I found no solution yet:
In the node/add/ccknodetype page I have a nodereference field to ccknodetype2 as a select box.
My goal is to dim out (disable) the cck node reference field for a particular role.
I have the following code in template.php:
The default value is shown in the dimmed control (select box) but on preview / submission the following error appears:
ccknodetype2: This post can't be referenced.
Please help me overcome this. Thank you.
Maybe you could try this
Hi,
I'm not a Drupal/PHP expert, but I think you could try to disable the field on the highest level. And I believe that the #attributes expects an array. So maybe it should be something like this:
$form['field_ccknodetype_fk_ccknodetype2']['#attributes'] = array('disabled' => 'disabled');
or maybe this:
$form['field_ccknodetype_fk_ccknodetype2']['#disabled'] = TRUE;
I'm not really sure...
Did you check this too? http://api.drupal.org/?q=api/file/developer/topics/forms_api_reference.html
A validation issue?
Thanks rdgouw, tried your suggestion but can't get it to work.
On searching further, it appears this is an issue with CCK.
Cross-referencing with http://drupal.org/node/114346#comment-844362
The above solution did NOT work for me
However the solution at http://drupal.org/node/261495#comment-1616098 DID work.
Here is the code:
thanks
above code also help me out
Works perfectly, but...
Everything is fine, but i've noticed one detail on i18n page - form requires language:
So i've added user language (in my case user's language was needed):
and it works again ;)
i was trying $node = new
i was trying
doh ... all i had to do was set $node->type = 'test_type'
my hair is back in now thanks