mirror of
https://github.com/joomla/joomla-cms.git
synced 2024-06-16 09:02:52 +00:00
Convert mod_articles_categories to new structure (#39552)
* services provider + dispatcher content * initial change in helper class * moved $startLevel variable * not used more * xml entry point * using componentInterface to get the categories * phpcs fixes * fix review * the category service can be instantiated directly * more verbose variables and methods names * fix missing getDatabase * cs fixes * cs fixes * cs fixes * update * revert docsBlocks - Get the category booting the component * revert output * avoiding nested if * cy test * cs fixes * Revert not needed layout change * Revert not needed layout change * cache * removing parent id --------- Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>
This commit is contained in:
parent
f6c73ec8b7
commit
2f4619814d
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_articles_categories
|
||||
*
|
||||
* @copyright (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Helper\ModuleHelper;
|
||||
|
||||
$cacheid = md5($module->id);
|
||||
|
||||
$cacheparams = new \stdClass();
|
||||
$cacheparams->cachemode = 'id';
|
||||
$cacheparams->class = 'Joomla\Module\ArticlesCategories\Site\Helper\ArticlesCategoriesHelper';
|
||||
$cacheparams->method = 'getList';
|
||||
$cacheparams->methodparams = $params;
|
||||
$cacheparams->modeparams = $cacheid;
|
||||
|
||||
$list = ModuleHelper::moduleCache($module, $params, $cacheparams);
|
||||
$startLevel = $list ? reset($list)->getParent()->level : null;
|
||||
|
||||
require ModuleHelper::getLayoutPath('mod_articles_categories', $params->get('layout', 'default'));
|
|
@ -11,7 +11,7 @@
|
|||
<description>MOD_ARTICLES_CATEGORIES_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Module\ArticlesCategories</namespace>
|
||||
<files>
|
||||
<filename module="mod_articles_categories">mod_articles_categories.php</filename>
|
||||
<folder module="mod_articles_categories">services</folder>
|
||||
<folder>src</folder>
|
||||
<folder>tmpl</folder>
|
||||
</files>
|
||||
|
|
41
modules/mod_articles_categories/services/provider.php
Normal file
41
modules/mod_articles_categories/services/provider.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_articles_categories
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\Service\Provider\HelperFactory;
|
||||
use Joomla\CMS\Extension\Service\Provider\Module;
|
||||
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
|
||||
/**
|
||||
* The articles categories module service provider.
|
||||
*
|
||||
* @since __DEPLOY_VERSION__
|
||||
*/
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since __DEPLOY_VERSION__
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\ArticlesCategories'));
|
||||
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\ArticlesCategories\\Site\\Helper'));
|
||||
|
||||
$container->registerServiceProvider(new Module());
|
||||
}
|
||||
};
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Site
|
||||
* @subpackage mod_articles_categories
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Module\ArticlesCategories\Site\Dispatcher;
|
||||
|
||||
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
|
||||
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
|
||||
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
|
||||
use Joomla\CMS\Helper\ModuleHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('JPATH_PLATFORM') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Dispatcher class for mod_articles_categories
|
||||
*
|
||||
* @since __DEPLOY_VERSION__
|
||||
*/
|
||||
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
|
||||
{
|
||||
use HelperFactoryAwareTrait;
|
||||
|
||||
/**
|
||||
* Returns the layout data.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since __DEPLOY_VERSION__
|
||||
*/
|
||||
protected function getLayoutData(): array
|
||||
{
|
||||
$data = parent::getLayoutData();
|
||||
$params = $data['params'];
|
||||
|
||||
$cacheParams = new \stdClass();
|
||||
$cacheParams->cachemode = 'id';
|
||||
$cacheParams->class = $this->getHelperFactory()->getHelper('ArticlesCategoriesHelper');
|
||||
$cacheParams->method = 'getChildrenCategories';
|
||||
$cacheParams->methodparams = [$params, $data['app']];
|
||||
$cacheParams->modeparams = md5(serialize($this->module->id));
|
||||
|
||||
$data['list'] = ModuleHelper::moduleCache($this->module, $params, $cacheParams);
|
||||
|
||||
$data['startLevel'] = $data['list'] ? reset($data['list'])->getParent()->level : null;
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
|
@ -10,7 +10,13 @@
|
|||
|
||||
namespace Joomla\Module\ArticlesCategories\Site\Helper;
|
||||
|
||||
use Joomla\CMS\Categories\Categories;
|
||||
use Joomla\CMS\Application\SiteApplication;
|
||||
use Joomla\CMS\Categories\CategoryInterface;
|
||||
use Joomla\CMS\Categories\CategoryNode;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\Database\DatabaseAwareInterface;
|
||||
use Joomla\Database\DatabaseAwareTrait;
|
||||
use Joomla\Registry\Registry;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
|
@ -21,35 +27,70 @@ use Joomla\CMS\Categories\Categories;
|
|||
*
|
||||
* @since 1.5
|
||||
*/
|
||||
abstract class ArticlesCategoriesHelper
|
||||
class ArticlesCategoriesHelper implements DatabaseAwareInterface
|
||||
{
|
||||
use DatabaseAwareTrait;
|
||||
|
||||
/**
|
||||
* Get list of articles
|
||||
* Given a parent category, return a list of children categories
|
||||
*
|
||||
* @param \Joomla\Registry\Registry &$params module parameters
|
||||
* @param Registry $moduleParams The module parameters.
|
||||
* @param SiteApplication $app The current application.
|
||||
*
|
||||
* @return CategoryNode[]
|
||||
*
|
||||
* @since __DEPLOY_VERSION__
|
||||
*/
|
||||
public function getChildrenCategories(Registry $moduleParams, SiteApplication $app): array
|
||||
{
|
||||
// Joomla\CMS\Categories\Categories options to set
|
||||
$options = [];
|
||||
|
||||
// Get the number of items in this category or descendants of this category at the expense of performance.
|
||||
$options['countItems'] = $moduleParams->get('numitems', 0);
|
||||
|
||||
/** @var CategoryInterface $categoryFactory */
|
||||
$categoryFactory = $app->bootComponent('com_content')->getCategory($options);
|
||||
|
||||
/** @var CategoryNode $parentCategory */
|
||||
$parentCategory = $categoryFactory->get($moduleParams->get('parent', 'root'));
|
||||
|
||||
if ($parentCategory === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get all the children categories of this node
|
||||
$childrenCategories = $parentCategory->getChildren(true);
|
||||
|
||||
$count = $moduleParams->get('count', 0);
|
||||
|
||||
if ($count > 0 && \count($childrenCategories) > $count) {
|
||||
$childrenCategories = \array_slice($childrenCategories, 0, $count);
|
||||
}
|
||||
|
||||
return $childrenCategories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of categories
|
||||
*
|
||||
* @param Registry &$params module parameters
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.5
|
||||
* @since 1.6
|
||||
*
|
||||
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
|
||||
* Use the non-static method getChildrenCategories
|
||||
* Example: Factory::getApplication()->bootModule('mod_articles_categories', 'site')
|
||||
* ->getHelper('ArticlesCategoriesHelper')
|
||||
* ->getChildrenCategories($params, Factory::getApplication())
|
||||
*/
|
||||
public static function getList(&$params)
|
||||
{
|
||||
$options = [];
|
||||
$options['countItems'] = $params->get('numitems', 0);
|
||||
/** @var SiteApplication $app */
|
||||
$app = Factory::getApplication();
|
||||
|
||||
$categories = Categories::getInstance('Content', $options);
|
||||
$category = $categories->get($params->get('parent', 'root'));
|
||||
|
||||
if ($category !== null) {
|
||||
$items = $category->getChildren();
|
||||
|
||||
$count = $params->get('count', 0);
|
||||
|
||||
if ($count > 0 && \count($items) > $count) {
|
||||
$items = \array_slice($items, 0, $count);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
return (new self())->getChildrenCategories($params, $app);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
describe('Test the articles categories module', () => {
|
||||
it('can load in frontend and showing the title of the categories', () => {
|
||||
cy.db_createCategory({ title: 'automated test category' })
|
||||
.then(() => cy.db_createModule({ module: 'mod_articles_categories' }))
|
||||
.then(() => {
|
||||
cy.visit('/');
|
||||
|
||||
cy.contains('li', 'automated test category');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user