4

I want to create special menu items with title "Welcome ". I want to make it as special menu item (without link to any node), that shows sub-menu on hover. For that I have used Special Menu Items module and is working fine.

But I am not able to figure out how to create dynamic menu title in form "Welcome Username". I have tried using Menu Token module as well, but it didn't work. It just displays token as it is.

Any suggestion how can I achieve this using module or programatically.

-- Many thanks for your time.

10
  • 4
    "I have tried using Menu Token module as well, but it didn't work" That module works well for me in dozens of projects, could you expand on what doesn't work?
    – Clive
    Commented Mar 14, 2013 at 12:10
  • It just shows token as it is, without replacing with actual value. Commented Mar 14, 2013 at 12:52
  • 1
    And you've definitely got the "Use tokens in title and in path" box checked, and have selected an option for "Methods for Nodes" or "Methods for Users" to invoke the tokens?
    – Clive
    Commented Mar 14, 2013 at 13:09
  • Yes, I followed that. Commented Mar 14, 2013 at 13:12
  • 2
    Looks like you've found a bug in the module then (like I say, that module works perfectly for me in dozens of projects). It would we a good idea to report the bug on the issue queue
    – Clive
    Commented Mar 14, 2013 at 13:14

6 Answers 6

1

I used this on www.grupposportivo.be in a bootstrap nav bar

<!-- de inlog button en user print -->
            <li><?php global $user; if ($user->uid != 0)
                { print '<li class="first">' .t('').
                    '<a href="' .url('user/'.$user->uid). '">' .$user->name. '</a>
                    </li>'; print '<li>
                    <a href="' .url('user/logout'). '">' .t('Logout'). '</a>
                    </li>'; } else { print '<li class="first">
                    <a href="' .url('user'). '">' .t('Login'). '</a>
                    </li>'; print '<li>
                    <a href="' .url('user/register'). '">' .t(''). '</a>
                    </li>'; } ?></li>
0

Copy the existing special_menu_items module. e.g. welcome_menu_items and put it in your custom modules directory. It is only 200 lines, so maintenance particularly if your requirements don't change should not be large.

Change this code adding '. check_plain($user->name);'.

  case '<nolink>':
    $tag = variable_get('special_menu_items_nolink_tag', '<span>');
    $title = $variables['options']['html'] ? $variables['text'] : check_plain($variables['text']) . check_plain($user->name);
    $variables['options']['attributes']['class'][] = 'nolink';
    break;

This will only give you the username, not the full name. You need to edit the profile to provide a full name. You might also want to check that $variables['text'] contains the work 'Welcome' before altering it.

You could debug $variables['text'] and submit a patch if there is a problem with this as noted by Clive.

5
  • But as it is special menu item with href="#", it links to nowhere, than what should be index of $items[]. Commented Mar 14, 2013 at 12:54
  • <nolink>. Use dpm or Netbeans debugger to double check.
    – Interlated
    Commented Mar 14, 2013 at 13:25
  • But won't it affect all special menu items with <nolink> Commented Mar 14, 2013 at 13:32
  • Looking at the code in special_menu_items you might be better off hooking function hook_link(array $variables) . It is only 200 lines of code. Copy to your custom and change the link code.
    – Interlated
    Commented Mar 14, 2013 at 14:06
  • I take back this answer. I can't see <nolink> in the array and it only runs when the menu is being rebuilt - so won't help you.
    – Interlated
    Commented Mar 14, 2013 at 14:07
0

Hope this code snippet will help you.

function YOURTHEMENAME_menu_link(array $variables) {
  $element = $variables['element'];
 if($element['#original_link']['mlid']=='YOUR MENU ID'){
    global $user;
    $element['#title'] = $element['#title']. ' '.$user->name;
  }
    $output = l($element['#title'], $element['#href'], $element['#localized_options'], array('attributes' => array('data-title' => $element['#title'])));
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";

}

Add this inside your sites > all > themes > default-theme > template.php

0

I think the cleanest method is to use Menu token module and add menu item with this settings:

  • Title: Welcome [current-user:name]
  • Path: [current-user:edit-url]
  • Use tokens in title and in path: checked
0

Here's how we accomplished the same thing you are looking to do. We used a custom module and hook_block_view. I've left the part relevant to you. header_user_nav is our custom nav block at the top of the site. We display the login/register link if the user is not logged in. Once logged in [if($user->uid !== 0)], the block content changes. The first li is the Welcome message plus the user's name.

function custom_module_name_block_view($delta = '') {
  $block = array();
  if($delta == 'header_user_nav'){
    $block['content'] = '<ul><li class="login">' . l('Login or Register', 'user/login', array('query' => drupal_get_destination())) . '</li></ul>';
    $block['subject'] = '';
    global $user;
    if($user->uid !== 0){
      $output = '<ul><li class="username">';
      $output .= t('Welcome ') . $user->name;
      $output .= '</li>';**
      $output .= '<li class="profile">';
      $output .= l('My Profile', 'user');
      $output .= '</li>';
      $output .= '<li class="logout">';
      $output .= l('Logout', 'user/logout');
      $output .= '</li></ul>';

      $block['content'] = $output;
    }
  }

  return $block;
}
0

Use the site current user token for menu tokens. See the screenshots it helps you.

enter image description here enter image description here enter image description here

2
  • This doesn't work. It only rendered the token name in the menu.
    – Christia
    Commented Jul 10, 2017 at 17:08
  • Please select "user from content" from dropdown "Method for User". please follow the above screenshots
    – Karuna
    Commented Jul 13, 2017 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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