Show a joomla article to registered users only, or a login screen if not logged in yes

Hans Wassink picture Hans Wassink · May 8, 2012 · Viewed 18.5k times · Source

In Joomla prior to 1.6 i could set a menu-item to public, and its contents: an article for example to registered.

This lead to a situation where people could see the link to an article in the menu when not logged in, but got a login component whenever they clicked it. And after that they saw the article.

In 1.7 these same actions lead to a situation where when I click the link the component screen just stays empty.

How do I get registered articles to show a login screen when the front end user is not logged in with sufficient rights? It was soo easy before and I can't seem to get it to work now.

Answer

Hans Wassink picture Hans Wassink · May 25, 2012

Im gonna answer my own question, because Im sure people will need this in the future, and my solution only involves a few rules of extra code and then you can set every article etc... to Registered and you'll see a login field when a user is not logged in.

In your templates index.php place this near the top, it gets your article's access level.

$article =& JTable::getInstance("content");
$article->load(JRequest::getVar('id'));
$cAccLevel = $article->get("access");

Then add a module position above your component, and only show it when your needed access level is > 1

<?php if($cAccLevel > 1): ?>
    <jdoc:include type="modules" name="LOGIN_MODULE_POSITION" />
<?php endif; ?>

Then add a login module in your module manager to LOGIN_MODULE_POSITION.

Voila... no routing needed etc... everything works out of the box, I chose to style away the logout box and action field like this:

.logout-button,
.actions{
    display:none;
}

Good luck!