My scenario is :
Content type
Story (has a node_reference field, type Photo) | Photo (has a image cck field)
When editing a story, I'd like the node reference to be able to choose a photo which owned by the same author of the story.
So I create a view, photo_reference, which basically takes an argument: uid, and has one field: title.
But when I set the node_reference setting it's not possible to make the uid argument be passed dynamically.
It seems that you can only pass a defined argument to the view, and not able to use the view's default argument.
In my case I actually write a PHP default argument in the view, but it's never called, so I have to impelement hook_views_pre_view to set the argument.
// hook_views_pre_view
function hook_views_pre_view(&$view, $display_id = 'default', $args = array()) {
if ($view->name == 'photo_reference' && !$args) {
// set the default arguments to be the node's author,
// then the photo_reference can get the photos owned by this user,
// and put them in the node reference selecton
$nid = 0;
$path = explode('/', $_GET['q']);
if(sizeof($path) == 3 && $path[0] == 'node' && is_numeric($path[1]) && $path[2] == 'edit' ) {
$nid = $path[1];
}
if ($nid) { // get node from url
$node = node_load($nid);
$uid = $node->uid;
}
else{ // may be new node, use the logged user id
global $user;
$uid = $user->uid;
}
$view->set_arguments(array($uid));
}
}
The above code is basically ok to fit in a views default argument. If the node_reference can load default argument when the argument field is empty, it will be great.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | tokenuid.PNG | 9.39 KB | yark |
Comments
Comment #1
blueskiwi commentedThanks for the code, I haven't tried it but I have the same requirement... to the use the view's default argument handling code when none is specified by the node reference argument field (otherwise you're stuck typing in a static argument - no good)
Comment #2
blueskiwi commentedComment #3
yark commentedI think it will be great to use tokens in View arguments field as in pathauto settings
Also in view filter you can use option
User: Current
Filter the view to the currently logged in user.
Comment #4
drewish commentedsubscribing, would be pretty cool.
Comment #5
drewish commentedmarked #295080: dinamically define view's arguments in nodereference definition as a duplicate
Comment #6
yched commentedLong standing request :-) There's already an issue with a patch for that, maybe two. The main reason it's not in is that no-one dare to review it enough to set it RTBC - well I'm also not completely sold on the case of node *creation* form (we don't have a node yet, thus no node context).
See http://drupal.org/node/196518#comment-984025