Hi all,

i have tried to print out user picture(avatar) as a menu icon for a few hours. It works well with custom menu but not with superfish menu.

I used this code:

function OMEGASUBTHEME_menu_link(array $variables) {
  global $user;
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $title = '';
  // Check if the user is logged in, that you are in the correct menu,
  // and that you have the right menu item
  if ($user->uid != 0 && $element['#theme'] == 'menu_link__user_menu' && $element['#title'] == t('My account')) {
    $element['#title'] = $user->name;
    // Add 'html' = TRUE to the link options
    $element['#localized_options']['html'] = TRUE;
    // Load the user picture file information; Unnecessary if you use theme_user_picture()
    $fid = $user->picture;
    $file = file_load($fid);
    // I found it necessary to use theme_image_style() instead of theme_user_picture()
    // because I didn't want any extra html, just the image.
    $title = theme('image_style', array('style_name' => 'thumbnail', 'path' => $file->uri, 'alt' => $element['#title'], 'title' => $element['#title'])) . $element['#title'];
  } else {
    $title = $element['#title'];
  }
  $output = l($title, $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

from here

What did i wrong? Can you help me please? Thanks :-)

CommentFileSizeAuthor
#2 current user picture token error.png4.23 KBFulgrim

Comments

mehrpadin’s picture

Hey there,

Have you tried the Menu Tokens module? http://drupal.org/project/menu_token

Fulgrim’s picture

StatusFileSize
new4.23 KB

thanks for your quick answer.

i tried menu token but i does not work. If i put [current-user:picture] token into title i just get text instead of a image displayed ^^

It looks like this:

<div class="user-picture"> <a href="/testumgebung/users/admin" title="Benutzerprofil anzeigen."><img src="http://localhost/testumgebung/sites/default/files/styles/thumbnail/public/pictures/picture-1-1348655058.jpg" alt="Bild des Benutzers admin" title="Bild des Benutzers admin" /></a> </div> <http://localhost/testumgebung/node/add>

I´ve found a solution with http://drupal.org/project/menu_views to get the user picture and user name in menu link but it's not exactly what I'm looking for.

Another idea is to create a custom block and print out the user picture and my superfish menu block with embed block to adjust it later with css but I have not tried it yet.

My favorite solution would be a function in my template.php or a small custom module.

Do you have any idea?

mehrpadin’s picture

Interesting, can you please try the Menu Token with SF v1.9-beta5? I'm about to release a new version, need to know whether it's a Superfish bug, and of course saving as much time as possible! danke in advance!

Fulgrim’s picture

ok i tested it with SF 1.9beta5 on a fresh installation and get the same error. I dont know if its a bug with superfish or menu token. Menu Views works as expected. I can override the title of the link and can place my picture token inside. Maybe I should post a issue for menu_token too :-)

mehrpadin’s picture

Project: Superfish Dropdown Menu » Menu Token
Version: 7.x-1.8 » 7.x-1.x-dev
Component: Documentation » Code
Category: support » bug

Ok, just tested it, it's not the Superfish, same with the Drupal menu blocks.

The Menu Token has to turn the html key in item options array to TRUE but it does not, in addition to this, it does not render the token from time to time, returns [user:picture], both SF and normal menu blocks, wondering why, moving this to Menu token.

deanflory’s picture

deanflory’s picture

Make sure the entity/entities you're trying to use are allowed, like user, profile (profile2), and profile type:
/admin/config/menu_token

byue’s picture

Tested with User entity on, but still the same.

byue’s picture

Hello All:

Discover a workaround, you can use Menu HTML module and select "Allow html" in your menu item, this will allow your token to display with the html tag.

Cheers,
byue

develcuy’s picture

Title: User picture and name in menu » Support for HTML in menu item title
Category: bug » task
Status: Active » Reviewed & tested by the community

Thanks for the feedback @byue!

Going to add this info to the manual.

develcuy’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.