Skip to content

Commit 8262060

Browse files
authored
Convert mod_related_items to new structure (#39628)
* adding service provider * adding the dispatcher * declare main function * Help class definition * layout adaptation * not used * fix some errors * adding cache * cs fix * update * cs * update * revert Helper * cy test * cs * javascript cs * cy test without need article ID * cache * revert tmpl * revert tmpl * revert tmpl * separate test code
1 parent 0a40ceb commit 8262060

File tree

6 files changed

+152
-42
lines changed

6 files changed

+152
-42
lines changed

modules/mod_related_items/mod_related_items.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

modules/mod_related_items/mod_related_items.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_RELATED_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\RelatedItems</namespace>
1313
<files>
14-
<filename module="mod_related_items">mod_related_items.php</filename>
14+
<folder module="mod_related_items">services</folder>
1515
<folder>src</folder>
1616
<folder>tmpl</folder>
1717
</files>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_related_items
6+
*
7+
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\Service\Provider\HelperFactory;
14+
use Joomla\CMS\Extension\Service\Provider\Module;
15+
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
16+
use Joomla\DI\Container;
17+
use Joomla\DI\ServiceProviderInterface;
18+
19+
/**
20+
* The articles related module service provider.
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
return new class () implements ServiceProviderInterface {
25+
/**
26+
* Registers the service provider with a DI container.
27+
*
28+
* @param Container $container The DI container.
29+
*
30+
* @return void
31+
*
32+
* @since __DEPLOY_VERSION__
33+
*/
34+
public function register(Container $container)
35+
{
36+
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\RelatedItems'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\RelatedItems\\Site\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_related_items
6+
*
7+
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Module\RelatedItems\Site\Dispatcher;
12+
13+
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
14+
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
15+
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
16+
use Joomla\CMS\Helper\ModuleHelper;
17+
18+
// phpcs:disable PSR1.Files.SideEffects
19+
\defined('JPATH_PLATFORM') or die;
20+
// phpcs:enable PSR1.Files.SideEffects
21+
22+
/**
23+
* Dispatcher class for mod_articles_popular
24+
*
25+
* @since __DEPLOY_VERSION__
26+
*/
27+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
28+
{
29+
use HelperFactoryAwareTrait;
30+
31+
/**
32+
* Returns the layout data.
33+
*
34+
* @return array
35+
*
36+
* @since __DEPLOY_VERSION__
37+
*/
38+
protected function getLayoutData(): array
39+
{
40+
$data = parent::getLayoutData();
41+
$params = $data['params'];
42+
43+
$cacheParams = new \stdClass();
44+
$cacheParams->cachemode = 'safeuri';
45+
$cacheParams->class = $this->getHelperFactory()->getHelper('RelatedItemsHelper');
46+
$cacheParams->method = 'getRelatedArticles';
47+
$cacheParams->methodparams = [$params, $data['app']];
48+
$cacheParams->modeparams = ['id' => 'int', 'Itemid' => 'int'];
49+
50+
$data['list'] = ModuleHelper::moduleCache($this->module, $params, $cacheParams);
51+
$data['showDate'] = $params->get('showDate', 0);
52+
53+
return $data;
54+
}
55+
}

modules/mod_related_items/src/Helper/RelatedItemsHelper.php

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
namespace Joomla\Module\RelatedItems\Site\Helper;
1212

13+
use Joomla\CMS\Application\SiteApplication;
1314
use Joomla\CMS\Factory;
1415
use Joomla\CMS\Language\Multilanguage;
1516
use Joomla\CMS\Language\Text;
1617
use Joomla\CMS\Router\Route;
1718
use Joomla\Component\Content\Administrator\Extension\ContentComponent;
1819
use Joomla\Component\Content\Site\Helper\RouteHelper;
20+
use Joomla\Component\Content\Site\Model\ArticlesModel;
21+
use Joomla\Database\DatabaseAwareInterface;
22+
use Joomla\Database\DatabaseAwareTrait;
1923
use Joomla\Database\ParameterType;
24+
use Joomla\Registry\Registry;
2025

2126
// phpcs:disable PSR1.Files.SideEffects
2227
\defined('_JEXEC') or die;
@@ -27,26 +32,30 @@
2732
*
2833
* @since 1.5
2934
*/
30-
abstract class RelatedItemsHelper
35+
class RelatedItemsHelper implements DatabaseAwareInterface
3136
{
37+
use DatabaseAwareTrait;
38+
3239
/**
33-
* Get a list of related articles
40+
* Retrieve a list of related articles based on the metakey field
3441
*
35-
* @param \Joomla\Registry\Registry &$params module parameters
42+
* @param Registry $params The module parameters.
43+
* @param SiteApplication $app The current application.
3644
*
37-
* @return array
45+
* @return \stdClass[]
46+
*
47+
* @since __DEPLOY_VERSION__
3848
*/
39-
public static function getList(&$params)
49+
public function getRelatedArticles(Registry $params, SiteApplication $app): array
4050
{
41-
$db = Factory::getDbo();
42-
$app = Factory::getApplication();
51+
$db = $this->getDatabase();
4352
$input = $app->getInput();
44-
$groups = Factory::getUser()->getAuthorisedViewLevels();
53+
$groups = $app->getIdentity()->getAuthorisedViewLevels();
4554
$maximum = (int) $params->get('maximum', 5);
4655
$factory = $app->bootComponent('com_content')->getMVCFactory();
4756

4857
// Get an instance of the generic articles model
49-
/** @var \Joomla\Component\Content\Site\Model\ArticlesModel $articles */
58+
/** @var ArticlesModel $articles */
5059
$articles = $factory->createModel('Articles', 'Site', ['ignore_request' => true]);
5160

5261
// Set application parameters in model
@@ -133,7 +142,7 @@ public static function getList(&$params)
133142

134143
// Filter by language
135144
if (Multilanguage::isEnabled()) {
136-
$query->whereIn($db->quoteName('a.language'), [Factory::getLanguage()->getTag(), '*'], ParameterType::STRING);
145+
$query->whereIn($db->quoteName('a.language'), [$app->getLanguage()->getTag(), '*'], ParameterType::STRING);
137146
}
138147

139148
$query->setLimit($maximum);
@@ -167,4 +176,27 @@ public static function getList(&$params)
167176

168177
return $related;
169178
}
179+
180+
/**
181+
* Get a list of related articles
182+
*
183+
* @param Registry &$params module parameters
184+
*
185+
* @return array
186+
*
187+
* @since 1.6
188+
*
189+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
190+
* Use the non-static method getRelatedArticles
191+
* Example: Factory::getApplication()->bootModule('mod_related_items', 'site')
192+
* ->getHelper('RelatedItemsHelper')
193+
* ->getRelatedArticles($params, Factory::getApplication())
194+
*/
195+
public static function getList(&$params)
196+
{
197+
/** @var SiteApplication $app */
198+
$app = Factory::getApplication();
199+
200+
return (new self())->getRelatedArticles($params, $app);
201+
}
170202
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
describe('Test the related items module', () => {
2+
it('can load in frontend showing a list of related articles based on the metakey field', () => {
3+
cy.db_createArticle({ title: 'Main Article', metakey: 'joomla', featured: 1 })
4+
.then(() => cy.db_createArticle({ title: 'article with joomla keyword', metakey: 'joomla' }))
5+
.then(() => cy.db_createModule({ module: 'mod_related_items' }))
6+
.then(() => {
7+
cy.visit('/');
8+
cy.contains('a', 'Main Article').click();
9+
10+
cy.contains('li', 'article with joomla keyword');
11+
});
12+
});
13+
});

0 commit comments

Comments
 (0)