Hi All,

I am designing an "about me" block which will contain the avitar, name, location, and bio. The code for this type of block exists here: http://drupal.org/node/20165

 <?php
  if (arg(0)=='blog'){
    $uid = arg(1);
    $account = user_load(array((is_numeric($uid) ? 'uid' : 'name') => $uid, 'status' => 1));
    profile_load_profile(&$account);
    echo("<div align=\"left\">");
    if (isset($account->picture)){
      echo("
        <img style=\"float: left; padding: 2px\" src=\"$account->picture\" height=\"60px\">
      ");
    }
    if (isset($account->realname)){
      echo("
        <b>Name:</b><br>
        $account->realname<br>
      ");
    }
    if (isset($account->country)){
      echo("
        <b>Location:</b><br>
        $account->country<br>
        <br>
      ");
    }
    if (isset($account->biography)){
      echo("
        <br>
        <em>$account->biography</em><br>
        <br>
      ");
    }
    echo("
      </div>
      <div align=\"right\">
      <a href=\"http://www.yourdomainnamehere.com/user/$account->uid\">more</a>
      </div>
    ");
  }
?> 

My problem however, is that info in the about me block is based off which user is viewing the page, not off which user created the page. In other words, if I am looking at my friends blog on the site, his page would show an about me block with MY info in it instead of his.

How can I customize the code to show the "about me" info based off who created the page, not who's viewing the page?

THANKS!

-Dan

Comments

merlinofchaos’s picture

Replace

    $uid = arg(1);

with

    $uid = YOURUSERID;

(You can extract your uid from your my account page's URL)

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

potential’s picture

I don't think I want it to be specific to one particular user ID... I want it to be based off who's blog it is. So if I (username "dan") view brian's blog at www.website.com/blog/brian I would want the "about me" block to fill in brian's picture and information, not my own.

cel4145’s picture

I'd like to know how to do this to. This would be great for sites with multiple personal blogs. For instance, I have some class sites that could use it. The "About Me" block could just pull from an "About Me" field where the user could put in a blogroll, bio, whatever they want in that field.

merlinofchaos’s picture

Aha! Ok, that's not at all difficult.

  // This works because of the way aliases work.
  if (arg(1) == 'blog') {
    $uid = intval(arg(2));
  }

  // If not a user's blog, or is the top level blog page,
  // return and don't print a block.
  if (!$uid) {
    return;
  }

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

potential’s picture

Thanks for your help. Unfortunatly, still having trouble. I tried to implement the code you suggested as you can see here:

<?php
  if (arg(1)=='blog'){
    $uid = intval(arg(2));
  }
  if (!$uid) {
    return;
  }
$account = user_load(array((is_numeric($uid) ? 'uid' : 'name') => $uid, 'status' => 1));
    profile_load_profile(&$account);
    echo("<div align=\"left\">");
    if (isset($account->picture)){
      echo("
        <img style=\"float: left; padding: 2px\" src=\"$account->picture\" height=\"60px\">
      ");
    }
    if (isset($account->realname)){
      echo("
        <b>Name:</b><br>
        $account->realname<br>
      ");
    }
    if (isset($account->country)){
      echo("
        <b>Location:</b><br>
        $account->country<br>
        <br>
      ");
    }
    if (isset($account->biography)){
      echo("
        <br>
        <em>$account->biography</em><br>
        <br>
      ");
    }
    echo("
      </div>
      <div align=\"right\">
      <a href=\"http://www.yourdomainnamehere.com/user/$account->uid\">more</a>
      </div>
    ");
  }
?> 

What did I do wrong?

merlinofchaos’s picture

I think I may have put arg(1) where I should have put arg(0) and arg(2) where I should have put arg(1). Sorry about that.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

merlinofchaos’s picture

Wait a minute. The code I wrote looks like it's actually very much the exact same code as what you quoted.

Now I'm very confused as to why you're getting YOUR profile out of that when you should be getting the profile of the person whose blog you're looking at. Mulling this over some.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

potential’s picture

I wasn't clear. When I use the code I last posted, i get this error in the block:

Parse error: parse error, unexpected '}' in F:\www\htdocs\includes\common.inc(1857) : eval()'d code on line 42

merlinofchaos’s picture

Ok, so I tried cut & pasting that code (being the code you initially posted). Other than a minor error in the code (There should not be a & in front of $account) it actually works perfectly for me, and load the account for the user whose blog I'm looking at.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

potential’s picture

Yeah, geez, I'm sorry for all the confusion. I have it working now using the original code. Thanks for your help Merlin.

I do have one other question for you. I'm also trying to make a block on each blog only visable to the creator of that blog. Other users would not see this block unless they were on their own blog.

merlinofchaos’s picture

/*
 * This snippet, intended for a block, will display nothing
 * if the current user is not the owner of the blog being
 * viewed.
*/
  if (arg(0) != 'blog'))
    return;
  
  global $user;
  if (!$user->uid)
    return;
  if (arg(1) != $user->uid)
    return;

  // Insert code here, or HTML after the 

.
?>

Aside from any typos I may've made, that ought to do the trick.
-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

potential’s picture

Perfect. you're the man. I'll give it a try tonight

thinkinkless’s picture

I stumbled across this thread just as I was getting totally frustrated trying to implement a blogroll based on a user profile field. This code works perfectly!

Here is the trimmed-down version I used in a block to display the user's blogroll (textarea in the profile that allows filtered html).

<?php
  if (arg(0)=='blog'){
    $uid = arg(1);
    $account = user_load(array((is_numeric($uid) ? 'uid' : 'name') => $uid, 'status' => 1));
    profile_load_profile($account);
    echo("<div style=\"text-align: left;\">");
    if (isset($account->profile_blogroll)){
print check_output($account->profile_blogroll);
    }
else print "no links added";
  }
?>
merlinofchaos’s picture

There's a section in the handbook about PHP Snippets. You should repost this there, explaining what it does.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

thinkinkless’s picture

Added here:
http://drupal.org/node/39858

I'll post more as I do more blogcentric stuff. I will also have lots of user profile stuff to share.

cel4145’s picture

Just tried it. This is something I have wanted. Thanks a bunch :)

thinkinkless’s picture

Hey cel4145, I forgot to close the div in the initial code I submitted. If you style the block it will have issues.
sorry!
The book page has been edited.

Glad it helped, though!

sidenote: does anyone know why the book page
http://drupal.org/node/39858
does not show up on the list for php block snippets?

cel4145’s picture

Thanks. I updated the block to use the new version that does not have the div.

As to your sidenote, pages do not show up in the book navigation while they await moderation. It's been approved now, so it shows up. If you ever need other book pages approved, feel free to contact me.

daniel read’s picture

(Updating this comment a few hours after I originally posted it--after I thought about it I figured out what the disconnect was.)

It took me a moment to figure out that the /blog/ path referred to in this thread points to the home page of the blog, whereas /node/ is used on the individual posts. So I can see how this code that looks for 'blog' in arg(0) and the user id in arg(1) would work.

My initial confusion arose because my goal was different: to find a way to pull from a profile field based on the author of a node, and not only on the main blog page for a user.

Does anyone know of a technique to accomplish this same thing under that circumstance? In other words:

- determine author of post
- load author's user profile
- print profile_blogroll field

I can use the settings on the block configuration page to control which page types this block appears on.

Thanks,
Dan

brenda003’s picture

Have you found an answer? I've also been trying to figure this out. I need the block to appear on each blog with an "About Me", not in their list of blogs page.

merlinofchaos’s picture

This is a quick piece and may need to be cleaned up.


  if (arg(0) == 'node' && is_numeric(arg(1)) {
    // This line works for Drupal 4.7; if using 4.6 uncomment the line below it and comment this one.
    $node = node_load(arg(1));
//    $node = node_load(array('nid' => arg(1)));
    if ($node->type != 'blog') {
      return;
    }
    $user = user_load(array('uid' => $node->uid));
  }
  if (arg(0) == 'blog' && is_numeric(arg(1)) {
    $user = user_load(array('uid' => arg(1)));
  }
  if (!$user) {
   return;
  }

  // ... insert your snippet here. $user is loaded; if it's a blog node, $node is also loaded.

}

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

brenda003’s picture

I still can't get this to work. I try so many things but nothing shows up in the block at all, and yes I have it turned on. A bit stumped here, can someone please help?

brenda003’s picture

I ended up using the patch here to the profile module.

sulleleven’s picture

Brenda,
this patch.... does it allow for display on the blog page or only blog node page?

brenda003’s picture

Unfortunately only blog node page. I've been trying to find a way to get it to display on both with little luck as of yet.

Ironically the blogroll module I want to use only displays on the blog page and NOT the blog node page. Go figure.

merlinofchaos’s picture

An imbalanced parens and nobody can figure this out? I'm not sure people should be messing with PHP snippets if you're not willing to learn the basics of how the language works.

Anyway, here's the cleaned up snippet that actually works:

  if (arg(0) == 'node' && is_numeric(arg(1))) {
    // This line works for Drupal 4.7; if using 4.6 uncomment the line below it and comment this one.
    $node = node_load(arg(1));
// $node = node_load(array('nid' => arg(1)));
    if ($node->type != 'blog') {
      return;
    }
    $user = user_load(array('uid' => $node->uid));
  }
  if (arg(0) == 'blog' && is_numeric(arg(1))) {
    $user = user_load(array('uid' => arg(1)));
  }
  if (!$user) {
   return;
  }
  // ... insert your snippet here. $user is loaded; if it's a blog node, $node is also loaded.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

Guy Shepperd’s picture

I wanted to say thanks for the snippet.

I have modified it a little to make it work for me, I now have it work ing on all pages except Forum posts.

here is what I have working.

<?php

    $uid = arg(1);
    $user = user_load(array((is_numeric($uid) ? 'uid' : 'name') => $uid, 'status' => 1));
    profile_load_profile(&$user);
    if (arg(0) == 'node' && is_numeric(arg(1))) {
    // This line works for Drupal 4.7; if using 4.6 uncomment the line below it and comment this one.
    $node = node_load(arg(1));
// $node = node_load(array('nid' => arg(1)));
    if ($node->type != 'blog') {
      return;
    }
    $user = user_load(array('uid' => $node->uid));
  
  }
  if (arg(0) == 'blog' && is_numeric(arg(1))) {
    $user = user_load(array('uid' => arg(1)));
  }
    if (arg(0) == 'forum' && is_numeric(arg(1))) {
    $user = user_load(array('uid' => arg(1)));
  }
  if (!$user) {
   return;
  }

  // ... insert your snippet here. $user is loaded; if it's a blog node, $node is also loaded.

  if (isset($user->name)){
      echo("
	  <b>UserID:</b>$user->name<br><br>
        <b>Name:</b><br>
        $user->profile_name<br>
		<b>Location:</b><br>
        $user->profile_Location<br>
        <br>
		<b>Country:</b><br>
        $user->profile_country<br>
        <br>
		<b>Website:</b><br>
        <br>
      ");
    }
?> 

any suggestions would be greatly apprecaited.

thanks

Guy

brenda003’s picture

Drop this perhaps:

    if ($node->type != 'blog') {
      return;
    }

Looks good. Thanks.

Guy Shepperd’s picture

that was it. I went in removed that section,

then put dont show on the top level nodes.and vola!

apprecaite the help!

Thanks again!