ShowSummaryProcessor.php in Facets 8
File
modules/facets_summary/src/Plugin/facets_summary/processor/ShowSummaryProcessor.php
View source
<?php
namespace Drupal\facets_summary\Plugin\facets_summary\processor;
use Drupal\facets_summary\FacetsSummaryInterface;
use Drupal\facets_summary\Processor\BuildProcessorInterface;
use Drupal\facets_summary\Processor\ProcessorPluginBase;
class ShowSummaryProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
$facets_config = $facets_summary
->getFacets();
if (!isset($build['#items'])) {
return $build;
}
foreach ($facets as $facet) {
if (empty($facet
->getActiveItems())) {
continue;
}
$items = $this
->getActiveDisplayValues($facet
->getResults());
$facet_summary = [
'#theme' => 'facets_summary_facet',
'#label' => $facets_config[$facet
->id()]['label'],
'#separator' => $facets_config[$facet
->id()]['separator'],
'#items' => $items,
'#facet_id' => $facet
->id(),
'#facet_admin_label' => $facet
->getName(),
];
array_unshift($build['#items'], $facet_summary);
}
return $build;
}
protected function getActiveDisplayValues(array $results) {
$items = [];
foreach ($results as $result) {
if ($result
->isActive()) {
$items[] = $result
->getDisplayValue();
}
if ($result
->hasActiveChildren()) {
$items = array_merge($items, $this
->getActiveDisplayValues($result
->getChildren()));
}
}
return $items;
}
}