Deplifer helped to make this snippet (http://drupal.org/node/162567). Thank you very much, deplifer!

By default one can't apply different filter to view depending on the viewer's role. So, using your code in Views PHP Filter module, you can do it. Now the output of the view depends on the user's role.

To show ALL nodes of the certain type to administrator roles, and to show ONLY user's own nodes to all other roles, you need to use Views PHP Filter module. Install it, then create the usual view, enable "Node: Node ID" filter, with operator "Is One Of", and following value:

global $user;
$type = 'page';
$allowed = array('Administrator','Editor');
foreach($user->roles as $role){
  if(in_array($role, $allowed)) {
    $nids = db_result(db_query("SELECT GROUP_CONCAT(nid) FROM node WHERE type = '%s'", $type));
  } else {
    $uid=$user->uid;
    $nids = db_result(db_query("SELECT GROUP_CONCAT(nid) FROM node WHERE type = '%s' AND uid = %d", $type, $uid));
  }
}
return $nids;

This might be not very clean code (I don't know PHP, I just played with snippets from this site), but anyway it is working and useful. You can use it, for example, with node reference field from cck module (it can be configured on the field setting page). Now non-administrators will be able to reference to their own nodes only, while administrators - to any node.

Comments

RAMilewski’s picture

Can someone please provide a link to the "Views PHP Filter Module" describe above. Does it exist for Drupal 5.x?

...found it! It's here: http://drupal.org/project/viewsphpfilter

handsofaten’s picture

Trying to figure out how to do something like this with Drupal 6 and Views 2. The Views PHP Filter module doesn't exist for this configuration. Any ideas? Can I implement a hook that will insert PHP code into the filter for a particular view?

LowMemory’s picture

Late, but for everyone ending here to know:

I've mixed up this code with Drupal's 6 example code (on http://drupal.org/node/763980 ), where you can find result code.

suffering drupal’s picture

Hello,
is it possible to have a filter in views be a variable instead of a fixed value from the dropdown? For example a role?
For a specific role views generates the following code:

SELECT users.uid AS uid,
users.picture AS users_picture,
users.name AS users_name,
location.city AS location_city,
location.country AS location_country
FROM users users
INNER JOIN users_roles users_roles ON users.uid = users_roles.uid
LEFT JOIN location_instance location_instance ON users.uid = location_instance.uid
LEFT JOIN location location ON location_instance.lid = location.lid
WHERE (users.uid not in ('0')) AND (users_roles.rid = 4)

Instead of the fixed role id number, in this case 4, I want the role to be the same as assigned to the current user.

Two questions:
1. What would the needed code be
2. In what PHP file should I put this code (in other cases I sometimes found code I wanted to try, but did not know where to put it, I'm a newbie since almost 2 years and only making slow progress - trial and error in few spare hours...)

thanx

I started with Drupal in 2007 and then my life got stuck...