From e0abebed4f829e2ea068eeca6a17f756aebe8b1d Mon Sep 17 00:00:00 2001 From: SharkyKZ Date: Tue, 4 Aug 2020 11:51:11 +0300 Subject: [PATCH] Get categories from component --- .../com_fields/src/Model/FieldsModel.php | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/administrator/components/com_fields/src/Model/FieldsModel.php b/administrator/components/com_fields/src/Model/FieldsModel.php index a1a0c28692856..cd0f323782296 100644 --- a/administrator/components/com_fields/src/Model/FieldsModel.php +++ b/administrator/components/com_fields/src/Model/FieldsModel.php @@ -11,7 +11,8 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Categories\Categories; +use Joomla\CMS\Categories\CategoryServiceInterface; +use Joomla\CMS\Categories\SectionNotFoundException; use Joomla\CMS\Factory; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\ListModel; @@ -199,14 +200,43 @@ protected function getListQuery() if ($parts) { - // Get the category - $cat = Categories::getInstance(str_replace('com_', '', $parts[0]) . '.' . $parts[1]); + // Get the categories for this component (and optionally this section, if available) + $cat = ( + function () use ($parts) { + // Get the CategoryService for this component + $componentObject = $this->bootComponent($parts[0]); - // If there is no category for the component and section, so check the component only - if (!$cat) - { - $cat = Categories::getInstance(str_replace('com_', '', $parts[0])); - } + if (!$componentObject instanceof CategoryServiceInterface) + { + // No CategoryService -> no categories + return null; + } + + $cat = null; + + // Try to get the categories for this component and section + try + { + $cat = $componentObject->getCategory([], $parts[1] ?: ''); + } + catch (SectionNotFoundException $e) + { + // Not found for component and section -> Now try once more without the section, so only component + try + { + $cat = $componentObject->getCategory(); + } + catch (SectionNotFoundException $e) + { + // If we haven't found it now, return (no categories available for this component) + return null; + } + } + + // So we found categories for at least the component, return them + return $cat; + } + )(); if ($cat) {