How to use JCategories in my component

Step 1: Create special helper

CREATE file in: JPATH_SITE /components/YOURCOMPONENT/helpers/category.php

Example:

<?php
defined('_JEXEC') or die;
jimport('joomla.application.categories');

class YOURCOMPONENTCategories extends JCategories
{
	public function __construct($options = array())
	{
		$options['table'] = '#__content';
		$options['extension'] = 'com_YOURCOMPONENT';
		parent::__construct($options);
	}
}

Step 2: Using in your model

class YOURCOMPONENTModelCategories extends JModelList
{
	public function getItems($recursive = false)
	{
		if (!count($this->_items)) {
			
			// import Joomla Categories library
			//if you forget this -> Fatal error: Class 'JCategories' not found in ...
			jimport( 'joomla.application.categories' );

			$app = JFactory::getApplication();
			
			$options = array();
			$options['countItems'] = 20;

//$categories = JCategories::getInstance('Content', $options);

			$categories = JCategories::getInstance('YOURCOMPONENT', $options);

			$this->_parent = $categories->get('root');

			if (is_object($this->_parent)) {
				$this->_items = $this->_parent->getChildren($recursive);
			}
			else {
				$this->_items = false;
			}
		}

		return $this->_items;
	}
}


Scroll to Top
Ads: