Newer
Older
Joris Vercammen
committed
* Contains Drupal\facets\Plugin\facets\url_processor\QueryString.
Joris Vercammen
committed
namespace Drupal\facets\Plugin\facets\url_processor;
use Drupal\facets\FacetInterface;
Joris Vercammen
committed
use Drupal\facets\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\HttpFoundation\Request;
Joris Vercammen
committed
* Query string URL processor.
Joris Vercammen
committed
* @FacetsUrlProcessor(
Joris Vercammen
committed
* label = @Translation("Query string"),
* description = @Translation("Query string is the default Facets URL processor, and uses GET parameters, e.g. ?f[0]=brand:drupal&f[1]=color:blue")
Joris Vercammen
committed
class QueryString extends UrlProcessorPluginBase {
Joris Vercammen
committed
/**
* A string that separates the filters in the query string.
Joris Vercammen
committed
*/
Joris Vercammen
committed
/**
* A string of how to represent the facet in the url.
*
* @var string
*/
protected $url_alias;
Joris Vercammen
committed
/**
* An array of active filters.
*
* @var string[]
Joris Vercammen
committed
* An array containing the active filters
*/
Joris Vercammen
committed
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $request);
$this->initializeActiveFilters();
}
Joris Vercammen
committed
/**
* {@inheritdoc}
*/
Joris Vercammen
committed
public function buildUrls(FacetInterface $facet, array $results) {
// Create links for all the values.
Jur de Vries
committed
// First get the current list of get parameters.
$get_params = $this->request->query;
Joris Vercammen
committed
// Set the url alias from the the facet object.
$this->url_alias = $facet->getUrlAlias();
Joris Vercammen
committed
// No results are found for this facet, so don't try to create urls.
Joris Vercammen
committed
return [];
/** @var \Drupal\facets\Result\ResultInterface $result */
Joris Vercammen
committed
foreach ($results as &$result) {
Joris Vercammen
committed
$filter_string = $this->url_alias . self::SEPARATOR . $result->getRawValue();
$result_get_params = clone $get_params;
$filter_params = $result_get_params->get($this->filterKey, [], TRUE);
Joris Vercammen
committed
// 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);
Jur de Vries
committed
$request = $this->request;
Jimmy Henderickx
committed
if ($facet->getFacetSource()->getPath()) {
$request = Request::create('/' . $facet->getFacetSource()->getPath());
Jur de Vries
committed
}
$url = Url::createFromRequest($request);
$url->setOption('query', $result_get_params->all());
Joris Vercammen
committed
Joris Vercammen
committed
return $results;
Joris Vercammen
committed
/**
* {@inheritdoc}
*/
Joris Vercammen
committed
public function setActiveItems(FacetInterface $facet) {
Joris Vercammen
committed
// Set the url alias from the the facet object.
$this->url_alias = $facet->getUrlAlias();
// Get the filter key of the facet.
Joris Vercammen
committed
if (isset($this->activeFilters[$this->url_alias])) {
foreach ($this->activeFilters[$this->url_alias] as $value) {
$facet->setActiveItem(trim($value, '"'));
}
}
}
/**
* Initialize the active filters.
Joris Vercammen
committed
* 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];