0

I've created one drupal7 view which will filter all contents based on roles not current user role. For that I've created one relationship 'author' then in view filter criteria I've given exposed author roles. But by default Anonymous and Authenticated roles are not showing there. Do we have any method to add Anonymous and Authenticated role to that filter?

1 Answer 1

1

That's because they are not really real roles, more just a group of users.

What I mean by this is that the relationship between users and roles is stored in the table users_roles. There is one field for uid (the users ID), and one field for rid (the role ID).

So, the admin user has a uid of 1 and also has the administrator role with an rid of 3, so there is an entry in the users_roles table like: 1 | 3.

Any other roles created and assigned will also have rows inserted into this table.

However when the admin user logs into the site there is not a row inserted into users_roles table for the "authenticated user" role. And likewise when they log out a row is not inserted for the "anonymous user" role.

The actual query used by views would be something like this:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, node.uid, ur.rid
FROM node node
LEFT JOIN users users_node ON node.uid = users_node.uid
INNER JOIN users_roles ur ON users_node.uid = ur.uid
WHERE node.`status` = '1' AND node.type IN  ('article') AND ur.rid = '3'
ORDER BY node_created DESC

Here it is joining onto the users_roles table, but we know that the roles "anonymous user" and "authenticated user" don't have entries in the users_role table so it won't work.

So whilst it is possible to add these 2 roles into the select, the query that runs won't ever return anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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