You are here

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

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\processor
View 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>&lt;origin&gt;|&lt;value&gt;|&lt;new label&gt;</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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
FacetsCustomLabelProcessor::build public function Runs before the renderable array is created. Overrides BuildProcessorInterface::build
FacetsCustomLabelProcessor::buildConfigurationForm public function Adds a configuration form for this processor. Overrides ProcessorPluginBase::buildConfigurationForm
FacetsCustomLabelProcessor::ORIGIN__DISPLAY constant
FacetsCustomLabelProcessor::ORIGIN__RAW constant
FacetsCustomLabelProcessor::SEPARATOR constant
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
ProcessorInterface::STAGE_BUILD constant Processing stage: build.
ProcessorInterface::STAGE_POST_QUERY constant Processing stage: post_query.
ProcessorInterface::STAGE_PRE_QUERY constant Processing stage: pre_query.
ProcessorInterface::STAGE_SORT constant Processing stage: sort.
ProcessorPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ProcessorPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 8
ProcessorPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ProcessorPluginBase::getDefaultWeight public function Returns the default weight for a specific processing stage. Overrides ProcessorInterface::getDefaultWeight
ProcessorPluginBase::getDescription public function Retrieves the processor description. Overrides ProcessorInterface::getDescription
ProcessorPluginBase::getQueryType public function Picks the preferred query type for this widget. Overrides ProcessorInterface::getQueryType 4
ProcessorPluginBase::isHidden public function Determines whether this processor should be hidden from the user. Overrides ProcessorInterface::isHidden
ProcessorPluginBase::isLocked public function Determines whether this processor should always be enabled. Overrides ProcessorInterface::isLocked
ProcessorPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ProcessorPluginBase::submitConfigurationForm public function
ProcessorPluginBase::supportsFacet public function Checks if the facet is supported by this widget. Overrides ProcessorInterface::supportsFacet 6
ProcessorPluginBase::supportsStage public function Checks whether this processor implements a particular stage. Overrides ProcessorInterface::supportsStage
ProcessorPluginBase::validateConfigurationForm public function Validates a configuration form for this processor. Overrides ProcessorInterface::validateConfigurationForm 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.