Add Block Regions to your User Profile page in Drupal 5 and 6
Aug
2008
Here's a small snippet to allow the output of blocks in your user profiles. This is assuming that you are currently over-riding the user page with the following code:
Update!
I've added code for both Drupal 5 and 6, take a look.
Drupal 5
<?php
/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($user, $fields = array()) {
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
// will be assigned within your template.
/* potential need for other code to extract field info */
return _phptemplate_callback('user_profile', array('user' => $user, 'fields' => $fields));
}
?>(Code from: http://drupal.org/node/35728 )
and have your user_profile.tpl.php named as so.
Settings up your block regions:
First we'll slip into our theme's template.php file and make sure we create a new region.
<?php
function basic_regions() {
return array(
'sidebar_left' => t('left sidebar'),
'sidebar_right' => t('right sidebar'),
'content_top' => t('content top'),
'content_bottom' => t('content bottom'),
'header' => t('header'),
'footer_block' => t('footer'),
'profile_block' => t('profile blocks'),
);
}
?> This allows us to create a new region called profile block which will hold all our blocks that we decide to put there. Now we want to be able to pass the $profile_block variable onto our user_profile.tpl.php to be outputted.
Adding the Block variables:
<?php
function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'user_profile':
$vars['profile_block'] = theme('blocks', 'profile_block');
break;
}
return $vars;
}
?>Now we can go into our user_profile.tpl.php and add the following code anywhere in the file. This is where our newly created block region will be outputted.
<?php
echo $profile_block;
?>Drupal 6
In your themename.info file (themename being replaced by your actual theme name) add your block region.
(In our themename.info)
<?php
regions[profile_block] = Profile Block
?>We now create our user-profile.tpl.php</code file where we will output the block region and any other profile information that we need. To expose the <code>$profile_block variable to our user-profile.tpl.php, we add the following code to our template.php file:
(In our template.php)
<?php
function phptemplate_preprocess_user_profile(&$variables) {
$variables['profile'] = array();
// Sort sections by weight
uasort($variables['account']->content, 'element_sort');
// Provide keyed variables so themers can print each section independantly.
foreach (element_children($variables['account']->content) as $key) {
$variables['profile'][$key] = drupal_render($variables['account']->content[$key]);
}
// Collect all profiles to make it easier to print all items at once.
$variables['user_profile'] = implode($variables['profile']);
$variables['profile_block'] = theme('blocks', 'profile_block');
}
?>( Structure taken from: http://api.drupal.org/api/function/template_preprocess_user_profile/6 )
Note the added
$variables['profile_block'] = theme('blocks', 'profile_block'); at the end. This allows us to access the block region, profile_block, by echoing out $profile_block in our user-profile.tpl.php file like so:
(In our user-profile.tpl.php)
<?php
echo $profile_block;
?>Voila! Block regions in our user profile template file!










Comments
this definitely worked to
Thu, 07/02/2009 - 18:24 - Promotional items (not verified)this definitely worked to output of blocks in your user profiles. I just tried it and it worked. Drupal works so good for my blogs. I use it for all my blogs.
thanks for the great
Tue, 06/30/2009 - 00:43 - turnkey websites (not verified)thanks for the great information. This is what i have been looking for.
But how can I just add regions?
Mon, 02/09/2009 - 08:18 - The AntiVirus (not verified)I'm trying to just add another block region I could use in Drupal 6. I updated my .info file adding the new region name. But what shall I do next?
Thanks.
Thanks!
Sun, 12/14/2008 - 00:02 - CB (not verified)Thanks so much for this tip! It really helped me a ton!
Can I use this to add latest blog posts by this user?
Mon, 12/08/2008 - 19:10 - Alice (not verified)Great tutorial, I think this is what I need to add "latest posts by this user" to the profile page. Any ideas how I might do this? Sorry quickly getting lost in all this...
A xx
Thanks for the tip. Found I
Mon, 11/10/2008 - 02:58 - Jason Ruyle (not verified)Thanks for the tip. Found I needed just the thing tonight.
slight change needed(?)
Thu, 09/04/2008 - 19:23 - tc (not verified)I found that with drupal 6.3, my methods in template.php all start with phptemplate for whatever reason.
so - using this method name - template_preprocess_user_profile - produced a duplicate definition error, but by prepending the php - ala: 'phptemplate_preprocess_user_profile', it worked like a charm.
Thanks for the original post!
Totally correct. "template"
Mon, 09/08/2008 - 01:30 - Steve KruegerTotally correct. "template" should be replaced with your current theme name or "phptemplate". I guess I should define that a bit more. thanks for the catch!
Thank you!
Wed, 08/20/2008 - 14:59 - Anonymous (not verified)Yes, a nice tutorial that should expand my knowledge and skill. Will give this a go.
Thank You!
Is this example Drupal 6
Mon, 08/18/2008 - 09:34 - Drupal Theme Garden (not verified)Is this example Drupal 6 compatible ?
Hey, I've updated the post
Mon, 08/18/2008 - 18:10 - Steve KruegerHey,
I've updated the post to include Drupal 6 compatibility.
a nice example on how to
Sat, 08/16/2008 - 13:06 - Anonymous (not verified)a nice example on how to provide new regions to a specific template file, thanks :)
Post new comment