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.
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@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@drupal.org http://lists.drupal.org/mailman/listinfo/themes
On Feb 7, 2008 2:40 PM, Florian Loretan floretan@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@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@drupal.org http://lists.drupal.org/mailman/listinfo/themes
themes mailing list themes@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'.
On Thu, Feb 07, 2008 at 04:08:16PM -0600, Tony Yarusso wrote:
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'.
Have you looked into the "Page specific visibility settings" for this particular block's configuration page? You can goto this configuration page by clicking the "configure" link next to the block's name in admin/build/block.
The description under this settings says: Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. <front> is the front page. If the PHP-mode is chosen, enter PHP code between <?php ?>. Note that executing incorrect PHP-code can break your Drupal site.
Have you looked into the "Page specific visibility settings" for this particular block's configuration page? You can goto this configuration page by clicking the "configure" link next to the block's name in admin/build/block.
These configuration options let you decide on what page you want the block to be displayed. However, if your block is dependent on some context-related data, it is up to you to handle how you access that data and to make sure that you never use invalid data or you'll break your site.
For example, if you use arg(1) to get the nid of the current node which you will load with node_load() it will work on /node/123, but if you display that block on a page where arg(1) returns a string like /blog/john you will call node_load('john') and get a "white screen of death".
Another example from a core module is the login block: you choose where you want it displayed, but the code generating the block makes sure that it is only displayed for anonymous users.