5

I am using T3 Framework blank template. In the megamenu I have inserted the login form as a submenu, so it shows up when the user hovers over the menu item.

I would like to change the menu item title (or the text that shows there), depending on if the user is logged in e.g. 'Login' if they are not and 'hi (username)' when they are.

Is this possible, maybe by adding some php in the template, without creating 2 different menu items, one for guest users and another one for registered?

6
  • Just set the login module permissions to Guest and make another module (hi) and set it's permissions to Registered :)
    – jonboy
    Aug 18, 2015 at 19:17
  • But I cannot assign more than one module to one menu item...
    – dfk_7677
    Aug 18, 2015 at 19:19
  • @johnny_s - but then won't the registered users also be able to see the "guest" login?
    – Lodder
    Aug 18, 2015 at 19:38
  • @Lodder No, unless I am misunderstanding the question (which is very possible!). I did this recently and it worked fine, I'll try write an answer if I get time.
    – jonboy
    Aug 18, 2015 at 19:54
  • @johnny_s - Ahh fair enough. Would like to test your solution as it would save me doing a template override on my sites
    – Lodder
    Aug 18, 2015 at 20:02

2 Answers 2

1

This is what I did:

Create a Template Override for mod_login

Once done, open the following file:

templates/YOUR_TEMPLATE/html/mod_login/default_logout.php

and add the following:

$doc = JFactory::getDocument();
$doc->addScriptDeclaration('
    jQuery(document).ready(function($){ 
        $(".menu").find("a.something").empty().text("Hi ' . $user->username .'");
    });
');
2
  • Thank you. Overriding and adding the JS did what I wanted. There is just an error in your JS code as text(Hi ' . $user->username . ') should be text("Hi ' . $user->username .'")
    – dfk_7677
    Aug 18, 2015 at 21:50
  • @dfk_7677 - Ahh yes, good spot. Updated
    – Lodder
    Aug 18, 2015 at 22:21
4

If I am understanding your question you want to hide/change the menu item depending on whether or not a person is logged in or not?

OK this is how I managed to how to hide content from guest users or logged in users.

Create a new 'Guest' group;

  • Create a Guest Group in the User Manager
  • Add a new Guest Viewing Access Level
  • Change the default guest group in the User Manager options

There's a detailed guide here.

Then;

  • Find the module you want visible for Guests > Edit > Set the Access to Guest
  • Find the module you want visible for Registered Users > Edit > Set the Access to Registered

This should solve your access level issues.

In order to add the 'Hi {Username}' what I did was use NoNumbers Sourcerer and simply echo the $user->username in the module title.

Sourcerer checks your php code to see if you are referencing any commonly used Joomla objects / variables, and then creates them for you. This means you don't have to create these variables yourself every time.

Perhaps not the cleanest method (compared to @Lodders) however it does work and is easily replicated for other modules etc...

Hope this helps!

3
  • Thank you for your answer, but as you said @Lodders method is clearer.
    – dfk_7677
    Aug 18, 2015 at 21:46
  • No problem - I agree! :)
    – jonboy
    Aug 19, 2015 at 7:52
  • You got my vote as it's another possible solution, however I'm not a fan of installing a plugin that's nearly 1mb for a small task.
    – Lodder
    Aug 19, 2015 at 10:25

Your Answer

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

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