DummyQuery.php 3.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

namespace Drupal\facets_query_processor\Plugin\facets\url_processor;

use Drupal\facets\FacetInterface;
use Drupal\facets\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\HttpFoundation\Request;

/**
 * Query string URL processor.
 *
 * @FacetsUrlProcessor(
 *   id = "dummy_query",
 *   label = @Translation("Dummy query"),
 *   description = @Translation("Dummy for testing.")
 * )
 */
class DummyQuery extends UrlProcessorPluginBase {

  /**
   * A string that separates the filters in the query string.
   */
  const SEPARATOR = '||';

  /**
   * A string of how to represent the facet in the url.
   *
   * @var string
   */
30
  protected $urlAlias;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

  /**
   * An array of active filters.
   *
   * @var string[]
   *   An array containing the active filters
   */
  protected $activeFilters = [];

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $request);
    $this->initializeActiveFilters();
  }

  /**
   * {@inheritdoc}
   */
  public function buildUrls(FacetInterface $facet, array $results) {
    // Create links for all the values.
    // First get the current list of get parameters.
    $get_params = $this->request->query;

    // Set the url alias from the the facet object.
57
    $this->urlAlias = $facet->getUrlAlias();
58
59
60
61
62
63
64
65

    // No results are found for this facet, so don't try to create urls.
    if (empty($results)) {
      return [];
    }

    /** @var \Drupal\facets\Result\ResultInterface $result */
    foreach ($results as &$result) {
66
      $filter_string = $this->urlAlias . self::SEPARATOR . $result->getRawValue();
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
      $result_get_params = clone $get_params;

      $filter_params = $result_get_params->get($this->filterKey, [], TRUE);
      // If the value is active, remove the filter string from the parameters.
      if ($result->isActive()) {
        foreach ($filter_params as $key => $filter_param) {
          if ($filter_param == $filter_string) {
            unset($filter_params[$key]);
          }
        }
      }
      // If the value is not active, add the filter string.
      else {
        $filter_params[] = $filter_string;
      }

      $result_get_params->set($this->filterKey, $filter_params);
84
85

      $url = $facet->getFacetSource()->getPath();
86
87
88
89
90
91
92
93
94
95
96
97
98
      $url->setOption('query', $result_get_params->all());

      $result->setUrl($url);
    }

    return $results;
  }

  /**
   * {@inheritdoc}
   */
  public function setActiveItems(FacetInterface $facet) {
    // Set the url alias from the the facet object.
99
    $this->urlAlias = $facet->getUrlAlias();
100
101

    // Get the filter key of the facet.
102
103
    if (isset($this->activeFilters[$this->urlAlias])) {
      foreach ($this->activeFilters[$this->urlAlias] as $value) {
104
105
106
107
108
109
        $facet->setActiveItem(trim($value, '"'));
      }
    }
  }

  /**
110
   * Initializes the active filters.
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
   *
   * Get all the filters that are active. This method only get's all the
   * filters but doesn't assign them to facets. In the processFacet method the
   * active values for a specific facet are added to the facet.
   */
  protected function initializeActiveFilters() {
    $url_parameters = $this->request->query;

    // Get the active facet parameters.
    $active_params = $url_parameters->get($this->filterKey, array(), TRUE);

    // Explode the active params on the separator.
    foreach ($active_params as $param) {
      list($key, $value) = explode(self::SEPARATOR, $param);
      if (!isset($this->activeFilters[$key])) {
        $this->activeFilters[$key] = [$value];
      }
      else {
        $this->activeFilters[$key][] = $value;
      }
    }
  }

}