heimrichhannot / contao-newsnavigation-bundle
A bundle to provide a navigation between news articles
Installs: 1 575
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 6
Forks: 0
Open Issues: 0
Type:contao-bundle
Requires
- php: ^8.1
- contao/core-bundle: ^4.13 || ^5.0
- contao/manager-plugin: ^2.0
- contao/news-bundle: ^4.13 || ^5.0
- symfony/dependency-injection: ^5.4 || ^6.0 || ^7.0
- symfony/deprecation-contracts: ^1.0 || ^2.0 || ^3.0
- symfony/event-dispatcher-contracts: ^1.0 || ^2.0 || ^3.0
- symfony/http-kernel: ^5.4 || ^6.0 || ^7.0
- symfony/translation-contracts: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- contao/contao-rector: dev-main
- phpstan/phpstan: ^1.8
- phpstan/phpstan-symfony: ^1.2
- rector/rector: ^1.0
- symplify/easy-coding-standard: ^12.1
README
A contao extension to provide a simple navigation between news articles. It add template variables to go from one news article to the next or the previous article. News article order is calculated by time property.
Features
- add Template variables to NewsReaderModule to jump between news articles
- customize article navigation with custom filters
Installation
Install via composer:
composer require heimrichhannot/contao-newsnavigation-bundle
Usage
The bundle provides two new variables for news reader templates: nextArticle
and previousArticle
.
Twig example:
{% if previousArticle|default %} <a href="{{ previousArticle.url }}" class="previous"> {{ previousArticle.label }} </a> {% endif %} {% if nextArticle|default %} <a href="{{ nextArticle.url }}" class="next"> {{ nextArticle.label }} </a> {% endif %}
HTML5 example:
<?php if ($this->previousArticle): ?> <a href="<?= $this->previousArticle->url ?>" title="<?= $this->previousArticle->title ?>"><?= $this->previousArticle->label ?></a> <?php endif; ?> <?php if ($this->nextArticle): ?> <a href="<?= $this->nextArticle->url ?>" title="<?= $this->nextArticle->title ?>"><?= $this->nextArticle->label ?></a> <?php endif; ?>
Customize article navigation
To customize which articles are shown as next and previous, you can use the NewsNavigationFilterEvent event.
It gets passed a filter instance and the ModuleModel
instance.
To modify the filter, use the methods of the filter object.
Example:
use HeimrichHannot\NewsNavigationBundle\Event\NewsNavigationFilterEvent; function __invoke(NewsNavigationFilterEvent $event): void { if ($event->model->someCustomTstamp) { $event->filter->setColumns(array_merge($event->filter->getColumns(), ['someCustomTstamp>=?'])); $event->filter->setValues(array_merge($event->filter->getValues(), [$event->model->someCustomTstamp])); } if (!empty(StringUtil::deserialize($event->moduleModel->categories, true))) { $filter->setColumns(array_merge($filter->getColumns(), ['tl_news.categories IN (?)'])); $filter->setValues(array_merge($filter->getValues(), StringUtil::deserialize($event->moduleModel->categories, true))); } }