http://drupal.org/node/395578 page New Book Page!!!

I use a custom php block i kinda put together from bits i found on the forum, had to upgrade and work out old versions of drupal from some of the bits.
my purpose is to show a block after the user logs in the log-in block disappears and this block appears to say "hello user, with thier picture and account link (maybe more links to come soon).

<?php
global $user;
if ($user->uid) {
    $name = $user->name;
    print "<h3>Welcome back <i>{$name}</i>!</h3><br />";
}
?>
<img src="/<?php
global $user;
if ($user->picture) {
   print "$user->picture";
   }
else { 
   print "misc/default_profile_image.jpg";
   }
?>">
<br/>
<?php
global $user;
if ($user->uid) {
    $name = $user->name;
    $id = $user->uid;
    print  l('Go to your account', "user/$id");
}
?>

My reason for posting this is 2 things

  • This way so others can find the code if they want
  • Also i was looking for some help with making this script simplier, i am not very good with php but i know enough to alter scripts for my pupose.

Comments

marcvangend’s picture

Hi Draven, thanks for taking the time to share your code.

Your code can be a little cleaner. First of all, you only have to call global $user only once within each function.
Second, the way you insert the image url into the img tag looks a little messy.
Third, I used if ($user->uid) at the beginning to make sure the rest of the code is only run if there is a logged-in user.
Fourth, I wrapped 'Go to your account' in the t() function. It's good practice to do this with all strings that are shown to a user. See http://api.drupal.org/api/function/t/6 for more info.
Last but not least, instead of using <br /> for your lay-out, use paragraphs and css for styling. This will also improve the validity of your HTML.

Personally, I like to define all needed values first and then create the output. So, my version would be:

<?php
  global $user;
  if ($user->uid == 0) { // uid == 0 means user is anonymous
    return;
  }
  $name = $user->name;
  if ($user->picture) {
    $picture = $user->picture;
  }
  else { 
    $picture = 'misc/default_profile_image.jpg';
  }
  $link = l(t('Go to your account'), 'user/'.$user->uid;)
?>

<div id="welcome-user-block">
  <h3>Welcome back <i><?php print $name; ?></i>!</h3>
  <p>
    <img src="/<?php print $picture; ?>" alt="user picture" />
  </p>
  <p>
    <?php print $link; ?>
  </p>
</div>

I hope that helps.

Draven_Caine’s picture

Parse error: syntax error, unexpected ';' in /home/websites/draven/public_html/includes/common.inc(1648) : eval()'d code on line 13

from the following code

<?php
  global $user;
  if ($user->uid == 0) { // uid == 0 means user is anonymous
    return;
  }
  $name = $user->name;
  if ($user->picture) {
    $picture = $user->picture;
  }
  else {
    $picture = 'misc/default_profile_image.jpg';
  }
  $link = l(t('Go to your account'), 'user/'.$user->uid;)
?>

<div id="welcome-user-block">
  <h3>Welcome back <i><?php print $name; ?></i>!</h3>
  <p>
    <img src="/<?php print $picture; ?>" alt="user picture" />
  </p>
  <p>
    <?php print $link; ?>
  </p>
</div>
Anonymous’s picture

Look at this line: $link = l(t('Go to your account'), 'user/'.$user->uid;)

the semi colon should be after the closing parenthesis:

$link = l(t('Go to your account'), 'user/'. $user->uid);

Draven_Caine’s picture

Thanks for the update, it works the same as my script from the first post just a little cleaner. ty everyone

marcvangend’s picture

Oops, I did see that, but somehow forgot to fix it before posting the code :-/
Thanks for this important finishing touch.

summit’s picture

Hi,

Can you put all kinds of profile stuff in this block also. Interested in his last comments for instance?
Thanks a lot in advance for your reply!

Greetings,
Martijn
www.trekking-world.com

Draven_Caine’s picture

What profile things did you want to add?

Draven_Caine’s picture

This block renders

  • Signups module
  • IM_paypal module
  • User account (core module)
<?php
  global $user;
  if ($user->uid == 0) { // uid == 0 means user is anonymous
    return;
  }
  $name = $user->name;
  if ($user->picture) {
    $picture = $user->picture;
  }
  else {
    $picture = 'misc/default_profile_image.jpg';
  }
  $link = l(t('Go to my account'), 'user/'.$user->uid);
  $signups = l(t('Go to my sign-ups'), 'user/'.$user->uid/signups);
?>

<div id="welcome-user-block">
  <h3>Welcome back <i><?php print $name; ?></i>!</h3>
  <p>
    <img src="/<?php print $picture; ?>" alt="user picture" />
  </p>
  <p>
    <?php print $link; ?><br />
    <?php print $signups ?>
  </p>
</div>
<?php
if (function_exists('lm_paypal_donate')) {
  
  // The amount is a one element array so an text input with the one value as
  //  default
  print 'If you would like to help support www.Shattered-Broken.com write in your own amount' .
    lm_paypal_donate(array(5), 'USD', 'donation to example.com') . '<br/>';
  // The amount is a multi element array so a select will be used. Note if one
  //   of the elements is itself an array that will be the default selection
  // The final two parameters are an alternative button (here we use the default)
 
}
?>
marcvangend’s picture

As you all can see above, it's very easy to expand on this code and add all kinds of data. I think it should be mentioned that there is a downside to this. When you read this article: http://2bits.com/articles/free-your-content-php-moving-php-code-out-bloc..., you will learn why it is in fact not ideal to store php code in the database. The alternative is to write a hello user module, which would look something like this (untested code):

<?php
/* implementation of hook_block() */
function hello_user_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'hello user'       => t('Hello user block'),
      );
      return $blocks;
    case 'view': default:
      if($delta == 0) {
        $block['subject'] = t('Title of block #1');
        $block['content'] = hello_user_block_contents();
      }
      return $block;
  }
}

/* implementation of hook_theme() */
function hello_user_theme() {
  return array(
    'hello_user_block' => array(
      'arguments' => array('vars'),
    ),
  );
}

/* collect data for the block */
function hello_user_block_contents() {
  global $user;
  if($user->uid == 0) {
    return;
  }
  $vars['name'] = $user->name;
  if ($user->picture) {
    $vars['picture'] = $user->picture;
  }
  else {
    $vars['picture'] = 'misc/default_profile_image.jpg';
  }
  $vars['uid'] = $user->uid;
  return theme('hello_user_block', $vars);
}

/* Generate the block's html output in a theme function, to allow theme overrides */
function theme_hello_user_block($vars) {
  $output  = '<div id="welcome-user-block">';
  $output .= '<h3>Welcome back <i>'.$vars['name'].'</i>!</h3>';
  $output .= '<p><img src="'.$vars['picture'].'" alt="user picture" /></p>';
  $output .= '<p>'.l(t('Go to your account'), 'user/'.$vars['uid']).'</p>';
  $output .= '</div>';
  return $output;
}
?>

This code should be put in a file called hello_user.module, in a folder called hello_user. You should also put a simple hello_user.info file in the same folder, see http://drupal.org/node/231036. When uploaded to your modules folder, it will show up on the modules mapge. When the module is enabled, the block will show up on the blocks page.

Draven_Caine’s picture

Please refer to the first post, this is ment as a snippit, info, help, ect.
Yes this can be made into a module but the issue with that as posted was if someone wanted to add other links it becomes diffuclt for some of the non-programers.

My reason for posting this is 2 things

* This way so others can find the code if they want
* Also i was looking for some help with making this script simplier, i am not very good with php but i know enough to alter scripts for my pupose.

marcvangend’s picture

I understand the intentions of your initial post. I wrote a comment about turning it into a module:
- because I hope that it helps people understand Drupal modules;
- as 'food for thought', because there are good reasons not to store php code in the database (which is what your code is meant for);
- to encourage people to create modules; myself, I didn't consider creating my own modules for a long time, because I thought it would be more difficult than it eventually turned out to be.

I guess our posts share the purpose of providing reusable code, info and help. If I can help you understanding the module code, please say so.

bramface’s picture

Okay, I tested the module code. I am not a coder, so this is all guesswork on my part.
The "Configure" option creates this error:

Fatal error: Cannot use string offset as an array in includes/form.inc 

It didn't create a block title for the list view. To fix that, instead of:

 case 'list':
      $blocks[0] = array(
        'hello user'       => t('Hello user block'),
      );

I used

    case 'list':
    $blocks[0]['info'] = t('Hello User');
    return $blocks;

also, I got rid of the title, changing:

$block['subject'] = t('Title of block #1');

to

$block['subject'] = t('');

Also, the English Major in me says, instead of

  $output .= '<h3>Welcome back <i>'.$vars['name'].'</i>!</h3>';

use

  $output .= '<h3>Welcome back <i>, '.$vars['name'].'</i>!</h3>';

And finally, it helped to include hello_user.info :

; $Id: hello_user.info,v 0.1 $
name = Hello User
description = Creates a Hello User block.
package = Other 

; Information added by drupal.org packaging script on 2007-01-20
project = "hello user"

core = 6.x

; Information added by drupal.org packaging script on 2008-04-29
version = "6.x-0.1"
core = "6.x"
project = "hello user"

REMEMBER: Ich bin eine coding noob.

--
Bram Moreinis
Greenfield Digital
http://greenfielddigital.com

marcvangend’s picture

Well, for a coding noob, you did great! :-)

You're right about $blocks[0]['info'] = t('Hello User');. I don't know where I got that code I wrote before...

If you want to get rid of the title, I would suggest that you keep a default title (ie. $block['subject'] = t('Hello!');) and override the title with '<none>' on the block configuration page.

The punctuation noob in me says that the space should be after the comma ;-) But of course it would be even better to make this text translatable:

$output .= '<h3>' . t( 'Welcome back, %name!', array('%name' => $vars['name']) ) . '</h3>';

(Without the php tags of course, they are just for the syntax highlighting.)

Obviously, the .info file is necessary if you want to get this module to work. However you don't need the stuff that says "Information added by drupal.org packaging script" (see also http://drupal.org/node/231036). This will do just fine:

; $Id$
name = Hello user
description = "Creates a 'Hello user' block."
core = 6.x
michlis’s picture

Thanks for sharing this useful module!

Small tip, in order to disable default block title, as marcvangend suggests, you need to setup the "configure" option in block_hook(). Just add another case to switch statement which will return empty array.

case 'configure':
return array();

Without this, I was getting errors trying to access block's configuration page.

Draven_Caine’s picture

Due to so many users messaging me i have made this into a module for easy use. I will be posting this very soon in the drupal.org downloads.

Thank you for everyone that said kind things about this work, it is heart warming.

Draven_Caine’s picture

<?php
  global $user;
  if ($user->uid == 0) { // uid == 0 means user is anonymous
    return;
  }
  $name = $user->name;
  if ($user->picture) {
    $picture = $user->picture;
  }
  else {
    $picture = 'misc/default_profile_image.jpg';
  }
  $my_account = l(t('My Account'), 'user/'.$user->uid);
  $admin = l(t('Admin'), 'admin');
  $create_content = l(t('Add Content'), 'node/add');
  $logout = l(t('Logout'), 'logout');
?>

<div id="welcome-user-block">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2" align="center" style="font-size: large;">Welcome back <i><?php print $name; ?></i>!</td>
      </tr>
      <tr>
        <td rowspan="4"><div class="rtecenter"><img src="/<?php print $picture; ?>" alt="user picture" height="80" /></div></td>
        <td><?php print $my_account; ?></td>
      </tr>
      <tr>
        <td><?php print $admin; ?></td>
      </tr>
      <tr>
        <td><?php print $create_content; ?></td>
      </tr>
      <tr>
        <td><?php print $logout; ?></td>
      </tr>
      <tr>
        <td colspan="2" align="center"><b><i>You now have <?php print userpoints_get_current_points($user->uid); ?> User Points.</b></i></td>
      </tr>
    </table>
</div>

For user points, it only desplays the default catagory .

bassplaya’s picture

I was searching in drupalmodules.com but I couldn't find your module or any module that could show those things..

I hope you could provide it very soon ;-)
Thanx for the good work !

Authentically,
BassPlaya

rick@pixeldirt’s picture

I got this code to work just fine. It prints the picture, my profile, and logout just like I need it to, however, when I click on logout out it hangs the site and you can't get back in. The screen goes blank except for the header and the only way to get back in is to completely remove the code.

Any ideas what is going on???

Thanks

Here is what I used:

 <?php
  global $user;
  if ($user->uid == 0) { // uid == 0 means user is anonymous
    return;
  }
  $name = $user->name;
  if ($user->picture) {
    $picture = $user->picture;
  }
else {
    $picture = 'misc/default_profile_image.jpg';
  }
  $link = l(t('My Profile'), 'user/'.$user->uid);
 $logout = l(t('Logout'), 'logout');
?>



<div id="loginpic">
  Hey, <?php print $name; ?><img src="/<?php print $picture; ?>" alt="user picture" height="50" align="left" /><br>
  <?php print $link; ?><br>
 <?php print $logout; ?>  
  
  
</div>
marcvangend’s picture

Your code looks fine. Does the error also occur when you type www.yoursite.com/logout in the address bar? I suspect that the error is not related to the helloworld block.

rick@pixeldirt’s picture

Thanks for the quick reply. I found the source of the error. In my page.tpl there is this line of code:

<!-- REMOVE these if you are using jquery-update!! -->
  <script type="text/javascript" src="<?php print $base_path ?>misc/collapse-fix.js"></script>
  <script type="text/javascript" src="<?php print $base_path ?>misc/compat-1.0.js"></script>

I am using jquery update so I removed it and I thought everything looked fine. Then I had the problem with the logout and I noticed my node admin links weren't working either. So, I commented it out again and everything is working perfect.

I'm not sure what is wrong with that javascript code but it was already in the drigg file so I'm just leaving it commented out.

Thanks again for checking out the code.

Gilles G.’s picture

Hi people,

I'm a new user of Drupal 7 and I'm interested in this module with the name and the image of the user once connected.

I managed to create the module, with in the file hello_user.module :

/**
* Implements hook_block_info().
*/
function hello_user_block_info() {
  $blocks['hello']['info'] = t('Hello User');
  $blocks['hello']['cache'] = DRUPAL_NO_CACHE;
  $blocks['hello']['properties']['administrative'] = TRUE;
  return $blocks;
}

It seems good in the available blocks (Admin > Structure > Blocks). But how return all the visible elements with the appropriate syntax to Drupal 7 ? It does not work with the higher available code...

Thanks to you. Bye

marcvangend’s picture

Perhaps the block_example module can show you how hook_block works in Drupal 7. Check out the code at http://api.drupal.org/api/examples/block_example--block_example.module or download it at http://drupal.org/project/examples.

The biggest change is that hooks like hook_block($op) no longer exist; they are replaced by a family of hooks, one for each option (hook_block_info(), hook_block_save(), hook_block_view() etc.). See also http://drupal.org/node/224333#remove_op.

Gilles G.’s picture

Thanks a lot. I try it and come back...