class FacetsPrettyPathsUrlProcessor in Facets Pretty Paths 8
Pretty paths URL processor.
Plugin annotation
@FacetsUrlProcessor(
id = "facets_pretty_paths",
label = @Translation("Pretty paths"),
description = @Translation("Pretty paths uses slashes as separator, e.g. /brand/drupal/color/blue"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\facets\Processor\ProcessorPluginBase implements ProcessorInterface uses DependencyTrait
- class \Drupal\facets\UrlProcessor\UrlProcessorPluginBase implements ContainerFactoryPluginInterface, UrlProcessorInterface
- class \Drupal\facets_pretty_paths\Plugin\facets\url_processor\FacetsPrettyPathsUrlProcessor implements ContainerFactoryPluginInterface
- class \Drupal\facets\UrlProcessor\UrlProcessorPluginBase implements ContainerFactoryPluginInterface, UrlProcessorInterface
- class \Drupal\facets\Processor\ProcessorPluginBase implements ProcessorInterface uses DependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FacetsPrettyPathsUrlProcessor
File
- src/
Plugin/ facets/ url_processor/ FacetsPrettyPathsUrlProcessor.php, line 25
Namespace
Drupal\facets_pretty_paths\Plugin\facets\url_processorView source
class FacetsPrettyPathsUrlProcessor extends UrlProcessorPluginBase implements ContainerFactoryPluginInterface {
/**
* The current_route_match service.
*
* @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface
*/
protected $routeMatch;
/**
* The service responsible for determining the active filters.
*
* @var \Drupal\facets_pretty_paths\PrettyPathsActiveFilters
*/
protected $activeFiltersService;
/**
* Constructs FacetsPrettyPathsUrlProcessor object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object for the current request.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The route match service.
* @param \Drupal\facets_pretty_paths\PrettyPathsActiveFilters $activeFilters
* The active filters service.
*
* @throws \Drupal\facets\Exception\InvalidProcessorException
*/
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();
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function buildUrls(FacetInterface $facet, array $results) {
// No results are found for this facet, so don't try to create urls.
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,
]);
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as &$result) {
$raw_value = $result
->getRawValue();
$filters_current_result = $filters;
// If the value is active, remove the filter string from the parameters.
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()) {
// Enable parent id again if exists.
$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 {
// Exclude others results if we are in the show_only_one_result mode.
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()) {
// If hierarchy is active, unset parent trail and every child when
// building the enable-link to ensure those are not enabled anymore.
$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]);
}
}
}
}
// Now we start transforming $filters_current_result array into a string
// which we append later to the current path.
$pretty_paths_presort_data = [];
foreach ($filters_current_result as $facet_id => $active_values) {
foreach ($active_values as $active_value) {
// Ensure we only load every facet and coder once.
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',
]);
// First get the current list of get parameters.
$get_params = $this->request->query;
// When adding/removing a filter the number of pages may have changed,
// possibly resulting in an invalid page parameter.
if ($get_params
->has('page')) {
$current_page = $get_params
->get('page');
$get_params
->remove('page');
}
$url
->setOption('query', $get_params
->all());
$result
->setUrl($url);
// Restore page parameter again. See https://www.drupal.org/node/2726455.
if (isset($current_page)) {
$get_params
->set('page', $current_page);
}
}
return $results;
}
/**
* Sorts an array with weight and name values.
*
* It sorts first by weight, then by the alias of the facet item value.
*
* @param array $pretty_paths
* The values to sort.
*
* @return array
* The sorted values.
*/
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;
}
/**
* Initializes the active filters from the url.
*
* Get all the filters that are active by checking the request url and store
* them in activeFilters which is an array where key is the facet id and value
* is an array of raw values.
*/
protected function initializeActiveFilters() {
$facet_source_id = $this->configuration['facet']
->getFacetSourceId();
$this->activeFilters = $this->activeFiltersService
->getActiveFilters($facet_source_id);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
FacetsPrettyPathsUrlProcessor:: |
protected | property | The service responsible for determining the active filters. | |
FacetsPrettyPathsUrlProcessor:: |
protected | property | The current_route_match service. | |
FacetsPrettyPathsUrlProcessor:: |
public | function |
Adds urls to the results. Overrides UrlProcessorInterface:: |
|
FacetsPrettyPathsUrlProcessor:: |
public static | function |
Creates an instance of the plugin. Overrides UrlProcessorPluginBase:: |
|
FacetsPrettyPathsUrlProcessor:: |
protected | function | Initializes the active filters from the url. | |
FacetsPrettyPathsUrlProcessor:: |
public | function | Sorts an array with weight and name values. | |
FacetsPrettyPathsUrlProcessor:: |
public | function |
Constructs FacetsPrettyPathsUrlProcessor object. Overrides UrlProcessorPluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessorInterface:: |
constant | Processing stage: build. | ||
ProcessorInterface:: |
constant | Processing stage: post_query. | ||
ProcessorInterface:: |
constant | Processing stage: pre_query. | ||
ProcessorInterface:: |
constant | Processing stage: sort. | ||
ProcessorPluginBase:: |
public | function |
Adds a configuration form for this processor. Overrides ProcessorInterface:: |
10 |
ProcessorPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
ProcessorPluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
8 |
ProcessorPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ProcessorPluginBase:: |
public | function |
Returns the default weight for a specific processing stage. Overrides ProcessorInterface:: |
|
ProcessorPluginBase:: |
public | function |
Retrieves the processor description. Overrides ProcessorInterface:: |
|
ProcessorPluginBase:: |
public | function |
Picks the preferred query type for this widget. Overrides ProcessorInterface:: |
4 |
ProcessorPluginBase:: |
public | function |
Determines whether this processor should be hidden from the user. Overrides ProcessorInterface:: |
|
ProcessorPluginBase:: |
public | function |
Determines whether this processor should always be enabled. Overrides ProcessorInterface:: |
|
ProcessorPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ProcessorPluginBase:: |
public | function | ||
ProcessorPluginBase:: |
public | function |
Checks if the facet is supported by this widget. Overrides ProcessorInterface:: |
6 |
ProcessorPluginBase:: |
public | function |
Checks whether this processor implements a particular stage. Overrides ProcessorInterface:: |
|
ProcessorPluginBase:: |
public | function |
Validates a configuration form for this processor. Overrides ProcessorInterface:: |
2 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlProcessorPluginBase:: |
protected | property | An array of active filters. | |
UrlProcessorPluginBase:: |
protected | property | The entity type manager. | |
UrlProcessorPluginBase:: |
protected | property | The query string variable. | |
UrlProcessorPluginBase:: |
protected | property | The clone of the current request object. | |
UrlProcessorPluginBase:: |
protected | property | The url separator variable. | 1 |
UrlProcessorPluginBase:: |
public | function |
Returns the active filters. Overrides UrlProcessorInterface:: |
|
UrlProcessorPluginBase:: |
public | function |
Returns the filter key. Overrides UrlProcessorInterface:: |
|
UrlProcessorPluginBase:: |
public | function |
Returns the url separator. Overrides UrlProcessorInterface:: |
|
UrlProcessorPluginBase:: |
public | function |
Set active filters. Overrides UrlProcessorInterface:: |
|
UrlProcessorPluginBase:: |
public | function |
Sets active items. Overrides UrlProcessorInterface:: |