I have a custom component that integrates com_categories in it, after upgrading from joomla 3.10 to 4.x, and fixing all the errors of my component on joomla 4.x, when accessing the categories menu, all categories are gone.
1 Answer
Your table which contains the items which go into each category needs to have a few columns:
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'Item unique ID',
`catid` BIGINT(20) NOT NULL COMMENT 'Category ID',
`state` TINYINT(3) NOT NULL DEFAULT '1' COMMENT 'Publish status',
Then add the follow to your manifest under <administration>
(where com_foo
is your component)
<administration>
<!-- Your code ... -->
<submenu>
<!-- Your code ... -->
<menu link="option=com_categories&extension=com_foo">JCATEGORIES</menu>
<!-- Your code ... -->
</submenu>
Note: You can display additional tabs with component-specific configuration options by creating the file administrator/components/com_foo
/forms/category.xml where com_foo
is the name of your component. This is a regular Joomla XML form file. Just remember, every you add becomes a tab in the Category edit page.
<?xml version="1.0" encoding="utf-8"?>
<form>
<!-- Your code ... -->
</form>
The Category service
If you need to get their parameters or create SEF routes for nested categories you need to somehow get information on categories. This is what the category service does for us. (where com_foo
or foo
is your component)
You need create a class in the file components/com_foo/src/Service/Category.php like this:
<?php
namespace YOUR_PREFIX\Component\YOUR_COMPONENT_NAME\Site\Service;
use Joomla\CMS\Categories\Categories;
\defined('_JEXEC') or die;
/**
* Foo Component Category Tree
*/
class Category extends Categories
{
/**
* Constructor
*
* @param array $options Array of options
*
*/
public function __construct($options = array())
{
$options['table'] = '#__foo';
$options['extension'] = 'com_foo';
parent::__construct($options);
}
}
Unlike other services, the category service is instantiated directly:
/** @var \YOUR_PREFIX\Component\YOUR_COMPONENT_NAME\Site\Service\Category $fooCategoryService */
$fooCategoryService = new \Joomla\Component\Content\Site\Service\Category($options);
Note: $options is a array and it can help you narrow down the list of categories. Check Joomla\CMS\Categories\Categories\Categories constructor function
Finally, you can for example, given a parent category return a list of children categories
/** @var \Joomla\CMS\Categories\CategoryNode $parentCategory */
$parentCategory = $fooCategoryService->get('root');
// Get all the childrens categories of this node
$childrenCategories = $parentCategory->getChildren(true);