Commit 478953b0 authored by StryKaizer's avatar StryKaizer Committed by borisson_

Issue #2912111 by borisson_, StryKaizer: Refactor breadcrumb code with new url generation

parent 1cc44f10
......@@ -238,6 +238,8 @@ function facets_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInter
$facet_sources_definitions = $facet_source_manager->getDefinitions();
$facets_url_generator = \Drupal::service('facets.utility.url_generator');
// No facet sources found, so don't do anything.
if (empty($facet_sources_definitions)) {
return;
......@@ -300,13 +302,13 @@ function facets_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInter
// url parameters when calling buildUrls.
$cloned_result = clone $result;
$cloned_result->setActiveState(FALSE);
$active_results[$facet->getUrlAlias()][] = $cloned_result;
$active_results[$facet->id()][] = $cloned_result;
}
}
if (!empty($active_results[$facet->getUrlAlias()])) {
$url_processor->buildUrls($facet, $active_results[$facet->getUrlAlias()]);
}
$active_facets[$facet->getUrlAlias()] = $facet;
$active_facets[$facet->id()] = $facet;
}
}
......@@ -316,34 +318,23 @@ function facets_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInter
// @see https://www.drupal.org/node/2861586
// TODO handle not grouped facets.
/** @var \Drupal\facets\Result\ResultInterface[] $facet_results */
foreach ($active_results as $facet_alias => $facet_results) {
$facet_used_result[] = $facet_alias;
foreach ($active_results as $facet_id => $facet_results) {
$facet_used_result[$facet_id] = [];
$facet_crumb_items = [];
reset($facet_results);
$facet_url = clone current($facet_results)->getUrl();
// Because we can't get the desired url trough a url processor method
// we iterate each result url and remove the facet params that haven't
// been used on previous crumbs.
// Because we can't get the desired display value trough a url processor
// method we iterate each result url and remove the facet params that
// haven't been used on previous crumbs.
foreach ($facet_results as $res) {
$facet_url = $res->getUrl();
/** @var \Drupal\Core\Url $facet_url */
$query = $facet_url->getOption('query');
$filter_key = $facet_source->getFilterKey() ?: 'f';
$source_filter = $query[$filter_key];
$source_filter = array_unique($source_filter);
$source_filter = array_filter($source_filter);
$matches = preg_grep('/^' . implode('|^', $facet_used_result) . '/', $source_filter);
$query[$filter_key] = $matches;
$facet_used_result[$facet_id][] = $res->getRawValue();
$facet_crumb_items[] = $res->getDisplayValue();
}
sort($facet_crumb_items);
// Set the new query and create the link.
$facet_url->setOption('query', $query);
$facet_url = $facets_url_generator->getUrl($facet_used_result, FALSE);
if (!empty($facet_source->getBreadcrumbSettings()['before'])) {
$crumb_text = $active_facets[$facet_alias]->label() . ': ' . implode(', ', $facet_crumb_items);
$crumb_text = $active_facets[$facet_id]->label() . ': ' . implode(', ', $facet_crumb_items);
}
else {
$crumb_text = implode(', ', $facet_crumb_items);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment