Skip to content

Commit

Permalink
group news by archive
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Jan 11, 2018
1 parent 1e69ad7 commit 73a67ff
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DependencyInjection/ContaoNewsRelatedExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ContaoNewsRelatedBundle\DependencyInjection;

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

class ContaoNewsRelatedExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('listener.yml');
}
}
42 changes: 42 additions & 0 deletions src/EventListener/NewsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ContaoNewsRelatedBundle\EventListener;

use Contao\DataContainer;
use Contao\NewsModel;

class NewsListener
{
/**
* Options callback for the related news selection.
*
* @param DataContainer $dc
*
* @return array
*/
public function relatedNewsOptionsCallback(DataContainer $dc)
{
$t = NewsModel::getTable();
$objNews = NewsModel::findAll(['order' => "$t.headline ASC"]);

$arrOptions = [];
foreach ($objNews as $news)
{
// skip self
if ('tl_news' == $dc->table && $dc->activeRecord && isset($dc->activeRecord->id) && $dc->activeRecord->id == $news->id)
{
continue;
}
if ('tl_news' == $dc->parentTable && $dc->activeRecord->pid == $news->id)
{
continue;
}

$arrOptions[$news->getRelated('pid')->title][$news->id] = $news->headline;
}

ksort($arrOptions);

return $arrOptions;
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/listener.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
contao_newsrelated.listener.news:
class: ContaoNewsRelatedBundle\EventListener\NewsListener
1 change: 1 addition & 0 deletions src/Resources/contao/dca/tl_news.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'label' => &$GLOBALS['TL_LANG']['tl_news']['relatedNews'],
'exclude' => true,
'inputType' => 'select',
'options_callback' => array('ContaoNewsRelatedBundle\EventListener\NewsListener', 'relatedNewsOptionsCallback'),
'foreignKey' => 'tl_news.headline',
'eval' => array('multiple'=>true, 'chosen'=>true, 'tl_style'=>'height:auto', 'tl_class'=>'clr'),
'relation' => array('type'=>'belongsToMany', 'load'=>'lazy'),
Expand Down

0 comments on commit 73a67ff

Please sign in to comment.