Last updated January 17, 2012. Created by gnassar on January 17, 2012.
Log in to edit this page.

LowMemory's verbatim original example for Views PHP Filter, to my knowledge the first such example for 6.x in the handbook. I owe LowMemory thanks for officially kicking off this section.

------
This example isn't particularly clever or anything.

I couldn't find an example of a snippet anywhere when I was trying to work it out so thought I'd post mine so that other newcomers perhaps don't take as long as I did.

Am very happy for anyone to replace this with a better snippet.

It gives the nids of the nodes where the current user is author or is referenced in a user reference cck field (field_users).

Updated with some code example to show ALL nodes of type 'page' to administrator roles, and to show ONLY user's own nodes to all other roles (inspired by Drupal v5's example).

<?php
global $user;
$uid=$user->uid;
$nids = array();
//get nids where user is author
$my_result = db_query("SELECT nid FROM node WHERE uid = $uid");
while (
$my_row = db_fetch_array($my_result))
{
   
$nids[] = $my_row['nid'];
}
//give nids where user is referenced
$my_result = db_query("SELECT nid FROM content_field_users WHERE field_users_uid = $uid");
while (
$my_row = db_fetch_array($my_result))
{
   
$nids[] = $my_row['nid'];
}
// give all nids of type 'page' for users having some kind of administrator role
// but give only nids of type 'page' for other users
$type = 'page';
$allowed = array('Administrator', 'Custom Admin');
$user_is_allowed = false;
foreach(
$user->roles as $role){
  if(
in_array($role, $allowed)) {
   
$user_is_allowed = true;
  }
}
if (
$user_is_allowed) {
  
$nids = db_result(db_query("SELECT GROUP_CONCAT(nid) FROM node WHERE type = '%s'", $type));
} else {
   
// care: $uid is defined at the very beginning
   
$nids = db_result(db_query("SELECT GROUP_CONCAT(nid) FROM node WHERE type = '%s' AND uid = %d", $type, $uid));
}
return
$nids;
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.