[themes] User info in blocks

Tony Yarusso tonyyarusso at gmail.com
Thu Feb 7 22:08:16 UTC 2008


On Feb 7, 2008 2:40 PM, Florian Loretan <floretan at gmail.com> wrote:

> The $node variable is not defined in hook_block.
>
> What you need to do is make sure you are indeed on a node page of type
> "blog", and then load the information from the url in the 'display'
> section of your hook_block:
>
> if (arg(0) == 'node' && is_numeric(arg(1))) {
>  $node = node_load(array('nid' => arg(1)));
>  if ($node->type == 'blog') {
>    $author = user_load(array('uid' => $node->uid));
>    $block['subject'] = t('About the author');
>    $block['content'] = theme('username', $author); // do whatever you
> want to create the content of your block using $node and $author
>    // ...
>
>    return $block;
>  }
> }
> else {
>  return; // you want to make sure this block is not displayed on other
> pages.
> }
>
> On Feb 7, 2008 12:24 PM, Tony Yarusso <tonyyarusso at gmail.com> wrote:
> > I'm trying to put together a block that will display additional
> information
> > on users' blog pages.  The person I'm working with wants some of the
> more
> > "traditional" blog stuff, like a user picture, short bio, blogroll, and
> > archives links shown in the sidebar.  All of the actual information is
> > supported by other modules, so I just need to be able to access it from
> > within my theme.  Essentially, what I'm going for is to load this block
> > whenever the node type is 'blog post', and populate it with the content
> > matching the author of the node.
> >
> > I am using a small snippet of code that someone else gave me already for
> > displaying a title.  This is residing in node-blog.tpl.php right now and
> > works fine:
> > <?php $the_user = user_load(array('uid' => $node->uid)); ?>
> > <?php if ($the_user->profile_blog_title != ""): ?>
> >   <h2><?php print $the_user->profile_blog_title ?></h2>
> >   <?php else: ?>
> >   <h2><?php print $node->name . "'s blog" ?></h2>
> > <?php endif; ?>
> >
> > What I've been using so far in my module's implementation of hook_block
> > looks like this, which is just worrying about the picture for now:
> >         case 'view':
> >             $block['subject'] = t('Blog');
> >              $the_user = user_load(array('uid' => $node->uid));
> >                 $user_pic = $the_user->picture;
> >             $block['content'] = $user_pic;
> >             return $block;
> >
> > However, this does not return anything, and upon further investigation
> with
> > print_r(), it appears to be returning the anonymous user from 'uid',
> which
> > of course doesn't have a picture associated with it.  Part of what I'm
> not
> > sure about is whether I can access the node information ($node->uid)
> from
> > within the block, since it's in the sidebar, not the main content area
> where
> > the node is loaded.  Please excuse me as I'm very new to PHP and Drupal
> > development, but looking to learn.
> >
> > --
> > Tony Yarusso
> > http://tonyyarusso.com/
> > _______________________________________________
> > themes mailing list
> > themes at drupal.org
> > http://lists.drupal.org/mailman/listinfo/themes
> >
> >
> _______________________________________________
> themes mailing list
> themes at drupal.org
> http://lists.drupal.org/mailman/listinfo/themes
>

Ah, okay.  That worked, but only partially.  I now have things displaying
okay when looking at any individual blog post (ie,
'blog/tony/2008/01/31/converted-site-drupal'), but not on the main blog
pages ('blog', 'blog/tony', etc.).  Apparently this is because those are
"views", not nodes, but I'm not really sure how to interact with a view
now.  To make things more complicated, it only makes sense to display this
stuff when everything belongs to the same user, so 'blog/tony' should show
the block, but not just 'blog'.

-- 
Tony Yarusso
http://tonyyarusso.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/themes/attachments/20080207/718bb426/attachment.htm 


More information about the themes mailing list