2

So I have an entity reference field on a node, the widget is a select-box.

I want so that an user can choose one of their own authored nodes as a reference OR if an admin is logged in, they can choose any content. So admins should be able to bypass this restriction (with a permission, role or something).

I tried building a view based on this, but the contextual filter seems to be the problem. I can't really do any either/or kind of thing with contextual filter.

  • 1
    There is OR in views filters available, right? Have you tried it? What's your problem with it? – Mołot Dec 19 '14 at 8:52
  • @Mołot I can't filter by current user's content in views filters. Contextual filter works, but there's no way to call that in views filters. – Risse Dec 19 '14 at 8:59
  • Why can't you? I do it on molot.pl to allow people to only reference their gifts on their gift lists, and it works, no sweat. And no need for contextual, just filters. – Mołot Dec 19 '14 at 9:01
  • I added a filter "Content: Author uid". I can't give it current user's uid, it just wants a list of uids. – Risse Dec 19 '14 at 9:05
  • 1
    I have (author) User: Current (Yes) set up in filters and it's simply there, working. – Mołot Dec 19 '14 at 9:12
1

Background

So I have an entity reference field on a node, the widget is a select-box.

You (Risse) have already done this, but to make this clear for other users, this involves adding an entity reference field to a content type and in the field settings changing from the default 'Simple..' to 'Views..':

enter image description here

Now the select list used to choose a referenced entity (in this case a node), will use views to create the select list, allowing us to customise the nodes listed. In the example above I have created a view (and added an 'Entity Reference' display) and called it entity_reference_select.

I want so that an user can choose one of their own authored nodes as a reference OR if an admin is logged in, they can choose any content. So admins should be able to bypass this restriction (with a permission, role or something).

It is easy enough to list only nodes by the current author using the filter "Content: Author uid" in the view (as per @molot above). Unfortunately this does allow us to differentiate the results for admin users.

An appropriate response might initially seen to involve an OR statement in the views filters to allow admin user to see all nodes in the select list, and other users to only see their own nodes. Unfortunately there are two issues with this:

  • There is no way to create a views Relationship to the current user in order to evaluate the current user's roles in the filters section
  • An OR conditional in the filters will remove options, not add them, so would remove non authored nodes from an admin role user anyway

Solution

Provide a default value for a contextual filter which is defined by PHP. This means we can specify which user(author) we are going to filter the results by, which would be the current user (fetched from the global $user object) unless the current user has the 'administrator' role, in which case we list nodes authored by all users.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.