Skip to content

Commit

Permalink
changes for category and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Patzer committed Aug 1, 2019
1 parent 85deb5b commit dd91f90
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/EventListener/AdjustFilterValueEventListener.php
Expand Up @@ -8,6 +8,7 @@

namespace HeimrichHannot\ReaderBundle\EventListener;

use Contao\Database;
use Contao\System;
use HeimrichHannot\FilterBundle\Event\AdjustFilterValueEvent;
use HeimrichHannot\ReaderBundle\Backend\FilterConfigElement;
Expand All @@ -18,6 +19,8 @@ class AdjustFilterValueEventListener
public function onAdjustFilterValue(AdjustFilterValueEvent $event, string $eventName, EventDispatcherInterface $dispatcher)
{
$element = $event->getElement();
$dca = $event->getDca();
$table = $event->getConfig()->getFilter()['dataContainer'];

if (FilterConfigElement::ALTERNATIVE_SOURCE_READER_BUNDLE_ENTITY !== $element->alternativeValueSource ||
null === ($readerConfig = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk(
Expand All @@ -31,9 +34,36 @@ public function onAdjustFilterValue(AdjustFilterValueEvent $event, string $event
}

$instance = System::getContainer()->get('huh.reader.manager.reader')->retrieveItem();
$value = null;

$value = $instance->{$element->readerField};
$value = array_filter(!\is_array($value) ? explode(',', $value) : $value);
if ($dca['eval']['isCategoryField']) {
$value = [];

$associations = System::getContainer()->get('huh.categories.manager')->findAssociationsByParentTableAndEntityAndField(
$table, $instance->getRawValue('id'), $element->field
);

if (null !== $associations) {
$value = $associations->fetchEach('category');
}
} elseif ('cfgTags' === $dca['inputType']) {
$value = [];
$relationTable = $dca['relation']['relationTable'];
$idName = str_replace('tl_', '', $table).'_id';

$associations = Database::getInstance()->prepare(
"SELECT cfg_tag_id FROM $relationTable WHERE $idName = ?"
)->execute($instance->getRawValue('id'));

if ($associations->numRows > 0) {
while ($associations->next()) {
$value[] = $associations->cfg_tag_id;
}
}
} else {
$value = $instance->{$element->readerField};
$value = array_filter(!\is_array($value) ? explode(',', $value) : $value);
}

$event->setValue($value);
}
Expand Down

0 comments on commit dd91f90

Please sign in to comment.