1

I use the following snippet in template.php to access user profile fields on comment.tpl.php. $persianName works fine when the user is administrator, and returns nothing for the anonymous users. Actually, when I use dsm($variables) in anonymous viewing, I see that all UIDs are set to 0.

function apblog_preprocess_comment(&$variables) {
  $user = $variables['user'];
  $user = user_load($user->uid);
  $persianName = field_get_items('user', $user, 'field_user_pname');
  if ($persianName) {
    $variables['persianName'] = $persianName[0]['safe_value'];
  }
  else {
    $variables['persianName'] ='';
  }

  // …
}
  • May be, anonymous user has permission to post comments into your site, so you are not getting value for $persianName for particular comment which anonymous is posted into your site. – Jayendra Kainthola Sep 12 '13 at 8:48
  • What are you trying to show for anonymous users? Since they are anonymous, you don't know what user profile to reference. Your code should work for all authenticated users, though. – rocket Sep 14 '13 at 3:10
1

For Drupal, anonymous users are all associated with the user account having ID equal to 0. It is a particular account, since it doesn't have a user profile; if you try to access it, you get an Access Denied error. (I took the screenshot as user #1; seeing that account is not a matter of user permissions.)

screenshot

As consequence, the anonymous user account doesn't have any field attached to it, since you cannot edit it. That is understandable, as that account is referring to more than one user.

As side note, the $user variable you are accessing is referred to the user who is currently logged-in. If you want to get the user account for the user who wrote the comment, you should use the following code.

$account = user_load($comment->uid);
  • Thanks this worked for me, the $comment object returns the correct uid even if the user is anonymous. – Reza Shadman Sep 14 '13 at 5:20
1

In drupal by default anonymous users have UID=0 , and you just need if $user->uid==0 set your variable to what you want , some string like "mehman" ,

because of doing little changes in your code I use inverse checking of uid value ( the are as the same ,

if ( $user->uid ==0 )  $variables['persianName'] ='mehman';    

or

if ( $user->uid !=0 ) ...... else  $variables['persianName'] ='mehman';

)

function apblog_preprocess_comment(&$variables) {
  $user = $variables['user'];
  $user = user_load($user->uid);
  $persianName = field_get_items('user', $user, 'field_user_pname');
  if ($user->uid !=0) {
    $variables['persianName'] = $persianName[0]['safe_value'];
  }
  else {
    $variables['persianName'] ='';
  }

  // …
}

I hope this work to you

be win ;)

  • Dear zhilevan, This works for assigning a default value to $persianName for anonymous users, the problem was that i couldn't print the $persianName field of user profile entity for all user roles, it was just shown for admin role. – Reza Shadman Sep 14 '13 at 5:23
0

anonymous users with uid = 0 is default behaviour.

Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.