Posted by Duncan.Slade on June 17, 2009 at 10:52am
| Project: | Form Block |
| Version: | 6.x-1.x-dev |
| Component: | Miscellaneous |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | postponed (maintainer needs more info) |
Issue Summary
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
Comments
#1
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?
#2
#3
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'
<?php
/** 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.
#4
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!
#5
You're welcome. Glad I could help :-)
#6
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.
#7
sorry, the screenshot for the issue i mentioned in my last comment.
#8
Hello Jackie,
I had the same problem when I was creating the module. I think the problem is that
<?php$form['field_cck_field_name'][0]['nid']['nid']['#value']
?>
<?php[0]['nid']['nid']['#value']
?>
<?php$form['field_cck_field_name']
?>
<?php$form['field_cck_field_name'][0]['nid']['#value']
?>
#9
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..
#10
You are very welcome. I am glad I could contribute to the Drupal Community.
#11
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;
}
?>
#12
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