public function ResetFacetsProcessor::build in Facets 8
Alter the items in the summary before creating the renderable array.
Parameters
\Drupal\facets_summary\FacetsSummaryInterface $facet: The facet being changed.
array $build: The render array.
\Drupal\facets\FacetInterface[] $facets: The facets that are available.
Return value
array The render array.
Overrides BuildProcessorInterface::build
File
- modules/
facets_summary/ src/ Plugin/ facets_summary/ processor/ ResetFacetsProcessor.php, line 29
Class
- ResetFacetsProcessor
- Provides a processor that adds a link to reset facet filters.
Namespace
Drupal\facets_summary\Plugin\facets_summary\processorCode
public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
$configuration = $facets_summary
->getProcessorConfigs()[$this
->getPluginId()];
$hasReset = FALSE;
// Do nothing if there are no selected facets.
if (empty($build['#items'])) {
return $build;
}
$request = \Drupal::requestStack()
->getMasterRequest();
$query_params = $request->query
->all();
// Bypass all active facets and remove them from the query parameters array.
foreach ($facets as $facet) {
$url_alias = $facet
->getUrlAlias();
$filter_key = $facet
->getFacetSourceConfig()
->getFilterKey() ?: 'f';
if ($facet
->getActiveItems()) {
// This removes query params when using the query url processor.
if (isset($query_params[$filter_key])) {
foreach ($query_params[$filter_key] as $delta => $param) {
if (strpos($param, $url_alias . ':') !== FALSE) {
unset($query_params[$filter_key][$delta]);
}
}
if (!$query_params[$filter_key]) {
unset($query_params[$filter_key]);
}
}
$hasReset = TRUE;
}
}
if (!$hasReset) {
return $build;
}
$url = Url::fromUserInput($facets_summary
->getFacetSource()
->getPath());
$url
->setOptions([
'query' => $query_params,
]);
// Check if reset link text is not set or it contains only whitespaces.
// Set text from settings or set default text.
if (empty($configuration['settings']['link_text']) || strlen(trim($configuration['settings']['link_text'])) === 0) {
$itemText = t('Reset');
}
else {
$itemText = $configuration['settings']['link_text'];
}
$item = (new Link($itemText, $url))
->toRenderable();
$item['#wrapper_attributes'] = [
'class' => [
'facet-summary-item--clear',
],
];
array_unshift($build['#items'], $item);
return $build;
}