class FacetsCustomLabelProcessor in Facets Custom Label 8
Provides a processor for FacetsCustomLabelProcessor.
Plugin annotation
@FacetsProcessor(
id = "facets_custom_label",
label = @Translation("Facets custom label processor"),
description = @Translation("Replaces the default label by a custom label for a specified raw or display value. Please make sure the facet processor order is correct."),
stages = {
"build" = 50
}
)
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_custom_label\Plugin\facets\processor\FacetsCustomLabelProcessor implements BuildProcessorInterface
- class \Drupal\facets\Processor\ProcessorPluginBase implements ProcessorInterface uses DependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FacetsCustomLabelProcessor
1 file declares its use of FacetsCustomLabelProcessor
- FacetsCustomLabelProcessorTest.php in tests/
src/ Unit/ Plugin/ processor/ FacetsCustomLabelProcessorTest.php
File
- src/
Plugin/ facets/ processor/ FacetsCustomLabelProcessor.php, line 22
Namespace
Drupal\facets_custom_label\Plugin\facets\processorView source
class FacetsCustomLabelProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
const SEPARATOR = '|';
const ORIGIN__RAW = 'r';
const ORIGIN__DISPLAY = 'd';
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$replacementValues = $this
->getConfiguration()['replacement_values'];
// TODO
// As a possible performance upgrade, we could save the result of the
// mapping processing as a serialized value in cache.
// Split per line.
$replacementValues_rows = preg_split('/\\r\\n|\\r|\\n/', $replacementValues);
$replacementValues_keyedByRawRows = [];
$replacementValues_keyedByValueRows = [];
foreach ($replacementValues_rows as $row) {
// TODO
// This currently does not support display values which contain pipes.
if (substr_count($row, self::SEPARATOR) < 2) {
// Need at least three parts in the row.
continue;
}
// Get the type of origin.
$origin = mb_strstr($row, self::SEPARATOR, TRUE);
if (mb_strlen($origin) == 0) {
// No origin flag set. Do nothing for this row.
continue;
}
$remaining = mb_substr($row, mb_strlen($origin) + mb_strlen(self::SEPARATOR));
$originalValue = mb_strstr($remaining, self::SEPARATOR, TRUE);
$remaining = mb_substr($remaining, mb_strlen($originalValue) + mb_strlen(self::SEPARATOR));
$newLabel = $remaining;
if (mb_strpos($origin, self::ORIGIN__RAW) !== FALSE) {
$replacementValues_keyedByRawRows[$originalValue] = $newLabel;
continue;
}
if (mb_strpos($origin, self::ORIGIN__DISPLAY) !== FALSE) {
$replacementValues_keyedByValueRows[$originalValue] = $newLabel;
continue;
}
}
/* @var \Drupal\facets\Result\Result $result */
foreach ($results as $result) {
if (isset($replacementValues_keyedByRawRows[$result
->getRawValue()])) {
$result
->setDisplayValue($replacementValues_keyedByRawRows[$result
->getRawValue()]);
continue;
}
if (isset($replacementValues_keyedByValueRows[$result
->getDisplayValue()])) {
$result
->setDisplayValue($replacementValues_keyedByValueRows[$result
->getDisplayValue()]);
continue;
}
}
return $results;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$config = $this
->getConfiguration();
$build['replacement_values'] = [
'#title' => $this
->t('Replacement values'),
'#type' => 'textarea',
'#default_value' => isset($config['replacement_values']) ? $config['replacement_values'] : '',
'#description' => $this
->t("Insert a replacement value on each row: <code><origin>|<value>|<new label></code>.<br />For example: <ul><li><code>r|article|Awesome news</code>: the <strong>r</strong> flag converts machine names (e.g.: content types IDs) or content IDs (node / term IDs, etc.).</li><li><code>d|Apple|New label of Apple term</code>: the <strong>d</strong> flag converts titles or names.</li></ul>"),
];
return $build;
}
}
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. | |
FacetsCustomLabelProcessor:: |
public | function |
Runs before the renderable array is created. Overrides BuildProcessorInterface:: |
|
FacetsCustomLabelProcessor:: |
public | function |
Adds a configuration form for this processor. Overrides ProcessorPluginBase:: |
|
FacetsCustomLabelProcessor:: |
constant | |||
FacetsCustomLabelProcessor:: |
constant | |||
FacetsCustomLabelProcessor:: |
constant | |||
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
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 |
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. |