Skip to content

Commit 928eaea

Browse files
authored
[5.4] Convert site mod_menu to service provider (joomla#45851)
* mod_menu converted to service provider
1 parent 552ee57 commit 928eaea

File tree

5 files changed

+219
-51
lines changed

5 files changed

+219
-51
lines changed

modules/mod_menu/mod_menu.php

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

modules/mod_menu/mod_menu.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_MENU_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\Menu</namespace>
1313
<files>
14-
<filename module="mod_menu">mod_menu.php</filename>
14+
<folder module="mod_menu">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_menu
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 menu 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\\Menu'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Menu\\Site\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_menu
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\Menu\Site\Dispatcher;
12+
13+
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
14+
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
15+
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
16+
17+
// phpcs:disable PSR1.Files.SideEffects
18+
\defined('_JEXEC') or die;
19+
// phpcs:enable PSR1.Files.SideEffects
20+
21+
/**
22+
* Dispatcher class for mod_menu
23+
*
24+
* @since __DEPLOY_VERSION__
25+
*/
26+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
27+
{
28+
use HelperFactoryAwareTrait;
29+
30+
/**
31+
* Runs the dispatcher.
32+
*
33+
* @return void
34+
*
35+
* @since __DEPLOY_VERSION__
36+
*/
37+
public function dispatch()
38+
{
39+
$displayData = $this->getLayoutData();
40+
41+
if (!$displayData['list']) {
42+
return;
43+
}
44+
45+
parent::dispatch();
46+
}
47+
48+
/**
49+
* Returns the layout data.
50+
*
51+
* @return array
52+
*
53+
* @since __DEPLOY_VERSION__
54+
*/
55+
protected function getLayoutData()
56+
{
57+
$data = parent::getLayoutData();
58+
59+
$menuHelper = $this->getHelperFactory()->getHelper('MenuHelper');
60+
61+
$data['list'] = $menuHelper->getItems($data['params'], $data['app']);
62+
$data['base'] = $menuHelper->getBaseItem($data['params'], $data['app']);
63+
$data['active'] = $menuHelper->getActiveItem($data['app']);
64+
$data['default'] = $menuHelper->getDefaultItem($data['app']);
65+
$data['active_id'] = $data['active']->id;
66+
$data['default_id'] = $data['default']->id;
67+
$data['path'] = $data['base']->tree;
68+
$data['showAll'] = $data['params']->get('showAllChildren', 1);
69+
$data['class_sfx'] = htmlspecialchars($data['params']->get('class_sfx', ''), ENT_COMPAT, 'UTF-8');
70+
71+
return $data;
72+
}
73+
}

modules/mod_menu/src/Helper/MenuHelper.php

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

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

13+
use Joomla\CMS\Application\CMSApplicationInterface;
1314
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
1415
use Joomla\CMS\Cache\Controller\OutputController;
1516
use Joomla\CMS\Factory;
1617
use Joomla\CMS\Language\Multilanguage;
1718
use Joomla\CMS\Router\Route;
19+
use Joomla\Registry\Registry;
1820

1921
// phpcs:disable PSR1.Files.SideEffects
2022
\defined('_JEXEC') or die;
@@ -30,20 +32,20 @@ class MenuHelper
3032
/**
3133
* Get a list of the menu items.
3234
*
33-
* @param \Joomla\Registry\Registry &$params The module options.
35+
* @param Registry &$params The module options.
36+
* @param CMSApplicationInterface $app The application
3437
*
3538
* @return array
3639
*
37-
* @since 1.5
40+
* @since __DEPLOY_VERSION__
3841
*/
39-
public static function getList(&$params)
42+
public function getItems(Registry &$params, CMSApplicationInterface $app): array
4043
{
41-
$app = Factory::getApplication();
4244
$menu = $app->getMenu();
4345

4446
// Get active menu item
45-
$base = self::getBase($params);
46-
$levels = Factory::getUser()->getAuthorisedViewLevels();
47+
$base = $this->getBaseItem($params, $app);
48+
$levels = $app->getIdentity()->getAuthorisedViewLevels();
4749
asort($levels);
4850

4951
// Compose cache key
@@ -136,7 +138,7 @@ public static function getList(&$params)
136138

137139
// Get the language of the target menu item when site is multilingual
138140
if (Multilanguage::isEnabled()) {
139-
$newItem = Factory::getApplication()->getMenu()->getItem((int) $itemParams->get('aliasoptions'));
141+
$newItem = $app->getMenu()->getItem((int) $itemParams->get('aliasoptions'));
140142

141143
// Use language code if not set to ALL
142144
if ($newItem != null && $newItem->language && $newItem->language !== '*') {
@@ -183,24 +185,25 @@ public static function getList(&$params)
183185
/**
184186
* Get base menu item.
185187
*
186-
* @param \Joomla\Registry\Registry &$params The module options.
188+
* @param Registry &$params The module options.
189+
* @param CMSApplicationInterface $app The application
187190
*
188191
* @return object
189192
*
190-
* @since 3.0.2
193+
* @since __DEPLOY_VERSION__
191194
*/
192-
public static function getBase(&$params)
195+
public function getBaseItem(Registry &$params, CMSApplicationInterface $app): object
193196
{
194197
// Get base menu item from parameters
195198
if ($params->get('base')) {
196-
$base = Factory::getApplication()->getMenu()->getItem($params->get('base'));
199+
$base = $app->getMenu()->getItem($params->get('base'));
197200
} else {
198201
$base = false;
199202
}
200203

201204
// Use active menu item if no base found
202205
if (!$base) {
203-
$base = self::getActive($params);
206+
$base = $this->getActiveItem($app);
204207
}
205208

206209
return $base;
@@ -209,33 +212,114 @@ public static function getBase(&$params)
209212
/**
210213
* Get active menu item.
211214
*
212-
* @param \Joomla\Registry\Registry &$params The module options.
215+
* @param CMSApplicationInterface $app The application
213216
*
214217
* @return object
215218
*
216-
* @since 3.0.2
219+
* @since __DEPLOY_VERSION__
217220
*/
218-
public static function getActive(&$params)
221+
public function getActiveItem(CMSApplicationInterface $app): object
219222
{
220-
$menu = Factory::getApplication()->getMenu();
223+
$menu = $app->getMenu();
221224

222-
return $menu->getActive() ?: self::getDefault();
225+
return $menu->getActive() ?: $this->getDefaultItem($app);
223226
}
224227

225228
/**
226229
* Get default menu item (home page) for current language.
227230
*
231+
* @param CMSApplicationInterface $app The application
232+
*
228233
* @return object
234+
*
235+
* @since __DEPLOY_VERSION__
229236
*/
230-
public static function getDefault()
237+
public function getDefaultItem(CMSApplicationInterface $app): object
231238
{
232-
$menu = Factory::getApplication()->getMenu();
239+
$menu = $app->getMenu();
233240

234241
// Look for the home menu
235242
if (Multilanguage::isEnabled()) {
236-
return $menu->getDefault(Factory::getLanguage()->getTag());
243+
return $menu->getDefault($app->getLanguage()->getTag());
237244
}
238245

239246
return $menu->getDefault();
240247
}
248+
249+
250+
/**
251+
* Get a list of the menu items.
252+
*
253+
* @param Registry &$params The module options.
254+
*
255+
* @return array
256+
*
257+
* @since 1.5
258+
*
259+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
260+
* Use the non-static method getItems
261+
* Example: Factory::getApplication()->bootModule('mod_menu', 'site')
262+
* ->getHelper('MenuHelper')
263+
* ->getItems($params, $app)
264+
*/
265+
public static function getList(&$params)
266+
{
267+
return (new self())->getItems($params, Factory::getApplication());
268+
}
269+
270+
/**
271+
* Get base menu item.
272+
*
273+
* @param Registry &$params The module options.
274+
*
275+
* @return object
276+
*
277+
* @since 3.0.2
278+
*
279+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
280+
* Use the non-static method getBaseItem
281+
* Example: Factory::getApplication()->bootModule('mod_menu', 'site')
282+
* ->getHelper('MenuHelper')
283+
* ->getBaseItem($params, $app)
284+
*/
285+
public static function getBase(&$params)
286+
{
287+
return (new self())->getBaseItem($params, Factory::getApplication());
288+
}
289+
290+
/**
291+
* Get active menu item.
292+
*
293+
* @param Registry &$params The module options.
294+
*
295+
* @return object
296+
*
297+
* @since 3.0.2
298+
*
299+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
300+
* Use the non-static method getActiveItem
301+
* Example: Factory::getApplication()->bootModule('mod_menu', 'site')
302+
* ->getHelper('MenuHelper')
303+
* ->getActiveItem($app)
304+
*/
305+
public static function getActive(&$params)
306+
{
307+
return (new self())->getActiveItem(Factory::getApplication());
308+
}
309+
310+
/**
311+
* Get default menu item (home page) for current language.
312+
*
313+
* @return object
314+
*
315+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
316+
* Use the non-static method getDefaultItem
317+
* Example: Factory::getApplication()->bootModule('mod_menu', 'site')
318+
* ->getHelper('MenuHelper')
319+
* ->getDefaultItem($app)
320+
*/
321+
public static function getDefault()
322+
{
323+
return (new self())->getDefaultItem(Factory::getApplication());
324+
}
241325
}

0 commit comments

Comments
 (0)