Paid affiliate advertisement
PHP snippet needed for Views argument - $100 bounty
Hello, I need a little peice of PHP code to paste into a Views argument validator.
The code basically needs to look at the first argument received (which is a NID), check it is valid (ie that it satisfies the node reference relationship that is required in the view- ie there needs to be a node referencing this NID). If so the View should display both the first argument and a second argument (which is an additional fixed NID).
If the first argument is invalid then the View should be empty. It should not display the second NID (even though this is a fixed value which does satisfy the node reference relationship).
I am not a PHP programmer so don't know how to put the syntax together. However, I think this should be very easy and quick for someone who knows what they're doing.
Merlinofchaos has described exactly what needs to be done. See: http://drupal.org/node/594916
I can pay a $100 bounty to anyone that can give me a working peice of code to paste in. I will need to reuse this code in multiple views each with the same issue above so also need some instructions on how to adapt it.
Thanks!

Code to add
Add the fillowing ocde in your second argumnets
Provide default argument -> PHP Code
if(is_numeric(arg(1))) {$node = node_load(arg(1));
if($node->type == 'testa'){ //testa: name of your first node reference node type
return 16; // 16: your fixed nid
}
}
thanks, but still doesn't seem to be working
This may be because I have not configured everything as you intended. Eg should the second argument be an OR argument (through Views_or) or an AND argument, should I allow multiple terms per argument etc etc.
I am ideally hoping for the solution proposed by merlinofchaos, where the two values are set through a single argument.
To illustrate what I am trying to do see these pages:
The table midway down this panel page should display both the school's particular data and National data (as it does correctly, under my Views_or solution): http://www.educationadviser.co.uk/node/92857
This one should display nothing, but currently still displays national data (because the second argument displays even though the first does not validate): http://www.educationadviser.co.uk/node/120000
I would start by adding a
I would start by adding a field to the content type(s) that can referenced using node referrer. Lets call the field 'field_backlink'.
Your view would have only on argument (the nid of the node being viewed) and you could add PHP code for the argument something like
$node = menu_get_object();$nids = array();
if ( !empty($node) ) {
$nids[] = $node->nid;
if ( !empty($node->field_backlink) ) {
// Have reference, add fixed node
$nids[] = 16;
}
return implode(',', $nids);
}
Make sure the argument is set to handle multiple values.
I wonder though if custom theming or panels might be a better approach.
Thanks very much, although
Thanks very much, although this solution is a little more complex than I had in mind.
I was hoping for a solution similar to this: http://drupal.org/node/594916
I want to avoid having to add new fields to the database, and only really want to install additional modules if there is some benefit in avoiding the need for custom code. However, I appreciate the suggestion, and interesting approach.
Tweaking the visibility settings for the view on Panels is another approach that could work. Again, if you know how this could be done please let me know!
Thanks again,
Daniel
I suggested adding the field,
I suggested adding the field, otherwise to verify a node is referenced you would need to query the database each time (note this would be true with panels also).
Otherwise the code is just an implementation of the comment you reference.
The sticking point then is the fact you want to check if a node is actually referenced,
Solution
Thanks - & appreciate your point.
However, for various reasons I have gone with a solution that does query the database. Hopefully this will not create performance issues. Thanks to edmund.kwok for supplying.
The code in the views argument validator is:
$count = db_result(db_query("SELECT nid FROM {content_type_mycontenttypehere} WHERE field_mynodereferencefieldhere_nid = %d", $argument));if ($count) {
$handler->argument = $argument . '+myfixedNIDhere';
return TRUE;
}
else {
return FALSE;
}
Thanks again for all the help