In this handbook (http://drupal.org/node/134431) it shows how to display blocks only when viewing a blog entry from a specific author. If anyone could help me out, I have a question regarding this code.

I want to display a different block for each author's blog entries. This code seems to work for this reason with one draw back. It seems to place all of the blocks when you view all blog entries (/blog/). So when you click view blogs you see this big long column of blocks.

I have tried rearranging this code a million times, I am not sure what I am doing just trial and error. Can anyone spot what might be causing the trouble? Thank you.


$match = FALSE;
$type = 'blog';

if (arg(0) == $type){
    $match = TRUE;
}
else if (arg(0) == 'node' && is_numeric(arg(1))) {
     $nid = arg(1);
     $node = node_load(array('nid' => $nid));
    $nodetype = $node->type;
    $name = $node->name;
     if ($type == $nodetype && $name == 'UsernameHere') {
        $match = TRUE;
    }
}

return $match;

Comments

nevets’s picture

I think you want to remove the first 'if' like this

$match = FALSE;

if (arg(0) == 'node' && is_numeric(arg(1))) {
     $nid = arg(1);
     $node = node_load($nid);
    $nodetype = $node->type;
    $name = $node->name;
     if ($type == $nodetype && $name == 'UsernameHere') {
        $match = TRUE;
    }
}

return $match;

Note I changed the call to node_load so it is more efficient, passing just $nid will get the cached copy of the node,

I would also recommend changing

    $name = $node->name;
     if ($type == $nodetype && $name == 'UsernameHere') {
        $match = TRUE;
    }

to

    // You need to replace UserIdHere with an actual user id (number)
     if ($type == $nodetype && $node->uid == UserIdHere ) {
        $match = TRUE;
    }

in part because a users name can change and in part because it is faster to compare numbers that strings.

Cory Goodwin’s picture

When I removed:

$type = 'blog';

if (arg(0) == $type){
    $match = TRUE;
}
else

No blocks were displayed on the site.

When I left the last line "else" in the script it put the long list of blocks on all pages of the site.

Thank you for taking the time to look into it.

manoloka’s picture

Thanks for this, but I need a slightly modification, how would the script be for a block to show only to de super admin user (uid=1)?

nevets’s picture

To show a block only to user 1 you can use

global $user;
if ( $user->uid == 1 ) {
  return TRUE;
}
return FALSE;
Cory Goodwin’s picture

I am thinking it's not important that we reference only blog pages.

If the author wrote a story or book node then the blog should be displayed as well.

Is there a way to show a specific author specific block on all pages that the author wrote?

Cory Goodwin’s picture

I had a cup of coffee, talked about with my girlfriend about the plan for lunch. And bingo it hit me.

I commented out
$type = 'blog'; and /*$type == $nodetype && */
and it worked perfectly. I will also post this on the orignial thread.

My other question still has my interest piqued though. If we can display a specific block for all content by an author and not limit it to just the blog entries.

Any suggestions?

nevets’s picture

Your two edits should do just that. The first removes the check for being on a users blog page (listing their blogs) so it will now only display on pages displaying a single node. Removing the check for node type means it show for any content authored by the person you check for.

Cory Goodwin’s picture

I actually stopped working on the site for the day after getting that right.

I went back to see the other pages and if the blocks showed and it worked. Thanks for pointing that out.

I just added the line code you gave me to switch to user ID's. It worked perfectly. Your absolutely right, It makes since to use the ID instead of the name since they can change it.

Thank you for your help.