1

I looked a lot of places and couldn't find a way to filter the results of a view based on the nodes a user could actually edit. So I used Views PHP Filter and some custom code to make it happen.

Comments and improvements welcome.

global $user;
$type = array();
$nid_list = array();
$perms = array();
foreach($user->roles as $role) {
  $role_perms = db_fetch_array(db_query("SELECT `role`.`name`, `permission`.`perm` FROM {role} INNER JOIN {permission} ON `role`.`rid` = `permission`.`rid` WHERE `role`.`name` = '%s'", $role));
  $perm_temp = explode(", ", $role_perms['perm']);
  $perms = array_merge($perms, $perm_temp);
}
foreach($perms as $perm) {
  $pos_edit = strpos($perm, "edit");
  $pos_content = strpos($perm, "content");
  $pos_products = strpos($perm, "products");
  if (($pos_edit !== false) && ($pos_content !== false)) {
    $perm = substr($perm,9,strlen($perm)-16);
    if (!in_array($perm, $type)) {
      $type[] = $perm;
    }
  } else if (($pos_edit !== false) && ($pos_products !== false)) {
     $perm = substr($perm,9,strlen($perm)-18);
     if (!in_array($perm, $type)) {
       $type[] = $perm;
     }
  }
}
foreach($type as $ctype) {
  $nids = db_query("SELECT `node`.`nid` FROM `node` WHERE `node`.`type` = '%s'", $ctype);
  while($row = db_fetch_array($nids)) {
    $nid_list[] = $row['nid'];
  }
}
asort($nid_list);
return $nid_list;
2
  • Hi mbent, welcome to DA, what is exactly your question ? Is your code not working and your looking for help ? Do you want us to improve it ? – tostinni Dec 2 '11 at 18:55
  • Mostly I was looking for feedback. I also thought I would post it if someone was looking for something similar. – merauluka Dec 2 '11 at 23:30
1

I would take the ORDER BY out of your last query and let views sort things itself. After all, you may want to sort the nids by date or something later on, so why do things twice?

And also change your query style to use the drupal syntax, like:

SELECT nid FROM {node}...
1
  • Thanks very much for that. I did that initially when I was testing the output. – merauluka Dec 2 '11 at 23:31
1

Be careful using php code in this way. This option in views is intended for examination during development and not for an active site. For this code located in the database to work you have to turn on php interpretation and this could let a user with an understanding of php code get access to unauthorized materials on the site. I recommend that you create a custom module and and a hook to views. I'm not certain which one, but the available hooks are discussed in the advanced help section for views.

2
  • At the moment the code is only used in a filter available to trusted users in our office. It's used on a content list so people will only see content they have access to edit. I'm going to have to look into creating a module for views. I have struggled with that in the past because I'm still very new to Drupal and it's structure. I've spent a year learning from the ground up. Views plugins, extensions, and custom modules are still a mystery to me. – merauluka Dec 2 '11 at 23:32
  • "Spent a year" sounds about right. There's a lot to learn and its not well documented. I got the book "the Definitive Guide to Drupal 7" which gives a lot of solid info about Drupal. Knowing PHP will help a lot although the structure inside Drupal is fairly sophisticated. This site is a good place to get support. Lots of experience with Drupal here and they're very willing to help. – Ashlar Dec 3 '11 at 2:05

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.