Hi all,
Can some PHP genius help me write a code, please?
How to output comma-separated list of node IDs of content type X created by user-id Y? This is needed to configure Views PHP Filter module...

Comments

deplifer’s picture

In php

// don't know if this help but this let's you select and comma separated list of nid's given by node type and uid

$uid = number;
$type = 'string';

// gives us an coma seperated list of nids works only for mysql(i)
$nids = db_result(db_query("SELECT GROUP_CONCAT(nid) FROM node WHERE type = '%s' AND uid = %d", $type, $uid))

In Views

As far as iknow you should be able todo the most of this without having to use php,

  1. Add an view
  2. In filters add the filter for Node Type
  3. In filters add an filter for Node Author is current user
birukoff-1’s picture

Thank you very much, deplifer! You really helped a lot!

The problem is that by default I can't change filters or arguments for certain selected roles. So, using your code, I did it with Views PHP Filter module. Now the output of the view depends on the user's role!

For those who wish to do the same, here is the way.
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.

I should probably add it to Drupal Handbook so that other people can find it easily.

scott815’s picture

Does anyone know if this works in Drupal 6 or what I have to make it work for drupal 6. Might be easier way to do in drupal 6 but I am unable to find one yet.