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 :-)
Comments
Comment #1
mehrpadin commentedHey there,
Have you tried the Menu Tokens module? http://drupal.org/project/menu_token
Comment #2
Fulgrim commentedthanks 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:
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?
Comment #3
mehrpadin commentedInteresting, 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!
Comment #4
Fulgrim commentedok 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 :-)
Comment #5
mehrpadin commentedOk, just tested it, it's not the Superfish, same with the Drupal menu blocks.
The Menu Token has to turn the
htmlkey in item options array toTRUEbut 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.Comment #6
deanflory commentedComment #7
deanflory commentedMake sure the entity/entities you're trying to use are allowed, like user, profile (profile2), and profile type:
/admin/config/menu_token
Comment #8
byue commentedTested with User entity on, but still the same.
Comment #9
byue commentedHello 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
Comment #10
develcuy commentedThanks for the feedback @byue!
Going to add this info to the manual.
Comment #11
develcuy commented