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

mooffie’s picture

print drupal_get_form('item_node_form');

Change it to:

$new_item_node = array('type' => 'item');
print drupal_get_form('item_node_form', $new_item_node);

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.

rdgouw’s picture

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:

$new_item_node = array('type' => 'item', 'field_part_of' => $node->nid);

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:

$new_item_node->field_node_referentie['nid'] = $node->nid;
//and this:
$new_item_node->field_node_referentie[0]['nid'] = $node->nid;

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?

mooffie’s picture

I was also thinking about automatically assigning the current node id to the nodereferencefield
[...]
The nodereference field that I use has the title "Part of" and Drupal creates a "field_part_of".

Do:

$new_item_node = array('type' => 'item');

$new_item_node['field_part_of'][0]['nid'] = $node->nid;

print drupal_get_form('item_node_form', $new_item_node);

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.

$new_item_node->field_node_referentie[0]['nid'] = $node->nid;

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.)

rdgouw’s picture

Hi Mooffie,

Thanks, this works! I should work on my PHP knowledge (which is almost zero now)... but I'm learning!

rdgouw’s picture

Ok, now I have this node reference working in my module:

$form['field_node_referentie']['nids']['#default_value'][0] = arg(1);

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) :

$form['field_node_referentie']['#type'] = 'hidden';
$form['field_node_referentie']['nids']['#value'][0] = arg(1);

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)

mooffie’s picture

Ok, now I have this node reference working in my module:

$form[ ....

First, where does $form come from? out of the blue?

(I could give you an answer outright, but that won't be instructive.)

(I also created a separate post with this question

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.)

bestknight’s picture

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...

rdgouw’s picture

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:

$form['field_node_referentie']['#type'] = 'hidden';
$form['field_node_referentie']['nids']['#value'][0] = arg(1);

And an error was shown: "This post can't be referenced."

but it should be this:

$form['field_node_referentie']['nids']['#type'] = 'hidden';
$form['field_node_referentie']['nids']['#value'] = arg(1);

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)

mooffie’s picture

but it should be this:
$form['field_node_referentie']['nids']['#type'] = 'hidden';
This solved the issue.

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.

hlopes’s picture

This also works perfectly on Drupal 7.

And it fixed my migraine along the way...

bestknight’s picture

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:

function phptemplate_ccknodetype_node_form($form) {
  $output = '';
  
  global $user;
  if (in_array('contententry',$user->roles)) {
      $form['field_ccknodetype_fk_ccknodetype2']['nids']['#attributes']['disabled']= 'disabled';
  }
  else {
  '';	  
  }
  
  $output .= drupal_render($form);
  return $output;
}

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.

rdgouw’s picture

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

bestknight’s picture

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

Jerimee’s picture

However the solution at http://drupal.org/node/261495#comment-1616098 DID work.

Here is the code:

<?php
$new_blognode = new stdClass();
$new_blognode->type = 'blog';
module_load_include('inc', 'node', 'node.pages');
$output .= drupal_get_form('blog_node_form', $new_blognode);
?>
roCKeshwar’s picture

above code also help me out

sandboxpl’s picture

Everything is fine, but i've noticed one detail on i18n page - form requires language:

Notice: Undefined property: stdClass::$language in node_form() (line 302 in /modules/node/node.pages.inc).
Notice: Undefined property: stdClass::$language in pathauto_form_node_form_alter() (line 464 in /sites/all/modules/pathauto/pathauto.module).

So i've added user language (in my case user's language was needed):

global $user;
$new_blognode->language = $user->language;

and it works again ;)

ndmaque’s picture

i was trying

$node = new stdClass() ;
drupal_get_form('test_node_form', $node)

doh ... all i had to do was set $node->type = 'test_type'

my hair is back in now thanks