Skip to content

Commit

Permalink
new implementation with NewsFilterEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Apr 6, 2023
1 parent b66d7fd commit a3909a8
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 295 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# OS
.DS_Store
Thumbs.db
Thumbs.db
/composer.lock
/vendor/
/node_modules/
6 changes: 4 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

$header = <<<EOF
This file is part of the ContaoNewsRelated bundle.
This file is part of the Contao News Related extension.
(c) fritzmg
(c) inspiredminds
@license LGPL-3.0-or-later
EOF;

$finder = PhpCsFixer\Finder::create()
Expand Down
20 changes: 15 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@
}
],
"require":{
"php": ">=7.1",
"contao/core-bundle": "^4.9 || ^5.0"
"php": ">=7.4",
"contao/core-bundle": "^4.9 || ^5.0",
"friendsofphp/php-cs-fixer": "^3.0",
"inspiredminds/contao-news-filter-event": "^1.1"
},
"require-dev": {
"contao/manager-plugin": "^2.0"
"contao/manager-plugin": "^2.0",
"rector/rector": "^0.14.5"
},
"autoload": {
"psr-4": {
"ContaoNewsRelatedBundle\\": "src/"
"InspiredMinds\\ContaoNewsRelated\\": "src/"
}
},
"extra": {
"contao-manager-plugin": "ContaoNewsRelatedBundle\\ContaoManager\\Plugin"
"contao-manager-plugin": "InspiredMinds\\ContaoNewsRelated\\ContaoManager\\Plugin"
},
"config": {
"allow-plugins": {
"contao-components/installer": true,
"php-http/discovery": false,
"contao/manager-plugin": true
}
}
}
13 changes: 13 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
_defaults:
autowire: true
autoconfigure: true

InspiredMinds\ContaoNewsRelated\:
resource: ../src

InspiredMinds\ContaoNewsRelated\EventListener\DataContainer\NewsOrderOptionsCallback:
arguments: ['@?InspiredMinds\ContaoNewsSorting\EventListener\ModuleDataContainerListener']

InspiredMinds\ContaoNewsRelated\EventListener\NewsFilterEventListener:
tags: ['kernel.event_listener']
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Contao\CoreBundle\DataContainer\PaletteManipulator;

/*
* This file is part of the ContaoNewsRelated bundle.
*
Expand Down Expand Up @@ -32,12 +34,12 @@
'sql' => "char(1) NOT NULL default ''",
];

$GLOBALS['TL_DCA']['tl_module']['fields']['news_order']['options_callback'] = ['contao_newsrelated.listener.news', 'newsOrderOptionsCallback'];
$GLOBALS['TL_DCA']['tl_module']['fields']['news_order']['reference'] = &$GLOBALS['TL_LANG']['tl_module'];

\Contao\CoreBundle\DataContainer\PaletteManipulator::create()
->addField('relatedOnly', 'config_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('includeCurrent', 'config_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('disableEmpty', 'config_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
PaletteManipulator::create()
->addLegend('news_related_legend', 'config_legend', PaletteManipulator::POSITION_AFTER, true)
->addField('relatedOnly', 'news_related_legend', PaletteManipulator::POSITION_APPEND)
->addField('includeCurrent', 'news_related_legend', PaletteManipulator::POSITION_APPEND)
->addField('disableEmpty', 'news_related_legend', PaletteManipulator::POSITION_APPEND)
->applyToPalette('newslist', 'tl_module')
;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@
* (c) fritzmg
*/

$GLOBALS['TL_LANG']['tl_module']['news_related_legend'] = 'Verwandte Nachrichten';
$GLOBALS['TL_LANG']['tl_module']['relatedOnly'] = ['Nur verwandte Einträge anzeigen', 'Beschränkt die Ausgabe auf verwandte Artikel.'];
$GLOBALS['TL_LANG']['tl_module']['includeCurrent'] = ['Aktuellen Artikel miteinbeziehen', 'In der Liste erscheint auch der aktuelle Artikel an erster Stelle.'];
$GLOBALS['TL_LANG']['tl_module']['disableEmpty'] = ['Keine leere Ausgabe', 'Zeigt alle Nachrichtenartikel an, wenn keine verwandten Artikel gefunden wurden.'];

$GLOBALS['TL_LANG']['tl_module']['order_date_asc'] = 'Datum (aufsteigend)';
$GLOBALS['TL_LANG']['tl_module']['order_date_desc'] = 'Datum (absteigend)';
$GLOBALS['TL_LANG']['tl_module']['order_headline_asc'] = 'Titel (aufsteigend)';
$GLOBALS['TL_LANG']['tl_module']['order_headline_desc'] = 'Titel (absteigend)';
$GLOBALS['TL_LANG']['tl_module']['order_random'] = 'Zufällig';
$GLOBALS['TL_LANG']['tl_module']['order_random_date_desc'] = 'Zufällig (Datum absteigend)';
$GLOBALS['TL_LANG']['tl_module']['order_featured_desc'] = 'Hervorgehoben (Datum absteigend)';
$GLOBALS['TL_LANG']['tl_module']['order_related'] = 'Verwandte Liste';
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@
* (c) fritzmg
*/

$GLOBALS['TL_LANG']['tl_module']['news_related_legend'] = 'Related news';
$GLOBALS['TL_LANG']['tl_module']['relatedOnly'] = ['Show only related entries', 'Limits the output to related entries.'];
$GLOBALS['TL_LANG']['tl_module']['includeCurrent'] = ['Include current article', 'The current article will be shown in the list as well as the first element.'];
$GLOBALS['TL_LANG']['tl_module']['disableEmpty'] = ['Disable empty output', 'Shows all news entries, if no related entries have been found.'];

$GLOBALS['TL_LANG']['tl_module']['order_date_asc'] = 'Date (ascending)';
$GLOBALS['TL_LANG']['tl_module']['order_date_desc'] = 'Date (descending)';
$GLOBALS['TL_LANG']['tl_module']['order_headline_asc'] = 'Headline (ascending)';
$GLOBALS['TL_LANG']['tl_module']['order_headline_desc'] = 'Headline (descending)';
$GLOBALS['TL_LANG']['tl_module']['order_random'] = 'Random';
$GLOBALS['TL_LANG']['tl_module']['order_random_date_desc'] = 'Random (Date descending)';
$GLOBALS['TL_LANG']['tl_module']['order_featured_desc'] = 'Featured (Date descending)';
$GLOBALS['TL_LANG']['tl_module']['order_related'] = 'Related list';
File renamed without changes.
17 changes: 17 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
]);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74
]);
};
12 changes: 7 additions & 5 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
declare(strict_types=1);

/*
* This file is part of the ContaoNewsRelated bundle.
* This file is part of the Contao News Related extension.
*
* (c) fritzmg
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace ContaoNewsRelatedBundle\ContaoManager;
namespace InspiredMinds\ContaoNewsRelated\ContaoManager;

use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Contao\NewsBundle\ContaoNewsBundle;
use ContaoNewsRelatedBundle\ContaoNewsRelatedBundle;
use InspiredMinds\ContaoNewsRelated\ContaoNewsRelatedBundle;

/**
* Plugin for the Contao Manager.
Expand All @@ -28,7 +30,7 @@ public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(ContaoNewsRelatedBundle::class)
->setLoadAfter([ContaoNewsBundle::class, 'news_sorting']),
->setLoadAfter([ContaoNewsBundle::class]),
];
}
}
15 changes: 9 additions & 6 deletions src/ContaoNewsRelatedBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
declare(strict_types=1);

/*
* This file is part of the ContaoNewsRelated bundle.
* This file is part of the Contao News Related extension.
*
* (c) fritzmg
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace ContaoNewsRelatedBundle;
namespace InspiredMinds\ContaoNewsRelated;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Configures the ContaoNewsRelated Bundle.
*/
class ContaoNewsRelatedBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
12 changes: 7 additions & 5 deletions src/DependencyInjection/ContaoNewsRelatedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
declare(strict_types=1);

/*
* This file is part of the ContaoNewsRelated bundle.
* This file is part of the Contao News Related extension.
*
* (c) fritzmg
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace ContaoNewsRelatedBundle\DependencyInjection;
namespace InspiredMinds\ContaoNewsRelated\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -22,7 +24,7 @@ class ContaoNewsRelatedExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('listener.yml');
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.yaml');
}
}
45 changes: 45 additions & 0 deletions src/EventListener/DataContainer/NewsOrderOptionsCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao News Related extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoNewsRelated\EventListener\DataContainer;

use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\DataContainer;
use InspiredMinds\ContaoNewsSorting\EventListener\ModuleDataContainerListener;

/**
* @Callback(table="tl_module", target="fields.news_order.options", priority=100)
*/
class NewsOrderOptionsCallback
{
private $newsSortingOptionsListener;

public function __construct(ModuleDataContainerListener $newsSortingOptionsListener = null)
{
$this->newsSortingOptionsListener = $newsSortingOptionsListener;
}

public function __invoke(DataContainer $dc): array
{
$defaultOptions = (new \tl_module_news())->getSortingOptions($dc);

if ($dc->activeRecord && 'newsmenu' === $dc->activeRecord->type) {
return $defaultOptions;
}

if (null !== $this->newsSortingOptionsListener) {
return array_merge($this->newsSortingOptionsListener->getSortingOptions($dc), ['order_related']);
}

return array_merge($defaultOptions, ['order_related']);
}
}
86 changes: 86 additions & 0 deletions src/EventListener/NewsFilterEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao News Related extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoNewsRelated\EventListener;

use Contao\Input;
use Contao\NewsModel;
use Contao\StringUtil;
use InspiredMinds\ContaoNewsFilterEvent\Event\NewsFilterEvent;

class NewsFilterEventListener
{
public function __invoke(NewsFilterEvent $event): void
{
$module = $event->getModule();

if (!$module->relatedOnly) {
return;
}

$archives = $event->getArchives();

if (empty($archives)) {
$this->forceEmptyResultIfApplicable($event);

return;
}

$newsAlias = Input::get('auto_item', false, true);

if (empty($newsAlias)) {
$this->forceEmptyResultIfApplicable($event);

return;
}

$news = NewsModel::findByAlias($newsAlias);

if (null === $news) {
$this->forceEmptyResultIfApplicable($event);

return;
}

$related = array_map('intval', StringUtil::deserialize($news->relatedNews, true));

if (empty($related)) {
$this->forceEmptyResultIfApplicable($event);

return;
}

// Add current element
if ($module->includeCurrent) {
array_unshift($related, (int) $news->id);
}

$event->addColumn('tl_news.id IN ('.implode(',', $related).')');

// Set sorting
if ('order_related' === $module->news_order) {
$event->addOption('order', 'FIELD(tl_news.id,'.implode(',', $related).')', true);
}
}

private function forceEmptyResultIfApplicable(NewsFilterEvent $event): void
{
if ($event->getModule()->disableEmpty) {
return;
}

$event
->setForceEmptyResult(true)
->stopPropagation()
;
}
}
Loading

0 comments on commit a3909a8

Please sign in to comment.