Hey Folks,
I'm trying to create a block in my sidebar that shows the current users avatar (profile pict) or the default avatar for those not logged in.
I found this snippet
<?php
global $user;
if ($user->uid) {print ("<br /><img src=\"$user->picture\"><br /><br />");}
else print ("<a href=\"/?q=user/register\"><img src=\"sites/default/files/images/Avatar.png\"></a>");
?>somewhere in the forums here, and while it seems to work, on certain pages the images comes up as broken.
For example, it works here:
http://www.ecoevolution.org/econews
but not here:
http://www.ecoevolution.org/news/coal_waste_threat_us_security_says_dept...
If I open the image, I notice that for some reason the broken image has the section address tacked onto it, which breaks the link. So instead of sites/default/files/images/Avatar.png it is changing it to news/sites/default/files/images/Avatar.png. It does this for any article or node beneath the main sections of my site.
Is there something in the way the snippet is structured that is causing this? I don't see anything wrong with it, but then I am no php expert. Or should I be looking into another way to achieve the same goal?
Comments
Try using base_url()
Does using base_url() work:
else print ('<a href="/?q=user/register"><img src="'. base_url() .'/sites/default/files/images/Avatar.png></a>');Hmm, still no
changing it to
else print ('<a href="/?q=user/register"><img src="'. base_url() .'/sites/default/files/images/Avatar.png></a>');didn't seem to have any effect..
I should add that even the Profile picture of a logged in user disappears when accessing these nodes, so it doesn't seem limited to the default avatar image.
_
Try this:
<?phpglobal $user;
if ($user->uid) {
print '<br /><img src="/' . $user->picture . '"/><br /><br />';
} else {
print l('<img src="/sites/default/files/images/avatar.png" />', 'user/register', array ('html'=>TRUE));
}
?>
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
Still no Joy
Using that snippet makes the logged in profile picture disappear completely, and it is still adding the section to the file name address.
It's really weird, I don't know why it is sticking the parent node location onto the file name. That seems to be the root of the problem. It should just be using $user->picture and sites/default/..., but something is causing it to add the page path to the file path.
_
hmm.. weird. I tested it and it works for me. What do you get with the following:
<?phpglobal $user;
print $user->picture;
?>
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
Double-weird If I print just
Double-weird
If I print just the $user-picture like you said, the picture disappears from the working pages and it actually prints in text the path instead of the picture in the block for the logged in user.
_
If i understand you correctly, that's correct-- $user->picture should be the path to the pic of the logged in user. What does it actually print though?
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
It doesn't actually print the
It doesn't actually print the picture of the logged in user like it should. It actually writes out the path in text on the pages where it didn't work before, as if the php input filter was on. So instead of an image, it is writing out "sites/default/files/userpictures/picture-1.jpg" in text where the picture would be.
Seems more like a bug of some sort (or User error on my part ;) ) than a problem with your code, but I can't seem to track down a way around it.
_
that's my point-- afaik, $user->picture is supposed to be the path, not the picture. So surrounding it with the proper
<img>tag should do the trick. The code I posted above works perfectly on my site-- not sure what the problem could be._
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
Yeah, I think there is
Yeah, I think there is something buggy going on. Like you said, it should be working, as it clearly works elsewhere (your site for example). I'm going to trouble shoot it a bit more, or maybe look into using views to do the same thing somehow.
_
Can you access the pictures directly by typing the proper address in the url?
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
Sub Page Shenanigans
Yeah, the image actually appears on most pages, and when it does, the url is the proper one. It appears to be just a quirk of these sub-pages, which is annoying because that is where all the specific content I have falls. So if a person follows a link to the actual content to read the story, their profile pict disappears.
The url is the right one, the block is the same for every page, but Drupal or a module is changing the url when it appears on certain pages.
Finally figured it out!
OMG, finally!
Got it to work. The problem was the file path after all. While "sites/default/files..." worked on all the main pages, the change I needed to make was to "/sites/default/files..." Forcing it to lock onto the right directory with an additional forward slash cleared up the issue. It is in the code Worldfallz posted but it wasn't working before. For what reason it accepted it on some pages but not others I have no idea...
Now both versions of the code, mine or Worldfallz works.
Let the dance of victory begin...
::edit::
Victory dance delayed, default avatar shows up now, but logged in user avatar still MIA. One down one to go I guess.
_
hmmm... that's exactly what the code I posted above does...should have worked.
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
Got it now
<?phpglobal $user;
if ($user->uid) {
print '<br /><img src="/' . $user->picture . '"/><br /><br />';
}
else print ("<a href=\"/?q=user/register\"><img src=\"/sites/default/files/images/Avatar.png\"></a>");
?>
This seems to work now. Used your $user->picture code, the code from the original post from the forums (I think) and fixed the file name. Obviously I suck at this, I think my confusion came in because I was going in between three different versions of the code, plus trying to keep the original block open in a tab in case the php whitescreened my site.(<-- That's a Pro tip for anyone messing with uncertain php in a block BTW.)
So for anyone else looking for this, here is the code wrapped in a div with the class "userimage" for targeting with CSS
<div class="userimage"><?phpglobal $user;
if ($user->uid) {
print '<br /><img src="/' . $user->picture . '"/><br /><br />';
}
else print ("<a href=\"/?q=user/register\"><img src=\"/path-to-your-file-HERE\"></a>");
?></div>
Big thanks again Worldfallz!