[development] Restricting node reference by uid

Larry Garfield larry at garfieldtech.com
Mon Sep 6 17:50:26 UTC 2010


I actually ended up doing exactly that on a project a while back.  The trick 
is to use a custom argument default plugin.  In my case I had a nodereference 
field that should point to "nodes owned by the current user (plus other 
restrictions)" on create, but "nodes owned by the owner of this node (plus 
those same restrictions)" on edit.  The plugin itself is really really simple:

class webb_plugin_argument_default_owner_or_user extends 
views_plugin_argument_default {
  function argument_form(&$form, &$form_state) {
  }

  function get_argument() {
    $node = menu_get_object('node');
    if ($node && $node->type == 'note') {
      return $node->uid;
    }
    else {
      return $GLOBALS['user']->uid;
    }
  }
}

The corresponding views definition hook is left as an exercise for the reader.

Then add an argument to a view to filter by uid (or a userreference, or 
whatever), and then set the default argument plugin to this plugin.  When 
called from a userreference field it will get no argument, and therefore use 
the logic above to fill in that argument.  

--Larry Garfield


On Monday, September 06, 2010 08:04:00 am E.J. Zufelt wrote:
> Good morning,
> 
> I was able to setup the filter properly,  but you have raised an
> interesting point.
> 
> Should the filter then be all nodes with the uid of the current user on
> create, and all nodes with the same uid as the referencing node on edit?
> 
> Thanks,
> Everett Zufelt
> http://zufelt.ca
> 
> Follow me on Twitter
> http://twitter.com/ezufelt
> 
> View my LinkedIn Profile
> http://www.linkedin.com/in/ezufelt
> 
> On 2010-09-06, at 8:56 AM, Jerad Bitner wrote:
> > One problem with the views filter I've had is that when you limit it
> > by uid, and you try to edit someone else's content as admin, the
> > nodereference is no longer available, because you are not the original
> > user. Has anyone found a reliable way to filter by current user OR
> > admin? I was unsuccessful last time I tried using views_or to try to
> > do this.
> > 
> > On Mon, Sep 6, 2010 at 12:22 AM, Chris Skene <chris at xtfer.com> wrote:
> >> Try creating a view filtering on nodes created by the current user, then
> >> use that in the Nodereference settings.
> >> On 06/09/2010, at 2:14 PM, E.J. Zufelt wrote:
> >> 
> >> Good evening,
> >> I am wondering if it is possible to restrict the nodes displayed in the
> >> CCK node reference field to only nodes created by the current user? 
> >> Currently I believe all nodes that the current user can access are
> >> being returned. Thanks in advance,
> >> Everett Zufelt
> >> http://zufelt.ca
> >> Follow me on Twitter
> >> http://twitter.com/ezufelt
> >> 
> >> View my LinkedIn Profile
> >> http://www.linkedin.com/in/ezufelt


More information about the development mailing list