-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new implementation with NewsFilterEvent
- Loading branch information
Showing
20 changed files
with
216 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/EventListener/DataContainer/NewsOrderOptionsCallback.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
; | ||
} | ||
} |
Oops, something went wrong.