View source
<?php
namespace Drupal\facets_pretty_paths\Plugin\facets\url_processor;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\FacetInterface;
use Drupal\facets\UrlProcessor\UrlProcessorPluginBase;
use Drupal\facets_pretty_paths\PrettyPathsActiveFilters;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class FacetsPrettyPathsUrlProcessor extends UrlProcessorPluginBase implements ContainerFactoryPluginInterface {
protected $routeMatch;
protected $activeFiltersService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $routeMatch, PrettyPathsActiveFilters $activeFilters) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $request, $entity_type_manager);
$this->routeMatch = $routeMatch;
$this->activeFiltersService = $activeFilters;
$this
->initializeActiveFilters();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('request_stack')
->getMasterRequest(), $container
->get('entity_type.manager'), $container
->get('current_route_match'), $container
->get('facets_pretty_paths.active_filters'));
}
public function buildUrls(FacetInterface $facet, array $results) {
if (empty($results)) {
return [];
}
$initialized_coders = [];
$initialized_facets = [];
$filters = $this
->getActiveFilters();
$coder_plugin_manager = \Drupal::service('plugin.manager.facets_pretty_paths.coder');
$coder_id = $facet
->getThirdPartySetting('facets_pretty_paths', 'coder', 'default_coder');
$coder = $coder_plugin_manager
->createInstance($coder_id, [
'facet' => $facet,
]);
foreach ($results as &$result) {
$raw_value = $result
->getRawValue();
$filters_current_result = $filters;
if ($result
->isActive()) {
if (($key = array_search($raw_value, $filters_current_result[$result
->getFacet()
->id()])) !== FALSE) {
unset($filters_current_result[$result
->getFacet()
->id()][$key]);
}
if ($result
->getFacet()
->getEnableParentWhenChildGetsDisabled() && $result
->getFacet()
->getUseHierarchy()) {
$parent_ids = $result
->getFacet()
->getHierarchyInstance()
->getParentIds($raw_value);
if (isset($parent_ids[0]) && $parent_ids[0]) {
$filters_current_result[$result
->getFacet()
->id()][] = $coder
->encode($parent_ids[0]);
}
}
}
else {
if ($result
->getFacet()
->getShowOnlyOneResult()) {
$filters_current_result[$result
->getFacet()
->id()] = [
0 => $raw_value,
];
}
else {
$filters_current_result[$result
->getFacet()
->id()][] = $raw_value;
}
if ($result
->getFacet()
->getUseHierarchy()) {
$parent_ids = $result
->getFacet()
->getHierarchyInstance()
->getParentIds($raw_value);
$child_ids = $result
->getFacet()
->getHierarchyInstance()
->getNestedChildIds($raw_value);
$parents_and_child_ids = array_merge($parent_ids, $child_ids);
foreach ($parents_and_child_ids as $id) {
if (($key = array_search($id, $filters_current_result[$result
->getFacet()
->id()])) !== FALSE) {
unset($filters_current_result[$result
->getFacet()
->id()][$key]);
}
}
}
}
$pretty_paths_presort_data = [];
foreach ($filters_current_result as $facet_id => $active_values) {
foreach ($active_values as $active_value) {
if (!isset($initialized_facets[$facet_id])) {
$facet = Facet::load($facet_id);
$initialized_facets[$facet_id] = $facet;
$coder_id = $facet
->getThirdPartySetting('facets_pretty_paths', 'coder', 'default_coder');
$coder = $coder_plugin_manager
->createInstance($coder_id, [
'facet' => $facet,
]);
$initialized_coders[$facet_id] = $coder;
}
$encoded_value = $initialized_coders[$facet_id]
->encode($active_value);
$pretty_paths_presort_data[] = [
'weight' => $initialized_facets[$facet_id]
->getWeight(),
'name' => $initialized_facets[$facet_id]
->getName(),
'pretty_path_alias' => "/" . $initialized_facets[$facet_id]
->getUrlAlias() . "/" . $encoded_value,
];
}
}
$pretty_paths_presort_data = $this
->sortByWeightAndName($pretty_paths_presort_data);
$pretty_paths_string = implode('', array_column($pretty_paths_presort_data, 'pretty_path_alias'));
$url = Url::fromUri('internal:' . $facet
->getFacetSource()
->getPath() . $pretty_paths_string);
$url
->setOption('attributes', [
'rel' => 'nofollow',
]);
$get_params = $this->request->query;
if ($get_params
->has('page')) {
$current_page = $get_params
->get('page');
$get_params
->remove('page');
}
$url
->setOption('query', $get_params
->all());
$result
->setUrl($url);
if (isset($current_page)) {
$get_params
->set('page', $current_page);
}
}
return $results;
}
public function sortByWeightAndName(array $pretty_paths) {
array_multisort(array_column($pretty_paths, 'weight'), SORT_ASC, array_column($pretty_paths, 'name'), SORT_ASC, array_column($pretty_paths, 'pretty_path_alias'), SORT_ASC, $pretty_paths);
return $pretty_paths;
}
protected function initializeActiveFilters() {
$facet_source_id = $this->configuration['facet']
->getFacetSourceId();
$this->activeFilters = $this->activeFiltersService
->getActiveFilters($facet_source_id);
}
}