I'm trying to do what I thought would be a simple thing, but I'm finding that it's not working. I want to display "Verified Member" on a user's profile page if they are a member of the "verified user" role that I've created. Basically, I want to tell everyone who views this user's page that the user has verified him/herself.

I've tried to create a custom content pane using the following PHP in Panels 3.

<?php
$verified_role = array('verified user');

  $node = node_load(array('nid' => arg(1)));
  $author = user_load(array('uid' => $node->uid));

  if (count(array_intersect($verified_role, $author->roles)) > 0 ) {
   $verified_message = 'Verified Member';
  } else {
    $verified_message = 'Unverified Member';
  }

echo $verified_message;
?>

This code works when I add it to a standard page node. If the author of that node is assigned the "verified user" role, "Verified Member" is displayed on the page. If not, "Unverified Member" is displayed. When I add this to a pane on my user profile template, I see "Verified Member" every time, whether or not the user/author of that profile page is assigned the role.

I wanted to test this by created a computed field on the user profile content type. In that computed field, I used the following code. I then added this field to the user profile panel like any other field would be added. Same issue. "Verified Member" is always displayed regardless of whether or not the user has that role assigned.

$verified_role = array('verified user');

  $node = node_load(array('nid' => arg(1)));
  $author = user_load(array('uid' => $node->uid));

  if (count(array_intersect($verified_role, $author->roles)) > 0 ) {
   $node_field[0]['value'] = 'Verified Member';
  } else {
   $node_field[0]['value'] = 'Unverified Member';
  }

(I did a little more testing and I think the user ID being returned when viewing a user's profile page is 1, even though the ID of the user whose profile I'm looking at is 3.)

The simple question is: How can I add a message, based on the role of the user whose profile page is being viewed, to that page? has anyone done this? Can a code snippet be provided?

Thanks for any help with this. I'm going crazy trying to figure it out.

Comments

dmetzcher’s picture

I did some testing (and a little educating myself) and I think I've figured it out.
First, this line...
$author = user_load(array('uid' => $node->uid));
...was the problem.

When grabbing the user ID of a profile node (Content Profile module) controlled by the Panels module, using $uid = arg(1); will get the correct user ID from the URL. That's what I needed. (I think, based on my testing, that the line above was returning "1" for the user ID, which is certainly not correct. This has something to do with the way Panels works, but that's above my head right now, so I can't explain why.)

The code below will work. Simply adding new content to a panel (and selecting "New custom content" on the window that will appear) and using this code, I can display "Verified Member" if the user is a member of the "verified user" role, and "Unverified Member" if not. At first, I was trying this with a Computed Field (CCK module with Computed Field module installed) and NOT storing the value in the database. That didn't seem to matter, however. It will not update the value unless the profile is edited and saved after the role is added or removed, so doing it all in Panels seems to be the way to go (unless someone else has a better idea, and I'd love to compare notes and hear from others on this).

Here's the code to enter into the custom content in a Panel that controls your user profile pages:

<?php
$verified_roles = array('verified user'); // Role names in quotes, separated by commas.
$uid = arg(1); // Get the user ID from the URL of the profile being viewed --> arg(1).
$author = user_load(array('uid' => $uid));

if (count(array_intersect($verified_roles, $author->roles)) > 0 ) {
 $node_field[0]['value'] = 'Verified Member';
} else {
 $node_field[0]['value'] = 'Unverified Member';
}
?>

By the way, I don't think this is a Content Profile issue at all... it's a Panels thing. If anyone knows differently, please clarify. I'm closing this ticket.

dmetzcher’s picture

Status: Active » Closed (fixed)
fabrizioprocopio’s picture

where can I find a module to show the role(s) of one user in his profile page? (not in block or pane but in his page)

dmetzcher’s picture

@fabrizioprocopio: You're looking for something a little different than what was addressed by this issue. You should post your question to the Post Installation forum, since it's not related to a specific module. This issue is closed, so no one else (other than me) is likely to see your question here. :-)

fabrizioprocopio’s picture

thanks you for advice
sorry for posting in the wrong place
:)

pgrant41’s picture

As an extra note, if you are using Drupal 6, to get this code to work you will have to enable the 'PHP Filter' module under the 'Core-optional' section on your modules page. When you enable it, do not give permission to any other roles to use it, the admin user is given permission by default, giving permission to anybody else could cause serious safety issues for your site.