2

I have a plugin that has to pre-process the description of a category based on it's params before it displays all the subcategories that it contains.

For articles (context = com_content.article), the plugin onContentPrepare event passes $article and $params giving everything one would need, but when the context is com_content.category, $params is empty, and $article only has $article->text.

I know that the category of interest is in the query string, but is there a better way to find category info, etc. that has already been retrieved along the way?

With index.php?option=com_content&view=category&id=123&Itemid=456

public function onContentPrepare($context, &$article, &$params, $page=0)
    if($context == 'com_content.category') {
        // $article has only article ->text
        // $params is empty
        // how can I get category params here?
    }

2 Answers 2

2

Looking at view.html.php for com_content.category, the trigger call is:

$dispatcher->trigger('onContentPrepare', array ('com_content.category', &$item, &$item->params, 0));

which is sending the $item (article) parameters. No good.

Looking at line 72 of the same file (parent::commonCategoryDisplay();), which itself is in JCategoryView (code here), the $params are stored (in view.html.php, $this->params) but as a category isn't technically content, the contentPrepare event isn't triggered at this stage.

You could override this view.html.php to fire the event. See this guide - but it does require a special plugin.

4
  • 1
    v3.3...but those are the params for each of the articles that are contained within the category...is the parent category I'm interested in, not the child articles.
    – GDP
    Commented Oct 14, 2014 at 14:41
  • Apologies, missed that in my haste. Updated answer. Commented Oct 14, 2014 at 14:50
  • 1
    Made that same hasty assumption myself when I started this, lol. Am trying to avoid that overriding though...was hoping a $model-> or $this-> or $that-> of some kind would get me there.
    – GDP
    Commented Oct 14, 2014 at 14:53
  • Perhaps JRegistry is the way to go here? Docs are poor but could be the easiest way of loading it in sans-override, something like phpcodesnipps.blogspot.co.uk/2013/02/… Commented Oct 14, 2014 at 15:06
1

As @codinghands pointed out, the category is not technically content so (by design) all the information of interest is not available, leaving only the title of the category generally available to alter.

The other "catch" when listing all the subcategories and articles within the category is that the plugin event is fired for all of them, so if you have more than a few categories and/or articles, the plugin code is going to fire a lot, unless provision is made to fire only once.

To get the parameters of only the main category being displayed, my assumption is that the first time the plugin is fired, it is for the main category. (This has been proven true in my case, but I can't be certain that other settings might change that).

index.php?option=com_content&view=category&id=123

static $isMainCat = true;
if($isMainCat){
    $catid = JFactory::getApplication()->input->getInt('id');

    $basePath = JPATH_ADMINISTRATOR . '/components/com_categories';
    require_once $basePath . '/models/category.php';
    $category = new CategoriesModelCategory(array('table_path' => $basePath . '/tables'));
    $item = $category->getItem($catid);

    $params = new JRegistry;
    $params->loadString($item->params);
    $image = $params->get('image','noimage.gif');

    $isMainCat = false; // Prevent this code from running on any but the first category
}

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.