There are certain roles on my site that can post content.

These roles are Free, Basic, Premium

I would like certain blocks only to show on a node if the node author has a certain role.

I want nodes by Free roles to display all available adverts blocks, nodes by Basic to show one advert block, and nodes by Premium not to show any advert blocks.

Would someone provide me with some php snippet for this.

Thank you

Comments

cog.rusty’s picture

Something like this in each block's visibility settings, using PHP input format:

$dont_show_if_author_is = array("Premium", "Basic");   // adjust this as needed

$showit = FALSE;  // adjust if you want the ad in non-node pages
if ( arg(0)=='node' && ctype_digit(arg(1)) ) {  // if we are on a full node page
  $node = node_load(array('nid' => arg(1)));
  $author = user_load(array('uid' => $node->uid));
  if (count( array_intersect($dont_show_if_author_is, $author->roles) > 0 )) {
    $showit = FALSE;
  }
  else {
    $showit = TRUE;
  }
}
return $showit; 
droople’s picture

I will test this over the next few days and bring back some feedback

cheers

ocamp’s picture

thanks, but how do i show this on every page not just node pages?

nevets’s picture

It only makes sense on node pages since it tests against the nodes author.

ocamp’s picture

oh yeah v_v

how can I get the authors role from arguments etc.

like views, user/[uid]/viewname

so if that user is role a display, but if that user is role b dont?

nevets’s picture

When you configure a block there is a section labeled "Show block for specific roles" where you can select the roles you want to be able to see the block.

cog.rusty’s picture

nevets, if I understood the question correctly, everyone should see the same blocks, based on who the author of the node is.

nevets’s picture

I missed that part of it being the authors role.

PeterZ’s picture

I've been trying a lot of variations of the code above from several different posts, but can't get it working to control display of a views block in Drupal 6.10. With this code in the block visibility settings, the block never displays.

I did try similar code in the node.tpl.php file, and it seemed to be finding all the variables.

And, I can control display by node type on the same page with the following code.

  // Only show if $match is true
  $match = false;
  // Which node types
  $types = array('report');
  // Match current node type with array of types
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load(array('nid' => $nid));
    $type = $node->type; 
    $match = in_array($type, $types);
  }
  return $match;

But I can't control display by node author using code further above, which I've slightly modified below:

$dont_show_if_author_is = array("subscriber200");   // adjust this as needed

$showit = FALSE;  // adjust if you want the ad in non-node pages
if ( arg(0)=='node' && ctype_digit(arg(1)) ) {  // if we are on a full node page
  $node = node_load(array('nid' => arg(1)));
  $author = user_load(array('uid' => $node->uid));
  if (count( array_intersect($dont_show_if_author_is, $author->roles) > 0 )) {
    $showit = FALSE;
  }
  else {
    $showit = TRUE;
  }
}
return $showit; 

I've tried variations of the above code, but seem to always get FALSE returned.

Any thoughts on how I can approach debugging this?

Other nodes/links that describe the same/similar solutions are:
http://drupal.org/node/273346
http://www.feike.biz/weblog/2008/01/07/drupal-using-node-and-user-inform...
http://drupal.org/node/64135

cog.rusty’s picture

My mistake. I don't write much PHP, so sometimes I don't see clearly. Change:

if (count( array_intersect($dont_show_if_author_is, $author->roles) > 0 )) {

to:

if (count( array_intersect($dont_show_if_author_is, $author->roles)) > 0 ) {
PeterZ’s picture

Very nice. Yes, that fixed it. Many thanks!

I combined it with showing based on node type to get:

  // Which node types to display on
  $display_on_types = array('hospital');

  // which author roles to hide on
  $dont_show_if_author_is = array('subscriber100');

  $match = FALSE;
  $showit = FALSE;

  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load(array('nid' => $nid));
    $type = $node->type; 
    // check if we should display on this node type
    $match = in_array($type, $display_on_types);
    if ($match == TRUE) {
      $author = user_load(array('uid' => $node->uid));
      if (count( array_intersect($dont_show_if_author_is, $author->roles)) > 0 ) {
        $showit = FALSE;
      }
      else {
        $showit = TRUE;
      }
    }
  }
// showit will only be true if match was also true
return $showit;
YIY’s picture

Thank you for a useful code.
But which code should be used if I want certain block only to show on a node if the node author has a certain NAME (not a role)?
I'd be grateful for your help.

cog.rusty’s picture

You can change

$dont_show_if_author_is = array('role1', 'role2', 'role3');

to

$show_if_author_name_is = array('name1', 'name2', 'name3');

and then change

      if (count( array_intersect($dont_show_if_author_is, $author->roles)) > 0 ) {
        $showit = FALSE;
      }
      else {
        $showit = TRUE;
      }

to

      if (in_array( $author->name, $show_if_author_name_is )) {
        $showit = TRUE;
      }
      else {
        $showit = FALSE;
      }

Personally, I would stick to the role-based one, I would create an additional special role and assign it to the users. It seems more flexible and logical to me.

nevets’s picture

Also, since users names can be changed the code would be easy to break.

YIY’s picture

It works! Thank you!

manoloka’s picture

This worked great for mi.
Thanks