GlossaryAZAllItemsProcessor.php in Search API AZ Glossary 8.4
File
src/Plugin/facets/processor/GlossaryAZAllItemsProcessor.php
View source
<?php
namespace Drupal\search_api_glossary\Plugin\facets\processor;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Drupal\facets\Result\Result;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Pager\PagerParametersInterface;
class GlossaryAZAllItemsProcessor extends ProcessorPluginBase implements BuildProcessorInterface, ContainerFactoryPluginInterface {
protected $pagerParams;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
return $instance
->setPagerParams($container
->get('pager.parameters'));
}
public function setPagerParams(PagerParametersInterface $pager_params) {
$this->pagerParams = $pager_params;
return $this;
}
public function build(FacetInterface $facet, array $results) {
$show_all_item_count = 0;
foreach ($results as $result) {
$show_all_item_count += $result
->getCount();
}
$show_all_item = new Result($facet, t('All')
->getUntranslatedString(), t('All'), $show_all_item_count);
$path = Request::create($facet
->getFacetSource()
->getPath());
$url = Url::createFromRequest($path);
$get_params = new ParameterBag($this->pagerParams
->getQueryParameters());
$facet_source_config = $facet
->getFacetSourceConfig();
$filterKey = $facet_source_config
->getFilterKey() ?: 'f';
$filter_params = $get_params
->get($filterKey, [], TRUE);
foreach ($filter_params as $key => $filter_param) {
unset($filter_params[$key]);
}
$get_params
->set($filterKey, array_values($filter_params));
$url
->setOption('query', $get_params
->all());
$show_all_item
->setUrl($url);
if (empty($facet
->getActiveItems())) {
$show_all_item
->setActiveState(TRUE);
}
$results[] = $show_all_item;
return $results;
}
public function supportsFacet(FacetInterface $facet) {
$widget = $facet
->getWidget()['type'];
$search_processors = $facet
->getFacetSource()
->getIndex()
->getProcessors();
if ($widget == 'glossaryaz' && array_key_exists('glossary', $search_processors)) {
return TRUE;
}
return FALSE;
}
}