I'd been searching for a way to have a user greeting like "Hi, [user]!" to the menu of one of my Joomla sites.
There are plenty of results on Google for people looking to add a user's name as a menu title on Joomla, but most of those focus on template overrides. Since I'm using a Helix 3 based template with its fancy-schmancy Mega Menu, none of them were any use to me and I was bashing my head against a wall trying to figure out why. If I knew more about what I was doing it wouldn't have been so difficult, but I'll put it here now so the next person that searches won't have as much hassle. I know enough to know there's almost certainly a better way to get this done.
If you're using a template based on Joomshaper's excellent Helix 3 Framework, a template override isn't going to help you. Instead you'll need to make a small change to Helix's core code. There are a couple of ways you can accomplish this, but this is what I did.
Step one: Create the menu item you want (I've called mine "User" but it doesn't much matter) and give it a unique class. In this case I've given mine the class "usermenu".
Step two: open up your favourite FTP program, or the File Manager on your host's website, or whatever you need to access your site's files. You'll need to open /public_html/[yoursite.com]/plugins/system/helix3/core/classes/menu.php
Step 3: When rendering the menu item, you're going to want to check to see if the menu item's class matches the class we gave it earlier and, if it does, replace it with our welcome text. To get the user's name you first have to get the $user object. All this occurs somewhere near line 413.
private function item($item, $extra_class=''){
$class = $extra_class;
$title = $item->anchor_title ? 'title="' . $item->anchor_title . '" ' : '';
$class .= ($item->anchor_css && $class) ? ' ' . $item->anchor_css : $item->anchor_css;
$class = ($class) ? 'class="' . $class . '"' : '';
if ($item->menu_image)
{
$item->params->get('menu_text', 1) ?
$linktitle = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" /><span class="image-title">' . $item->title . '</span> ' :
$linktitle = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}
else
{
// Check if the menu item's class matches the string we're looking for
if(strpos($item->anchor_css, "usermenu") !== false)
{
// Get the user object
$user = JFactory::getUser();
// Set the title of our menu item to the welcome message we want
$linktitle = "Hi, " . $user->name;
}
// Otherwise, use the menu item's proper title
else
$linktitle = $item->title;
}
// And so on and so forth
Step 4: Profit! You have now made a fancy user greeting menu item, probably at the cost of some of your host's CPU cycles. Who can tell? Certainly not me.
Did this help you? Let me know in the comments.
Is what I did stupid and you know a better way? Definitely let me know in the comments.