Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert mod_related_items to new structure #39628

Merged
merged 37 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2c8fada
adding service provider
carlitorweb Jan 13, 2023
5b26cdd
adding the dispatcher
carlitorweb Jan 13, 2023
3528b61
declare main function
carlitorweb Jan 13, 2023
0138895
Help class definition
carlitorweb Jan 13, 2023
4d11f90
layout adaptation
carlitorweb Jan 13, 2023
92b5dac
not used
carlitorweb Jan 13, 2023
b4d5848
fix some errors
carlitorweb Jan 14, 2023
d48bece
adding cache
carlitorweb Jan 14, 2023
66fdb3b
cs fix
carlitorweb Jan 14, 2023
7d7fc38
Merge branch '4.3-dev' into convert_mod_related_items
carlitorweb Jan 22, 2023
033e531
Merge branch '4.3-dev' into convert_mod_related_items
carlitorweb Jan 22, 2023
d4769f8
Merge branch '4.3-dev' into convert_mod_related_items
carlitorweb Mar 11, 2023
eb52b74
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Mar 11, 2023
cdbddb5
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Mar 28, 2023
9120fbb
update
carlitorweb Mar 28, 2023
dc371de
cs
carlitorweb Mar 29, 2023
f4f86e3
Merge remote-tracking branch 'origin/convert_mod_related_items' into …
carlitorweb Mar 29, 2023
b88e8dc
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Apr 1, 2023
b939bf8
update
carlitorweb Apr 1, 2023
c8ecf8f
revert Helper
carlitorweb Apr 1, 2023
9da27fa
cy test
carlitorweb Apr 1, 2023
a4a6fe9
cs
carlitorweb Apr 1, 2023
fae9a08
javascript cs
carlitorweb Apr 1, 2023
035fd6d
cy test without need article ID
carlitorweb Apr 1, 2023
3bd9d55
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Apr 3, 2023
4de75cc
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Apr 11, 2023
24a98b5
cache
carlitorweb Apr 11, 2023
df4bfcc
Merge branch '4.4-dev' into convert_mod_related_items
laoneo Apr 14, 2023
993d33d
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Apr 14, 2023
b4a595c
revert tmpl
carlitorweb Apr 14, 2023
6bfd8a4
revert tmpl
carlitorweb Apr 14, 2023
1900044
revert tmpl
carlitorweb Apr 14, 2023
3959777
separate test code
laoneo Apr 14, 2023
b486542
Merge branch '4.4-dev' into convert_mod_related_items
laoneo Apr 18, 2023
45eea40
Merge branch '4.4-dev' into convert_mod_related_items
carlitorweb Apr 18, 2023
f3c2f72
Merge branch '4.4-dev' into convert_mod_related_items
laoneo Apr 18, 2023
09027d0
Merge branch '4.4-dev' into convert_mod_related_items
laoneo Apr 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions modules/mod_related_items/mod_related_items.php

This file was deleted.

2 changes: 1 addition & 1 deletion modules/mod_related_items/mod_related_items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<description>MOD_RELATED_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Module\RelatedItems</namespace>
<files>
<filename module="mod_related_items">mod_related_items.php</filename>
<folder module="mod_related_items">services</folder>
<folder>src</folder>
<folder>tmpl</folder>
</files>
Expand Down
41 changes: 41 additions & 0 deletions modules/mod_related_items/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @package Joomla.Site
* @subpackage mod_related_items
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\Service\Provider\HelperFactory;
use Joomla\CMS\Extension\Service\Provider\Module;
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

/**
* The articles related module service provider.
*
* @since __DEPLOY_VERSION__
*/
return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\RelatedItems'));
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\RelatedItems\\Site\\Helper'));

$container->registerServiceProvider(new Module());
}
};
55 changes: 55 additions & 0 deletions modules/mod_related_items/src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* @package Joomla.Site
* @subpackage mod_related_items
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Module\RelatedItems\Site\Dispatcher;

use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
use Joomla\CMS\Helper\ModuleHelper;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Dispatcher class for mod_articles_popular
*
* @since __DEPLOY_VERSION__
*/
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
use HelperFactoryAwareTrait;

/**
* Returns the layout data.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function getLayoutData(): array
{
$data = parent::getLayoutData();
$params = $data['params'];

$cacheParams = new \stdClass();
$cacheParams->cachemode = 'safeuri';
$cacheParams->class = $this->getHelperFactory()->getHelper('RelatedItemsHelper');
$cacheParams->method = 'getRelatedArticles';
$cacheParams->methodparams = [$params, $data['app']];
$cacheParams->modeparams = ['id' => 'int', 'Itemid' => 'int'];

$data['list'] = ModuleHelper::moduleCache($this->module, $params, $cacheParams);
$data['showDate'] = $params->get('showDate', 0);

return $data;
}
}
52 changes: 42 additions & 10 deletions modules/mod_related_items/src/Helper/RelatedItemsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

namespace Joomla\Module\RelatedItems\Site\Helper;

use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Administrator\Extension\ContentComponent;
use Joomla\Component\Content\Site\Helper\RouteHelper;
use Joomla\Component\Content\Site\Model\ArticlesModel;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -27,26 +32,30 @@
*
* @since 1.5
*/
abstract class RelatedItemsHelper
class RelatedItemsHelper implements DatabaseAwareInterface
{
use DatabaseAwareTrait;

/**
* Get a list of related articles
* Retrieve a list of related articles based on the metakey field
*
* @param \Joomla\Registry\Registry &$params module parameters
* @param Registry $params The module parameters.
* @param SiteApplication $app The current application.
*
* @return array
* @return \stdClass[]
*
* @since __DEPLOY_VERSION__
*/
public static function getList(&$params)
public function getRelatedArticles(Registry $params, SiteApplication $app): array
{
$db = Factory::getDbo();
$app = Factory::getApplication();
$db = $this->getDatabase();
$input = $app->getInput();
$groups = Factory::getUser()->getAuthorisedViewLevels();
$groups = $app->getIdentity()->getAuthorisedViewLevels();
$maximum = (int) $params->get('maximum', 5);
$factory = $app->bootComponent('com_content')->getMVCFactory();

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

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

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

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

return $related;
}

/**
* Get a list of related articles
*
* @param Registry &$params module parameters
*
* @return array
*
* @since 1.6
*
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
* Use the non-static method getRelatedArticles
* Example: Factory::getApplication()->bootModule('mod_related_items', 'site')
* ->getHelper('RelatedItemsHelper')
* ->getRelatedArticles($params, Factory::getApplication())
*/
public static function getList(&$params)
{
/** @var SiteApplication $app */
$app = Factory::getApplication();

return (new self())->getRelatedArticles($params, $app);
}
}
24 changes: 15 additions & 9 deletions modules/mod_related_items/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

if (!count($list)) {
laoneo marked this conversation as resolved.
Show resolved Hide resolved
return;
}

?>
<ul class="mod-relateditems relateditems mod-list">
<?php foreach ($list as $item) : ?>
<li>
<a href="<?php echo $item->route; ?>">
<?php if ($showDate) {
echo HTMLHelper::_('date', $item->created, Text::_('DATE_FORMAT_LC4')) . ' - ';
} ?>
<?php echo $item->title; ?></a>
</li>
<?php endforeach; ?>
<?php foreach ($list as $item) : ?>
<li>
<a href="<?php echo $item->route; ?>">
<?php if ($showDate) : ?>
<?php echo HTMLHelper::_('date', $item->created, Text::_('DATE_FORMAT_LC4')) . ' - '; ?>
<?php endif; ?>

<?php echo $item->title; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('Test the related items module', () => {
it('can load in frontend showing a list of related articles based on the metakey field', () => {
cy.db_createArticle({ title: 'Main Article', metakey: 'joomla', featured: 1 })
.then(() => cy.db_createArticle({ title: 'article with joomla keyword', metakey: 'joomla' }))
.then(() => cy.db_createModule({ module: 'mod_related_items' }))
.then(() => {
cy.visit('/');

cy.contains('a', 'Main Article').click();
cy.contains('li', 'article with joomla keyword');
});
});
});