So here's what I'm trying to do. In my custom profile override, I have the following code, which displays a nice little sentence about how long the user has been a subscriber:

<?php if($account->profile_subscriber_length): ?>
<div class="profile-subscriber-length">I've been a subscriber for <?php print check_plain($account->profile_subscriber_length) ?></div>
<?php endif ?>

What I want to do is change the word "subscriber" to "editor" (as in "I've been an editor for TK years") just for the people on the site whose roles are "editor."

After much searching, I haven't come up with an answer. I've found a lot of snippets based on the "role," but can I put an addition php if statement inside of that, to accommodate the above (since it's possible that not everyone filled in that field)?

This, for instance, shows content if the user viewing the page has the correct role (obviously not quite what I want to do).

<?php if (in_array('super admin',$GLOBALS['user']->roles)): ?>
<div class="fields">Supper Administraator</div>
<?php endif; ?>

Anyone know how to do this? Many thanks in advance!

Comments

amandawolfe’s picture

no one? I hate to beg... :)

Anonymous’s picture

Hi, you would have to double check, but I use this code for a D5 site:

<?php
$user = user_load(array('uid' => $node->uid));
if ((in_array(4, array_keys($user->roles))!==FALSE)) {
	$role = 'Editor';
}?>

You would replace '4' with the value of your role. To find out the value simply browse to your roles list, hover over "Edit Permissions" and look for the number at the end of the url.

Hope this helps you out.

amandawolfe’s picture

Thanks Kristy! So after some trial and error, I got the basic mechanics working, whoo! Had to change to the $account variable for D6, but other than that it's pretty straightforward. Here's my question though. I need to print this php statement as $output.

<div class="profile-subscriber-length">I've been an editor for <?php print check_plain($account->profile_subscriber_length) ?></div>

Right now I'm just printing "HEY!" as a test. But when I put that php statement in, obviously I get an error. Is there a way to do this? I'm obviously no php whiz--just sort of blundering my way into things working. :) With the help of very kind people, of course!

<?php
$output = 'HEY!';
$account = user_load(array('uid' => $account->uid));
if ((in_array(5, array_keys($account->roles))!==FALSE)) {
    $role = 'Editor';
    print $output;
}?>
amandawolfe’s picture

Oops, spoke too soon! Should have had more faith in myself. :) Got the above working. Just ONE more sticky wicket and this is solved. Here it is. For the other users who aren't editors, I need it to say I've been a READER for... instead of EDITOR for... So I've got that bit of logic working beautifully, except the field isn't mandatory, so I need to include another "if" statement in case they didn't fill it in. (There are reasons that it has to be optional.) I keep getting errors when I try to add it with &&... the code for both is below. Top one works great, bottom one, not so much. Any ideas on what I'm doing wrong?

<?php
$output = "I've been an editor for ";
$account = user_load(array('uid' => $account->uid));
if ((in_array(5, array_keys($account->roles))!==FALSE)) {
    $role = 'Editor';
  print '<div class="profile-subscriber-length">';
  print $output;
  print check_plain($account->profile_subscriber_length);
  print '</div>';
}?>

<?php
$output = "I've been a subscriber for ";
$role = 'editor';
$account = user_load(array('uid' => $account->uid));
if ((in_array(5, array_keys($account->roles))!==TRUE)) && (($account->profile_subscriber_length)) {

  print '<div class="profile-subscriber-length">';
  print $output;
  print check_plain($account->profile_subscriber_length);
  print '</div>';
}?>

For the record I've tried (($account->profile_subscriber_length)), (($account->profile_subscriber_length)==""), and (($account->profile_subscriber_length)==TRUE) but the error I get is because of the &&.

Anonymous’s picture

I haven't tested this, but you could first check for the cck field value..

<?php if ($node->field_my_cck_field[0]['view']) {
$account = user_load(array('uid' => $account->uid));
if ((in_array(5, array_keys($account->roles))!==FALSE)) {
	$role = 'Editor';
}
  elseif ((in_array(2, array_keys($account->roles))!==FALSE)) {
	$role = 'Reader';
} 
 
print $role;
?>
Anonymous’s picture

Are you using the core profile module?? I would look at using the Content Profile Module which is the successor of Bio and Node Profile for D6.. it allows you to use CCK in profile.. which I find are easier to work with then the core profile.

amandawolfe’s picture

I am using core! I've based a fairly complex profile on it (basically every field is printed individually) so when I found content profile I made the decision that I wouldn't go back and redo everything. This was the one last sticky thing (which i realized almost as we're about to launch and editors are starting to register) and then hopefully it's all set. I hope.

Anyhow, thank you for all your help! That first bit of code was exactly the starting point I needed and couldn't find or come up with on my own!

amandawolfe’s picture

D'oh. So basically this whole thread is me talking to myself... Figured it out. Just needed an extra set of parenthesis. Posting the final code in case someone else ever wants to do this and is going crazy like me. :)

<?php
$output = "I've been an editor for ";
$account = user_load(array('uid' => $account->uid));
if ((in_array(5, array_keys($account->roles))!==FALSE)) {
    $role = 'Editor';
  print '<div class="profile-subscriber-length">';
  print $output;
  print check_plain($account->profile_subscriber_length);
  print '</div>';
}?>

<?php
$output = "I've been a subscriber for ";
$role = 'editor';
$account = user_load(array('uid' => $account->uid));
if (((in_array(5, array_keys($account->roles))!==TRUE)) && (($account->profile_subscriber_length))) {

  print '<div class="profile-subscriber-length">';
  print $output;
  print check_plain($account->profile_subscriber_length);
  print '</div>';
}?>