Question

What is the recommended way to get the access level for the current content (article) being rendered for the active request, using the Joomla API.

Currently I've got it hacked using a manual query but this doesn't feel right in that I assume that there should be a way to get the access level using the object model, and not by having to write a custom query.

$db = JFactory::getDBO();

$cid = JRequest::getInt('id', 0);   // get the content id from the request

$query  = ' SELECT #__content.access FROM #__content WHERE #__content.id="'.$cid.'"';

$db->setQuery($query);
$objList = $db->loadObjectList();

foreach($objList as $obj)
{
   return $obj->access;             // gets the access level for the first object
}
Was it helpful?

Solution

You can call getItem function defined in model of article in below file:

components\com_content\models\article.php

You need to load the model first and call the getItem function by passing article id.

jimport('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models');
$articleModel = JModelLegacy::getInstance( 'Article', 'ContentModel' );

Call the function:

$result = $articleModel->getItem($articleId);

And use the access level:

$result->access;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top