By Hunabku on
function yourmodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'your_form':
$form['#after_build'][] = 'yourmodule_after_build';
break;
}
}
/**
* Where arg(3) is the parent node id passed from an add node link
*/
function yourmodule_after_build($form, &$form_state) {
$form['field_yournoderef']['nid']['nid']['#value'] = arg(3);
$form['field_yournoderef']['#access'] = false;
return $form;
}
Comments
. . . even cleaner . . .
Creating and rendering parent-child relationships, in an obvious and direct way, is NOT one of Drupal's strengths. I can't speak to using modules, as i refuse to use a module for something so elemental.
In Drupal 6 with CCK, form_alter comes in too early in the process and CCK trounces it. Since i like having my form altering code consolidated, i have placed all of it in after_build.
So here is a cleaner version of the above code:
thanks, this worked as
thanks, this worked as advertised!
Doesn't work for me :(
Doesn't work for me (D6).
Nothing get's set for the node reference field. Maybe it's my multistep.
_
Why not just set the field to 'hidden' on the 'display fields' tab?
thanks for that WorldFallz -
thanks for that WorldFallz - never knew about that one. Think i'll mostly stick to hiding fields with form altering, as i like the flexibility and having as much form tweaking in one spot as possible.
_
no worries-- i just thought i was missing something, lol.
I'm dealing with some similar
I'm dealing with some similar issue here. I don't care how to hide the field. My concern is about populating it.
I'm using panels and I want to add a "add child" form below the node being viewed using panels.
what would be the cleanies way to populate and hide the node_reference field?
I've tried a module with only
I've tried a module with only this:
$ref_nid = arg(count(arg())-1); // in admin, the first arg will be admin
$form['title']['#value'] = $ref_nid;
$form['field_ref']['nid']['nid']['#value'] = $ref_nid;
I had the node add form as a pane and I thought that might be my problem. But I've tried adding a node with the regular node/add/mynodetype and what's happening is that in the form, I can see the values filled in, both in title and in the dropdown for the node reference field, but when I submit the form, it creates a new node WITH A BLANK TITLE AND FIELD_REF.
I can't even delete the node!
'#value' probably should be
'#value' probably should be '#default_value'
I've just tried. This sounds
I've just tried. This sounds reasonable but doesn't do anything. At least not on after_build. I'll try before or something but I have to check the docs. Any advice?
I also wanted to add that this snipped of code will also alter the form when the node is being edited and will clear the fields after modifying the record.
I alter forms directly in
I alter forms directly in hook_form_alter() and not after_build and that works for me.
Using default_value in
Using default_value in hook_form_alter() worked fine for the title but not for CCK fields.
Is there any hook for CCK fields?
OMG
Apologies to everyone.
It was way simpler than that!
You can just use the default value php code and content permissions to disallow the edition. Once again we complicate ourselves way more than necessary.
This is the code i'm using.
$shift = arg(0) == 'admin' ? 1 : 0;
if (arg(0 + $shift) == 'node' && is_numeric(arg(1 + $shift))) {
$out= arg(1 + $shift);
}
elseif (arg(1 + $shift) == 'add' && is_numeric(arg(3 + $shift))) {
$out= arg(3 + $shift);
}
else {
$out= '0';
}
return array(
0 => array('nid' => $out),
);
Most form altering can take
Most form altering can take place in form alter - however cck and some occasionally other modules form elements need to be delt with in the after_build. In that case it is best to handle them all with conditional statements in the after_build as mentioned above.
subscribe
subscribe
My way of doing this :
My way of doing this :
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com