Add Block Regions to your User Profile page in Drupal 5 and 6

18
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

Thanks for the tip. Found I

Thanks for the tip. Found I needed just the thing tonight.

slight change needed(?)

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"

Steve Krueger's picture

Totally 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!

Yes, a nice tutorial that should expand my knowledge and skill. Will give this a go.

Thank You!

Is this example Drupal 6

Is this example Drupal 6 compatible ?

Hey, I've updated the post

Steve Krueger's picture

Hey,

I've updated the post to include Drupal 6 compatibility.

a nice example on how to

a nice example on how to provide new regions to a specific template file, thanks :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h2> <br/>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 12 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.