Skip to content

Commit a22305d

Browse files
authored
[5.4] Convert mod_latest to service provider (joomla#45762)
mod_latest converted to service provider
1 parent f75d5a3 commit a22305d

File tree

5 files changed

+204
-49
lines changed

5 files changed

+204
-49
lines changed

administrator/modules/mod_latest/mod_latest.php

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

administrator/modules/mod_latest/mod_latest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_LATEST_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\Latest</namespace>
1313
<files>
14-
<filename module="mod_latest">mod_latest.php</filename>
14+
<folder module="mod_latest">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_latest
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\\Latest'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Latest\\Administrator\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage mod_latest
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\Latest\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\CMS\Helper\ModuleHelper;
18+
use Joomla\CMS\Layout\LayoutHelper;
19+
20+
// phpcs:disable PSR1.Files.SideEffects
21+
\defined('_JEXEC') or die;
22+
// phpcs:enable PSR1.Files.SideEffects
23+
24+
/**
25+
* Dispatcher class for mod_latest
26+
*
27+
* @since __DEPLOY_VERSION__
28+
*/
29+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
30+
{
31+
use HelperFactoryAwareTrait;
32+
33+
/**
34+
* Runs the dispatcher.
35+
*
36+
* @return void
37+
*
38+
* @since __DEPLOY_VERSION_
39+
*/
40+
public function dispatch()
41+
{
42+
$this->loadLanguage();
43+
44+
$displayData = $this->getLayoutData();
45+
46+
// Stop when display data is false
47+
if ($displayData === false) {
48+
return;
49+
}
50+
51+
// Execute the layout without the module context
52+
$loader = static function (array $displayData) {
53+
// If $displayData doesn't exist in extracted data, unset the variable.
54+
if (!\array_key_exists('displayData', $displayData)) {
55+
extract($displayData);
56+
unset($displayData);
57+
} else {
58+
extract($displayData);
59+
}
60+
61+
/**
62+
* Extracted variables
63+
* -----------------
64+
* @var \stdClass $module
65+
* @var Registry $params
66+
*/
67+
68+
if (\count($list)) {
69+
require ModuleHelper::getLayoutPath('mod_latest', $params->get('layout', 'default'));
70+
} else {
71+
$app->getLanguage()->load('com_content');
72+
73+
echo LayoutHelper::render('joomla.content.emptystate_module', [
74+
'textPrefix' => 'COM_CONTENT',
75+
'icon' => 'icon-copy',
76+
]);
77+
}
78+
// End of extracted variables
79+
};
80+
81+
$loader($displayData);
82+
}
83+
84+
/**
85+
* Returns the layout data.
86+
*
87+
* @return array
88+
*
89+
* @since __DEPLOY_VERSION_
90+
*/
91+
protected function getLayoutData()
92+
{
93+
$data = parent::getLayoutData();
94+
$helper = $this->getHelperFactory()->getHelper('LatestHelper');
95+
96+
$model = $data['app']->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Administrator', ['ignore_request' => true]);
97+
$data['list'] = $helper->getArticles($data['params'], $model, $data['app']);
98+
$data['workflow_enabled'] = ComponentHelper::getParams('com_content')->get('workflow_enabled');
99+
100+
if ($data['workflow_enabled']) {
101+
$data['app']->getLanguage()->load('com_workflow');
102+
}
103+
104+
if ($data['params']->get('automatic_title', 0)) {
105+
$data['module']->title = $helper->getModuleTitle($data['params'], $data['app']);
106+
}
107+
108+
return $data;
109+
}
110+
}

administrator/modules/mod_latest/src/Helper/LatestHelper.php

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

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

13-
use Joomla\CMS\Categories\Categories;
13+
use Joomla\CMS\Application\CMSApplicationInterface;
1414
use Joomla\CMS\Factory;
1515
use Joomla\CMS\Language\Text;
1616
use Joomla\CMS\Router\Route;
@@ -26,19 +26,22 @@
2626
*
2727
* @since 1.5
2828
*/
29-
abstract class LatestHelper
29+
class LatestHelper
3030
{
3131
/**
3232
* Get a list of articles.
3333
*
34-
* @param Registry $params The module parameters.
35-
* @param ArticlesModel $model The model.
34+
* @param Registry $params The module parameters.
35+
* @param ArticlesModel $model The model.
36+
* @param CMSApplicationInterface $app The application instance.
3637
*
3738
* @return mixed An array of articles, or false on error.
39+
*
40+
* @since __DEPLOY_VERSION__
3841
*/
39-
public static function getList(Registry $params, ArticlesModel $model)
42+
public function getArticles(Registry $params, ArticlesModel $model, CMSApplicationInterface $app): mixed
4043
{
41-
$user = Factory::getUser();
44+
$user = $app->getIdentity();
4245

4346
// Set List SELECT
4447
$model->setState('list.select', 'a.id, a.title, a.checked_out, a.checked_out_time, a.modified,' .
@@ -107,19 +110,22 @@ public static function getList(Registry $params, ArticlesModel $model)
107110
/**
108111
* Get the alternate title for the module.
109112
*
110-
* @param \Joomla\Registry\Registry $params The module parameters.
113+
* @param Registry $params The module parameters.
114+
* @param CMSApplicationInterface $app The application instance.
111115
*
112116
* @return string The alternate title for the module.
117+
*
118+
* @since __DEPLOY_VERSION__
113119
*/
114-
public static function getTitle($params)
120+
public function getModuleTitle(Registry $params, CMSApplicationInterface $app): string
115121
{
116122
$who = $params->get('user_id', 0);
117123
$catid = (int) $params->get('catid', null);
118124
$type = $params->get('ordering') === 'c_dsc' ? '_CREATED' : '_MODIFIED';
119125
$title = '';
120126

121127
if ($catid) {
122-
$category = Categories::getInstance('Content')->get($catid);
128+
$category = $app->bootComponent('com_content')->getCategory()->get($catid);
123129
$title = Text::_('MOD_POPULAR_UNEXISTING');
124130

125131
if ($category) {
@@ -133,4 +139,41 @@ public static function getTitle($params)
133139
$title
134140
);
135141
}
142+
143+
/**
144+
* Get a list of articles.
145+
*
146+
* @param Registry $params The module parameters.
147+
* @param ArticlesModel $model The model.
148+
*
149+
* @return mixed An array of articles, or false on error.
150+
*
151+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
152+
* Use the non-static method getArticles
153+
* Example: Factory::getApplication()->bootModule('mod_latest', 'administrator')
154+
* ->getHelper('LatestHelper')
155+
* ->getArticles($params, $model, Factory::getApplication())
156+
*/
157+
public static function getList(Registry $params, ArticlesModel $model)
158+
{
159+
return (new self())->getArticles($params, $model, Factory::getApplication());
160+
}
161+
162+
/**
163+
* Get the alternate title for the module.
164+
*
165+
* @param Registry $params The module parameters.
166+
*
167+
* @return string The alternate title for the module.
168+
*
169+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
170+
* Use the non-static method getModuleTitle
171+
* Example: Factory::getApplication()->bootModule('mod_latest', 'administrator')
172+
* ->getHelper('LatestHelper')
173+
* ->getModuleTitle($params, Factory::getApplication())
174+
*/
175+
public static function getTitle($params)
176+
{
177+
return (new self())->getModuleTitle($params, Factory::getApplication());
178+
}
136179
}

0 commit comments

Comments
 (0)