my problem is that i don't seem to get this working: i'm trying to have a noreference field with a default value wich is extracted from an argument in the url. i have a content type (lets say "story") which has a nodereference field in it. the url for adding a node of this type is: "node/add/story". but i'd like to be able to refer to that url passing one more argument (a node id in my case), like this: "node/add/story/5".
The argument would then be retrieved and used to get the node to refer to. this, i think, should be done from the default value with php in the nodereference field settings.
This is the snippet i've working on (please be gentle since my php might not be very good):
// retrieve the nid from the url argument
$refnid = arg(3);
if (isset($refnid)){
// make the query to get the title from the nid passed in the argument
$result = db_query("SELECT title, nid FROM {node} WHERE nid = %d", $refnid);
$data = db_fetch_object($result);
// return the array containing the queried node
return array(
0 => array($refnid => $data->title),
);
}
well, this is actually not working, i don't get any error or sintax warning of any type, so i'm lost, any suggestion? or at least someone tell me if what i'm trying to do is achiavable or not. thanks in advance
Comments
Comment #1
leanazulyoro commentedi've tried something simplier that the above code and discovered that even doing the simplest way awound i can't get this working.
this is the help text for the "php default value":
Advanced Usage Only: PHP code that returns a default value. Should not include
delimiters. If this field is filled out, the value returned by this code will override any value specified above. Expected format :
array(
0 => array('nid' => value for nid),
// You'll usually want to stop here. Provide more values
// if you want your 'default value' to be multi-valued :
1 => array('nid' => value for nid),
2 => ...
);
so, am i supposed to replase the 'nid' for the actual id of the node i'm tring to reference? and the 'value for nid' for it's title?
or
am i supposed to leave 'nid' as it is and replace 'value for nid' for the actual id of the node i'm tring to reference?
anyway i tried:
return array(0 => array('nid' => 5),); // not workingreturn array(0 => array(5 => 'title of the node'),); // not workingComment #2
yched commentedIts
" 'nid' as it is and replace 'value for nid' for the actual id of the node i'm tring to reference? "
Which means
should work...
Be sure to not actually include the < ?php ... ? > delimiters
Comment #3
leanazulyoro commentedYes, thanks, that is working now (don't know why it didn't work before, perhaps a little sintax error somewhre). but now to what's the real deal about this post: can i retrieve an argument from the url (which would be a nid) and use it to select the referenced node?
this is what i do:
$refnid = arg(3); //i get the argument from the url: "node/add/story/5"
return array( 0 => array('nid' => $refnid),);
this should put the nid passed in the url in the variable used to select the referenced node, but i get:
This post can't be referenced.
The default value php code created Array ( [0] => Array ( [nid] => story ) ) which is invalid.
the thing is that this message apears when tring to update the field, and takes the argument of the url during that process (which is "admin/content/types/story/fields/field_fieldname"), so it retrives the arg(3), in this case is "story" and takes it as an invalid value and does not update the field. what can i do?
Comment #4
drenton commentedI did this for the default value :
return array(0 => array('nid' => $_GET['storynumber']));and then called the add like this :
node/add/story/?storynumber=5
Works for us, although I'm not sure this is the proper way to do it.
Comment #5
leanazulyoro commentedok, i'll give it a try, although as you said i don't think it is the "drupal" way of doing this, i believe it should involve the arg() function for the url to mantain a clean look, but those are details, if it works this way is enough for me at the moment, many thanks
Comment #6
lucidD commentedi am also trying to figure out how to get the default value to show up properly. what i have is a view that lists several nodes based on taxonomy. each row has a node being displayed and also has a node reference cck field. i would like to make the default value for the node reference to be itself for each node - i.e. the default node reference to nid 300 is 300. how can i do this? is there somewhere that i might find what format to place the default values in? thanks.
Comment #7
lucidD commentedbump
Comment #8
drenton commentedI'm not sure how you would do that, since when you create a new node the nid is still blank at this time. You probably need to use hook_nodeapi when the $op is equal to insert. For example :
function your_module_nodeapi(&$node, $op) {
if ( $node->type == 'your_node_type' && $op == 'insert') {
$node->field_your_field[0]['nid'] = $node->nid;
}
}
Comment #9
grobemo commentedHere's one way that works. Include the following code in the default PHP value for your nodereference field:
One problem with this code: It doesn't check whether the node is of the right type, which can create problems. Anyone know an easy way to figure out which node types are referenceable for the field that's being filled?
Here's how this code works. menu_get_item() returns an array with lots of information about the page you're calling. Of particular interest is the element 'page_arguments', which is an array containing each part of the path. For instance, here's the output I get from menu_get_item() when I go to /node/add/degree/4:
Anyone have a simpler way?
Comment #10
eoneillPPH commentedInstead, try this:
That "else" handles null values (works if you're doing a db call and might have no result, too, or any other such case). I found that further down the comments here.
Comment #11
vivianspencer commentedAlthough I prefer the various methods mentioned above there's actually a module which provides this functionality: prepopulate
Comment #12
clockwood commentedI could really use some help with picking the nodeprofile reference variable to use to set the Default value. I'm using the code posted by eoneillPPH above:
If I hardcode $refnid to an actual profilenode id (ie, 101), it works just fine, however I just cant find the right variable/value to use to programmatically grab the user's profilenode id. I've tried just about every variable i can find, but it just isn't giving me the referenced node id.
Any suggestions?
Comment #13
clockwood commentedOk, after many hours of guesswork and then finally a little thought, I came up with a working solution for my problem:
What it does is load the current logged in user's nodeprofile (based on their uid), which then gives me access to the nid of their profile.
It feels hacky, but it works.
Comment #14
artatac commentedI have a node type called property and at the bottom of the node is an add event link this has a node_referrer field that allows a user to choose the property node linked to that event. What I would really like to do is AUTOMATICALLY populate the events property_node_referrer field based on the property node we have just come from.
Comment #15
yched commented@clockwood: doesn't seem hacky to me...
@artatac: this is not related to this thread - you might want to look at http://drupal.org/project/prepopulate
I think this issue is fixed now ?
Comment #17
ionmedia commentedwhy it is not work ?
Comment #18
clashar commentedsubscribe