You are here

public function PrettyPathsActiveFilters::getActiveFilters in Facets Pretty Paths 8

Returns the active filters for a given Facet source ID.

Parameters

string $facet_source_id: The Facet Source ID.

Return value

array The array of active filters.

File

src/PrettyPathsActiveFilters.php, line 72

Class

PrettyPathsActiveFilters
Used for determining the Pretty Paths active filters on a given request.

Namespace

Drupal\facets_pretty_paths

Code

public function getActiveFilters($facet_source_id) {

  // Do heavy lifting only once per facet source id.
  $mapping =& drupal_static('facets_pretty_paths_init', []);
  if ($mapping && isset($mapping[$facet_source_id])) {
    return $mapping[$facet_source_id];
  }
  $mapping[$facet_source_id] = [];

  // Keep a local cache of already initialised coders.
  $initialized_coders = [];
  $filters = $this
    ->getFiltersFromRoute();
  if (!$filters) {
    return $mapping[$facet_source_id];
  }
  $parts = explode('/', $filters);
  if (count($parts) % 2 !== 0) {

    // Our key/value combination should always be even. If uneven, we just
    // assume that the first string is not part of the filters, and remove
    // it. This can occur when an url lives in the same path as our facet
    // source, e.g. /search/overview where /search is the facet source path.
    array_shift($parts);
  }
  foreach ($parts as $index => $part) {
    if ($index % 2 == 0) {
      $url_alias = $part;
    }
    else {

      // The $url_alias comes from the previous (odd) iteration.
      $facet_id = $this
        ->getFacetIdByUrlAlias($url_alias, $facet_source_id);
      if (!$facet_id) {

        // No valid facet url alias specified in url.
        continue;
      }

      // Only initialize facet and their coder once per facet id.
      if (!isset($initialized_coders[$facet_id])) {

        /** @var \Drupal\facets\FacetInterface $facet */
        $facet = $this->entityTypeManager
          ->getStorage('facets_facet')
          ->load($facet_id);
        $coder_id = $facet
          ->getThirdPartySetting('facets_pretty_paths', 'coder', 'default_coder');
        $coder = $this->coderManager
          ->createInstance($coder_id, [
          'facet' => $facet,
        ]);
        $initialized_coders[$facet_id] = $coder;
      }
      if (!isset($mapping[$facet_source_id][$facet_id])) {
        $mapping[$facet_source_id][$facet_id] = [
          $initialized_coders[$facet_id]
            ->decode($part),
        ];
      }
      else {
        $mapping[$facet_source_id][$facet_id][] = $initialized_coders[$facet_id]
          ->decode($part);
      }
    }
  }
  return $mapping[$facet_source_id];
}