Skip to content

Commit 89685dd

Browse files
authored
[6.0] Convert Popular Module to Service (#45808)
* move module to services * cs and spelling fixes * now the other cs check * deprecate unused language string ---------
1 parent 348f98b commit 89685dd

File tree

7 files changed

+196
-100
lines changed

7 files changed

+196
-100
lines changed

administrator/language/en-GB/mod_popular.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ MOD_POPULAR_FIELD_VALUE_ADDED_OR_MODIFIED_BY_ME="Added or modified by me"
1111
MOD_POPULAR_FIELD_VALUE_ANYONE="Anyone"
1212
MOD_POPULAR_FIELD_VALUE_NOT_ADDED_OR_MODIFIED_BY_ME="Not added or modified by me"
1313
MOD_POPULAR_ITEMS="Popular Items"
14+
; The following string is deprecated and will be removed with 7.0
1415
MOD_POPULAR_NO_MATCHING_RESULTS="No Matching Results"
1516
MOD_POPULAR_TITLE="Popular Articles"
1617
MOD_POPULAR_TITLE_1="Top Popular Article"

administrator/modules/mod_popular/mod_popular.php

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

administrator/modules/mod_popular/mod_popular.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_POPULAR_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\Popular</namespace>
1313
<files>
14-
<filename module="mod_popular">mod_popular.php</filename>
14+
<folder module="mod_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.Administrator
5+
* @subpackage mod_popular
6+
*
7+
* @copyright (C) 2025 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 latest articles 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\\Popular'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Popular\\Administrator\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage mod_popular
6+
*
7+
* @copyright (C) 2025 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\Popular\Administrator\Dispatcher;
12+
13+
use Joomla\CMS\Component\ComponentHelper;
14+
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
15+
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
16+
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
17+
use Joomla\Module\Popular\Administrator\Helper\PopularHelper;
18+
19+
// phpcs:disable PSR1.Files.SideEffects
20+
\defined('_JEXEC') or die;
21+
// phpcs:enable PSR1.Files.SideEffects
22+
23+
/**
24+
* Dispatcher class for mod_popular
25+
*
26+
* @since __DEPLOY_VERSION__
27+
*/
28+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
29+
{
30+
use HelperFactoryAwareTrait;
31+
32+
/**
33+
* Returns the layout data.
34+
*
35+
* @return array
36+
*
37+
* @since __DEPLOY_VERSION__
38+
*/
39+
protected function getLayoutData()
40+
{
41+
$data = parent::getLayoutData();
42+
/** @var PopularHelper $helper */
43+
$helper = $this->getHelperFactory()->getHelper('PopularHelper', $data);
44+
$articleModel = $this
45+
->getApplication()
46+
->bootComponent('com_content')
47+
->getMVCFactory()
48+
->createModel('Articles', 'Administrator', ['ignore_request' => true]);
49+
50+
if ($data['params']->get('automatic_title', 0)) {
51+
$data['module']->title = $helper->getModuleTitle($data['params']);
52+
}
53+
54+
$data['list'] = $helper->getArticles($data['params'], $articleModel);
55+
$data['record_hits'] = (int) ComponentHelper::getParams('com_content')->get('record_hits', 1);
56+
57+
return $data;
58+
}
59+
}

administrator/modules/mod_popular/src/Helper/PopularHelper.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace Joomla\Module\Popular\Administrator\Helper;
1212

13-
use Joomla\CMS\Categories\Categories;
14-
use Joomla\CMS\Factory;
13+
use Joomla\CMS\Application\CMSApplicationInterface;
1514
use Joomla\CMS\Language\Text;
1615
use Joomla\CMS\Router\Route;
1716
use Joomla\Component\Content\Administrator\Model\ArticlesModel;
@@ -26,8 +25,35 @@
2625
*
2726
* @since 1.6
2827
*/
29-
abstract class PopularHelper
28+
class PopularHelper
3029
{
30+
/**
31+
* @var CMSApplicationInterface
32+
*
33+
* @since __DEPLOY_VERSION__
34+
*/
35+
protected $app;
36+
37+
/**
38+
* @var Registry
39+
*
40+
* @since __DEPLOY_VERSION__
41+
*/
42+
protected $params;
43+
44+
/**
45+
* Helper class constructor
46+
*
47+
* @param array $config Parameters we are using
48+
*
49+
* @since __DEPLOY_VERSION__
50+
*/
51+
public function __construct($config)
52+
{
53+
$this->app = $config['app'];
54+
$this->params = $config['params'];
55+
}
56+
3157
/**
3258
* Get a list of the most popular articles.
3359
*
@@ -37,10 +63,12 @@ abstract class PopularHelper
3763
* @return mixed An array of articles, or false on error.
3864
*
3965
* @throws \Exception
66+
*
67+
* @since __DEPLOY_VERSION__
4068
*/
41-
public static function getList(Registry $params, ArticlesModel $model)
69+
public function getArticles(Registry $params, ArticlesModel $model): mixed
4270
{
43-
$user = Factory::getApplication()->getIdentity();
71+
$user = $this->app->getIdentity();
4472

4573
// Set List SELECT
4674
$model->setState('list.select', 'a.id, a.title, a.checked_out, a.checked_out_time, ' .
@@ -102,15 +130,17 @@ public static function getList(Registry $params, ArticlesModel $model)
102130
* @param Registry $params The module parameters.
103131
*
104132
* @return string The alternate title for the module.
133+
*
134+
* @since __DEPLOY_VERSION__
105135
*/
106-
public static function getTitle($params)
136+
public function getModuleTitle(Registry $params): string
107137
{
108138
$who = $params->get('user_id', 0);
109139
$catid = (int) $params->get('catid', null);
110140
$title = '';
111141

112142
if ($catid) {
113-
$category = Categories::getInstance('Content')->get($catid);
143+
$category = $this->app->bootComponent('com_content')->getCategory()->get($catid);
114144
$title = Text::_('MOD_POPULAR_UNEXISTING');
115145

116146
if ($category) {

administrator/modules/mod_popular/tmpl/default.php

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,66 @@
1212

1313
use Joomla\CMS\HTML\HTMLHelper;
1414
use Joomla\CMS\Language\Text;
15+
use Joomla\CMS\Layout\LayoutHelper;
1516

1617
$moduleId = str_replace(' ', '', $module->title) . $module->id;
1718

1819
?>
19-
<table class="table" id="<?php echo str_replace(' ', '', $module->title) . $module->id; ?>">
20-
<caption class="visually-hidden"><?php echo $module->title; ?></caption>
21-
<thead>
22-
<tr>
23-
<th scope="col" class="w-60"><?php echo Text::_('JGLOBAL_TITLE'); ?></th>
24-
<th scope="col" class="w-20"><?php echo Text::_('JGLOBAL_HITS'); ?></th>
25-
<th scope="col" class="w-20"><?php echo Text::_('JDATE'); ?></th>
26-
</tr>
27-
</thead>
28-
<tbody>
29-
<?php if (count($list)) : ?>
30-
<?php foreach ($list as $i => $item) : ?>
31-
<?php // Calculate popular items ?>
32-
<?php $hits = (int) $item->hits; ?>
33-
<?php $hits_class = ($hits >= 10000 ? 'danger' : ($hits >= 1000 ? 'warning' : ($hits >= 100 ? 'info' : 'secondary'))); ?>
34-
<tr>
35-
<th scope="row">
36-
<?php if ($item->checked_out) : ?>
37-
<?php echo HTMLHelper::_('jgrid.checkedout', $moduleId . $i, $item->editor, $item->checked_out_time); ?>
38-
<?php endif; ?>
39-
<?php if ($item->link) : ?>
40-
<a href="<?php echo $item->link; ?>" title="<?php echo Text::_('JACTION_EDIT'); ?> <?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>">
41-
<?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>
42-
</a>
43-
<?php else : ?>
44-
<?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>
45-
<?php endif; ?>
46-
</th>
47-
<td>
48-
<span class="badge bg-<?php echo $hits_class; ?>"><?php echo $item->hits; ?></span>
49-
</td>
50-
<td>
51-
<?php echo HTMLHelper::_('date', $item->publish_up, Text::_('DATE_FORMAT_LC4')); ?>
52-
</td>
53-
</tr>
54-
<?php endforeach; ?>
20+
<?php if ($record_hits === 0) : ?>
21+
<?php echo LayoutHelper::render('joomla.content.emptystate_module', [
22+
'title' => 'JGLOBAL_RECORD_HITS_DISABLED',
23+
'icon' => 'icon-minus-circle',
24+
]); ?>
25+
<?php else : ?>
26+
<?php if (\count($list)) : ?>
27+
<table class="table" id="<?php echo str_replace(' ', '', $module->title) . $module->id; ?>">
28+
<caption class="visually-hidden"><?php echo $module->title; ?></caption>
29+
<thead>
30+
<tr>
31+
<th scope="col" class="w-60"><?php echo Text::_('JGLOBAL_TITLE'); ?></th>
32+
<th scope="col" class="w-20"><?php echo Text::_('JGLOBAL_HITS'); ?></th>
33+
<th scope="col" class="w-20"><?php echo Text::_('JDATE'); ?></th>
34+
</tr>
35+
</thead>
36+
<tbody>
37+
<?php foreach ($list as $i => $item) : ?>
38+
<?php // Calculate popular items ?>
39+
<?php $hits = (int) $item->hits; ?>
40+
<?php $hits_class = ($hits >= 10000 ? 'danger' : ($hits >= 1000 ? 'warning' : ($hits >= 100 ? 'info' : 'secondary'))); ?>
41+
<tr>
42+
<th scope="row">
43+
<?php if ($item->checked_out) : ?>
44+
<?php echo HTMLHelper::_('jgrid.checkedout', $moduleId . $i, $item->editor, $item->checked_out_time); ?>
45+
<?php endif; ?>
46+
<?php if ($item->link) : ?>
47+
<a href="<?php echo $item->link; ?>" title="<?php echo Text::_('JACTION_EDIT'); ?> <?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>">
48+
<?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>
49+
</a>
50+
<?php else : ?>
51+
<?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>
52+
<?php endif; ?>
53+
</th>
54+
<td>
55+
<span class="badge bg-<?php echo $hits_class; ?>"><?php echo $item->hits; ?></span>
56+
</td>
57+
<td>
58+
<?php echo HTMLHelper::_('date', $item->publish_up, Text::_('DATE_FORMAT_LC4')); ?>
59+
</td>
60+
</tr>
61+
<?php endforeach; ?>
62+
</tbody>
63+
</table>
64+
5565
<?php else : ?>
56-
<tr>
57-
<td colspan="3">
58-
<?php echo Text::_('MOD_POPULAR_NO_MATCHING_RESULTS'); ?>
59-
</td>
60-
</tr>
66+
<?php
67+
// If there are no articles to display, show empty state.
68+
$app->getLanguage()->load('com_content');
69+
70+
echo LayoutHelper::render('joomla.content.emptystate_module', [
71+
'textPrefix' => 'COM_CONTENT',
72+
'icon' => 'icon-copy',
73+
]);
74+
?>
75+
6176
<?php endif; ?>
62-
</tbody>
63-
</table>
77+
<?php endif; ?>

0 commit comments

Comments
 (0)