Hi, I have a nodeformblock that uses a nodereference field,
by using node reference url widget I can pass an argument to the node/add/ page,
but it doesnot seem to work via panels,
any help

regard sduncan

CommentFileSizeAuthor
#7 nr_empty.png15.57 KBranavaibhav
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mikey_p’s picture

There really isn't any way to do this, as blocks don't accept arguments. Perhaps you could use a hook_form_alter to achieve the result you are looking for?

mikey_p’s picture

Status: Active » Postponed (maintainer needs more info)
pnigro’s picture

I accomplished populating a node reference using hook_form_alter in a custom module. The tricky part is that cck fields are accessible in after_build as opposed to hook_form_alter. I believe this is because the cck fields are added after the form is built. I created the following module using the following references:

Populate CCK Node Reference Field and hide it - is this the cleanest way
hook_form_alter() and CCK fields
FAPI/CCK Confusion: 'value' vs '#value'

/** hook_form_alter **/

function mymodulename_form_alter(&$form, $form_state, $form_id) {
  // Check for a particular content type's node form. Form name is [content_type_name]_node_form
  if ($form_id == 'content_type_name_node_form') {
    // Add an after_build function to process when everything's complete.
    $form['#after_build'][] = 'mymodulename_after_build';
  }
}

/**
* Populate node reference field
*/
function mymodulename_after_build($form, &$form_state) {
  // Obtain node object from nid in url
  $node_reference_field_name = node_load(arg(1));
  //populate autocomplete node reference field with node title. Name of CCK field is field_[cck_field_name].
  $form['field_cck_field_name'][0]['nid']['nid']['#value'] = $node_reference_field_name->title; 

return $form;
}

I hope this helps someone.

ryanilg’s picture

And... you just saved me a bunch of work. I was planning on doing this exact same thing on a project I am currently working on.

Thanks!

pnigro’s picture

You're welcome. Glad I could help :-)

ranavaibhav’s picture

Hello Paul,
Thanks for the code. I created a custom module with your code and enabled it. It works however the title is printed just below the Node Reference field instead of inside the filed. Am i doing anything wrong here? Please guide. Thanks.

ranavaibhav’s picture

FileSize
15.57 KB

sorry, the screenshot for the issue i mentioned in my last comment.

pnigro’s picture

Hello Jackie,
I had the same problem when I was creating the module. I think the problem is that $form['field_cck_field_name'][0]['nid']['nid']['#value'] or [0]['nid']['nid']['#value'] is not correct for your setup. If you install the Devel module you can find out what this form field should be by typing dsm($form); in the #after_build stage. This will show you the node object in a tree structure. Click the node reference field and look for #value. There may be several so you will have to do some trial and error to find the correct one. Remember that each level you go down you have to add it to the end of $form['field_cck_field_name']If I were you I would first try $form['field_cck_field_name'][0]['nid']['#value'] I hope this helps.

ranavaibhav’s picture

Hello Paul,
Thank you for your quick response.
I found that my setup did not like [0] in the code. I guess it's because i have setup field for single value only.
After changing
$form['field_cck_field_name'][0]['nid']['nid']['#value']
to
$form['field_cck_field_name']['nid']['nid']['#value']
it start working..

Thank you so much for your help. I literally spend so many days figuring out how to generate the field value and came across your post..

pnigro’s picture

You are very welcome. I am glad I could contribute to the Drupal Community.

konordo’s picture

Thanks for the code, very helpful.

This is what worked for me, for a user reference field. Form is displayed in a tab, in user/* pages:

<?php
function my_module_after_build($form, &$form_state) {

  //Obtain User object from the argument. user/uid
  $thisUser = user_load(arg(1));

 //populate select list user reference field with user uid. Name of CCK field is field_[cck_field_name].
  $form['field_[cck_field_name]']['uid']['uid']['#value'] = $thisUser->uid;

return $form;
}
?>
itserich’s picture

Thanks for this.

I have forms in a node panel and want to relate the form to the original node.

Node Reference URL Widget also appears to offer a solution.

http://drupal.org/node/1008212

smartsystems160’s picture

For the benefit of those using Entity Reference module, this module Entity Reference Prepopulate Token will do this for you on panels.

mikey_p’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Closed (outdated)

I haven't seen much interest in this issue in awhile, and it's filed against 6.x which is unsupported, so I'm closing this issue for now.