Skip to content

Commit d36ef9a

Browse files
authored
[5.1] Convert mod_tags_popular to service provider (#42899)
1 parent ca3faf7 commit d36ef9a

File tree

5 files changed

+145
-37
lines changed

5 files changed

+145
-37
lines changed

modules/mod_tags_popular/mod_tags_popular.php

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

modules/mod_tags_popular/mod_tags_popular.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_TAGS_POPULAR_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\TagsPopular</namespace>
1313
<files>
14-
<filename module="mod_tags_popular">mod_tags_popular.php</filename>
14+
<folder module="mod_tags_popular">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_tags_popular
6+
*
7+
* @copyright (C) 2024 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 popular tags 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\\TagsPopular'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\TagsPopular\\Site\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_tags_popular
6+
*
7+
* @copyright (C) 2024 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\TagsPopular\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('_JEXEC') or die;
20+
// phpcs:enable PSR1.Files.SideEffects
21+
22+
/**
23+
* Dispatcher class for mod_tags_popular
24+
*
25+
* @since __DEPLOY_VERSION__
26+
*/
27+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
28+
{
29+
use HelperFactoryAwareTrait;
30+
31+
/**
32+
* Runs the dispatcher.
33+
*
34+
* @return void
35+
*
36+
* @since __DEPLOY_VERSION__
37+
*/
38+
public function dispatch()
39+
{
40+
$displayData = $this->getLayoutData();
41+
42+
if (!\count($displayData['list']) && !$displayData['params']->get('no_results_text')) {
43+
return;
44+
}
45+
46+
parent::dispatch();
47+
}
48+
49+
/**
50+
* Returns the layout data.
51+
*
52+
* @return array
53+
*
54+
* @since __DEPLOY_VERSION__
55+
*/
56+
protected function getLayoutData()
57+
{
58+
$data = parent::getLayoutData();
59+
60+
$cacheparams = new \stdClass();
61+
$cacheparams->cachemode = 'safeuri';
62+
$cacheparams->class = $this->getHelperFactory()->getHelper('TagsPopularHelper');
63+
$cacheparams->method = 'getTags';
64+
$cacheparams->methodparams = $data['params'];
65+
$cacheparams->modeparams = ['id' => 'array', 'Itemid' => 'int'];
66+
67+
$data['list'] = ModuleHelper::moduleCache($this->module, $data['params'], $cacheparams);
68+
$data['display_count'] = $data['params']->get('display_count', 0);
69+
70+
return $data;
71+
}
72+
}

modules/mod_tags_popular/src/Helper/TagsPopularHelper.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Joomla\CMS\Component\ComponentHelper;
1414
use Joomla\CMS\Factory;
1515
use Joomla\CMS\Helper\ContentHelper;
16+
use Joomla\Database\DatabaseAwareInterface;
17+
use Joomla\Database\DatabaseAwareTrait;
1618
use Joomla\Database\ParameterType;
1719

1820
// phpcs:disable PSR1.Files.SideEffects
@@ -24,21 +26,24 @@
2426
*
2527
* @since 3.1
2628
*/
27-
abstract class TagsPopularHelper
29+
class TagsPopularHelper implements DatabaseAwareInterface
2830
{
31+
use DatabaseAwareTrait;
32+
2933
/**
3034
* Get list of popular tags
3135
*
3236
* @param \Joomla\Registry\Registry &$params module parameters
3337
*
3438
* @return mixed
3539
*
36-
* @since 3.1
40+
* @since __DEPLOY_VERSION__
3741
*/
38-
public static function getList(&$params)
42+
public function getTags(&$params)
3943
{
40-
$db = Factory::getDbo();
41-
$user = Factory::getUser();
44+
$db = $this->getDatabase();
45+
$app = Factory::getApplication();
46+
$user = $app->getIdentity();
4247
$groups = $user->getAuthorisedViewLevels();
4348
$timeframe = $params->get('timeframe', 'alltime');
4449
$maximum = (int) $params->get('maximum', 5);
@@ -183,9 +188,29 @@ public static function getList(&$params)
183188
$results = $db->loadObjectList();
184189
} catch (\RuntimeException $e) {
185190
$results = [];
186-
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
191+
$app->enqueueMessage($e->getMessage(), 'error');
187192
}
188193

189194
return $results;
190195
}
196+
197+
/**
198+
* Get list of popular tags
199+
*
200+
* @param \Joomla\Registry\Registry &$params module parameters
201+
*
202+
* @return mixed
203+
*
204+
* @since 3.1
205+
*
206+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
207+
* Use the non-static method getTags
208+
* Example: Factory::getApplication()->bootModule('mod_tags_popular', 'site')
209+
* ->getHelper('TagsPopularHelper')
210+
* ->getTags($params)
211+
*/
212+
public static function getList(&$params)
213+
{
214+
return (new self())->getTags($params);
215+
}
191216
}

0 commit comments

Comments
 (0)