Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Draven_Caine on
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
Hi Draven, thanks for taking
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:
I hope that helps.
Error
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
Look at this line: $link =
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);
Very cool
Thanks for the update, it works the same as my script from the first post just a little cleaner. ty everyone
Oops, I did see that, but
Oops, I did see that, but somehow forgot to fix it before posting the code :-/
Thanks for this important finishing touch.
Hi, Can you put all kinds of
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
profile stuff links
What profile things did you want to add?
updated custom hello user block
This block renders
food for thought
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):
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.
hmmm ...
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.
I understand the intentions
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.
RE: Untested Code
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:
It didn't create a block title for the list view. To fix that, instead of:
I used
also, I got rid of the title, changing:
to
Also, the English Major in me says, instead of
use
And finally, it helped to include hello_user.info :
REMEMBER: Ich bin eine coding noob.
--
Bram Moreinis
Greenfield Digital
http://greenfielddigital.com
more improvements
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:
(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:
Configuration form
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.
Without this, I was getting errors trying to access block's configuration page.
Module update
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.
alittle to add
For user points, it only desplays the default catagory .
Did you create this module yet?
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
I got this code to work just
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:
Your code looks fine. Does
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.
thanks
Thanks for the quick reply. I found the source of the error. In my page.tpl there is this line of code:
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.
And for Drupal 7 ?
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 :
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
block_example.module
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.
Thanks a lot. I try it and
Thanks a lot. I try it and come back...