Skip to content

Commit

Permalink
Merge pull request #3 from fritzmg/change-template-variable
Browse files Browse the repository at this point in the history
Change template variable instead of using custom templates
  • Loading branch information
ngdot authored Nov 3, 2021
2 parents dcb229c + 67efe4e commit 0d23065
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 53 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
"forum": "https://community.contao.org/de"
},
"require": {
"php": "^7.1",
"contao/core-bundle": "~4.4",
"contao/news-bundle": "~4.4"
"contao/news-bundle": "~4.4",
"symfony/config": "^3.4 || ^4.4 || ^5.1",
"symfony/dependency-injection": "^3.4 || ^4.4 || ^5.1",
"symfony/http-kernel": "^3.4 || ^4.4 || ^5.1"
},
"require-dev": {
"contao/manager-plugin": "^2.0"
Expand Down
26 changes: 26 additions & 0 deletions src/DependencyInjection/ContaoChangeNewsMoreExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the ContaoChangeNewsMore Bundle.
*
* (c) Nicolas Görlach <https://github.com/ngdot>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ContaoChangeNewsMoreBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class ContaoChangeNewsMoreExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yaml');
}
}
29 changes: 29 additions & 0 deletions src/EventListener/ParseArticlesListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the ContaoChangeNewsMore Bundle.
*
* (c) Nicolas Görlach <https://github.com/ngdot>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ContaoChangeNewsMoreBundle\EventListener;

use Contao\FrontendTemplate;
use Contao\Module;
use Contao\NewsModel;
use ContaoChangeNewsMoreBundle\NewsHelper;

class ParseArticlesListener
{
public function __invoke(FrontendTemplate $template, array $newsEntry, Module $module): void
{
if (empty($newsEntry['linktext'])) {
return;
}

$template->more = (new NewsHelper())->getReadMoreLink($newsEntry['linktext'], NewsModel::findByPk($newsEntry['id']));
}
}
30 changes: 30 additions & 0 deletions src/NewsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the ContaoChangeNewsMore Bundle.
*
* (c) Nicolas Görlach <https://github.com/ngdot>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ContaoChangeNewsMoreBundle;

use Contao\ModuleNews;
use Contao\NewsModel;

/**
* Helper class to expose ModuleNews::generateLink()
*/
class NewsHelper extends ModuleNews
{
public function __construct() {}

public function compile() {}

public function getReadMoreLink(string $linkText, NewsModel $news): string
{
return $this->generateLink($linkText, $news, false, true);
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
ContaoChangeNewsMoreBundle\EventListener\ParseArticlesListener:
public: true
5 changes: 5 additions & 0 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use ContaoChangeNewsMoreBundle\EventListener\ParseArticlesListener;

$GLOBALS['TL_HOOKS']['parseArticles'][] = [ParseArticlesListener::class, '__invoke'];
28 changes: 0 additions & 28 deletions src/Resources/contao/templates/news/news_latest.html5

This file was deleted.

24 changes: 0 additions & 24 deletions src/Resources/contao/templates/news/news_short.html5

This file was deleted.

0 comments on commit 0d23065

Please sign in to comment.