You are here

public function ExcludeSpecifiedItemsProcessor::build in Facets 8

Runs before the renderable array is created.

Parameters

\Drupal\facets\FacetInterface $facet: The facet being changed.

\Drupal\facets\Result\ResultInterface[] $results: The results being changed.

Return value

\Drupal\facets\Result\ResultInterface[] The changed results.

Overrides BuildProcessorInterface::build

File

src/Plugin/facets/processor/ExcludeSpecifiedItemsProcessor.php, line 27

Class

ExcludeSpecifiedItemsProcessor
Provides a processor that excludes specified items.

Namespace

Drupal\facets\Plugin\facets\processor

Code

public function build(FacetInterface $facet, array $results) {
  $config = $this
    ->getConfiguration();

  /** @var \Drupal\facets\Result\ResultInterface $result */
  $exclude_item = $config['exclude'];
  foreach ($results as $id => $result) {
    $is_excluded = FALSE;
    if ($config['regex']) {
      $matcher = '/' . trim(str_replace('/', '\\/', $exclude_item)) . '/';
      if (preg_match($matcher, $result
        ->getRawValue()) || preg_match($matcher, $result
        ->getDisplayValue())) {
        $is_excluded = TRUE;
      }
    }
    else {
      $exclude_items = explode(',', $exclude_item);
      foreach ($exclude_items as $item) {
        $item = trim($item);
        if ($result
          ->getRawValue() == $item || $result
          ->getDisplayValue() == $item) {
          $is_excluded = TRUE;
        }
      }
    }

    // Invert the is_excluded result when the invert setting is active.
    if ($config['invert']) {
      $is_excluded = !$is_excluded;
    }

    // Filter by the excluded results.
    if ($is_excluded) {
      unset($results[$id]);
    }
  }
  return $results;
}