3

I tried using the method described in one of the answers here: How to put user profile in a block

I created a block and used the following PHP code as a test; <?php render(user_view($account)); ?>

It didnt work. So the question remains, How can you create a block and call user profile fields into it using PHP?

5

render() only returns the string, it doesn't print it to the output. This code should work:

<?php print render(user_view($account)); ?>

Assuming of course that you have loaded the $account object. If not you probably want to use something like:

global $user;
$account = user_load($user->uid);

As an aside, using the PHP filter is considered bad practice (it uses eval() which is an inherent security risk).

The best way to get what you're looking for is to write a small custom module that implements hook_block_info() and hook_block_view():

function MYMODULE_block_info() {
  return array(
    'user_profile' => array(
      'info' => 'User Profile Block',
      'cache' => DRUPAL_NO_CACHE
    )
  );
}

function MYMODULE_block_view($delta = '') {
  $block = array();

  if ($delta == 'user_profile') {
    global $user;
    $account = user_load($user->uid);
    $block['subject'] = 'User Profile';
    $block['content'] = user_view($account);
  }

  return $block;
}

If your new to writing modules the best resource I know of is the Module developer's guide, and in particular the Creating Drupal 7.x modules sub-section; it will walk you through all of the steps necessary to get your module up and running.

UPDATE

Just to address your second comment...

I think the easiest way to get only certain fields to display would be to install the Entity View Modes module. Then you can create a custom view mode for the user entity (called block_profile or something), and in the "Manage Fields" admin page for users assign only the fields you want to be displayed to that new view mode.

Then you can pass the new view mode to the user_view() function:

$block['content'] = user_view($account, 'block_profile');

And voila, your block will contain only the fields that you've added to the new view mode through the admin UI.

If Entity View Modes is causing you issues you can just uninstall it, and add the new view mode yourself using hook_entity_info_alter() (again in your new custom module):

function MYMODULE_entity_info_alter(&$info) {
  $info['user']['view modes']['profile_block'] = array(
    'label' => t('Profile Block'), 
    'custom settings' => TRUE
  );
}
  • Awesome answer! Thank you! Can you elaborate a bit more on the 'module creation' process? Or, add a link to tutorial? Then this answer would be 100% solid. – Digital fire Apr 26 '12 at 17:07
  • Also, I see this line: $block['content'] = user_view($account); And cant help but wonder if it would be possible to specify fields instead of the entire profile? – Digital fire Apr 26 '12 at 17:10
  • I've updated the answer a bit; rendering certain fields requires a bit more effort but it's relatively easy. Hope it helps :) – Clive Apr 26 '12 at 17:17
  • When trying to use the Entity View Modes, I got the following error; Notice: Undefined index: #bundle in ds_build_load(). I know that it has nothing to do with the Entity View Mode module as its an error with DS. I will go about create the module for the user profile block. Once I finish with that I will post an update. – Digital fire Apr 26 '12 at 17:33
  • I've added a bit more...if that module is causing you problems you can add the view mode through code pretty easily. As you're already writing a module this might even be preferable to having another contrib module installed – Clive Apr 26 '12 at 17:39
0

You can print the user profile using:

<?php print render($user_profile); ?>

You can get specific user information by

<?php print render($user_profile['user_picture']); ?> 
<?php print render($user_profile['summary']); ?>
  • I tried <?php print render($user_profile); ?>. It gave me a blank block. I tried using print_r($user_profile) to check if anything was loaded and I get an error: Notice: Undefined variable: user_profile in eval(). – Digital fire Apr 26 '12 at 17:00
  • Sorry, the above code would only work at the theme level. – aerozeppelin Apr 26 '12 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.