Commit 41f59dd5 authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #3280838 by mkalkbrenner: Allow modification of facet link URL

parent bceb4835
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -25,4 +25,13 @@ final class FacetsEvents {
   */
  public const ACTIVE_FILTERS_PARSED = ActiveFiltersParsed::class;

  /**
   * This event allows modules to change the facet links's URL if needed.
   *
   * @Event
   *
   * @see \Drupal\facets\Event\UrlCreated
   */
  public const URL_CREATED = UrlCreated::class;

}
+100 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\facets\Event;

use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\Result\ResultInterface;
use Drupal\Component\EventDispatcher\Event;

/**
 * Implements the url created event.
 *
 * This event allows modules to change the facet link's URL if needed.
 */
final class UrlCreated extends Event {

  /**
   * The get parameters.
   *
   * @var \Drupal\Core\Url
   */
  private $url;

  /**
   * The facet result.
   *
   * @var \Drupal\facets\Result\ResultInterface
   */
  private $facetResult;

  /**
   * The facet.
   *
   * @var \Drupal\facets\FacetInterface
   */
  private $facet;

  /**
   * UrlCreated constructor.
   *
   * @param \Drupal\Core\Url $url
   *   The facet link URL.
   * @param \Drupal\facets\Result\ResultInterface $facetResult
   *   The facet result.
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   */
  public function __construct(Url $url, ResultInterface $facetResult, FacetInterface $facet) {
    $this->url = $url;
    $this->facetResult = $facetResult;
    $this->facet = $facet;
  }

  /**
   * Get the URL.
   *
   * @return \Drupal\Core\Url
   *   The URL.
   */
  public function getUrl(): Url {
    return $this->url;
  }

  /**
   * Set the URL.
   *
   * @param \Drupal\Core\Url $url
   *   The URL to set.
   */
  public function setUrl(Url $url): void {
    $this->url = $url;
  }

  /**
   * Get the facet result.
   *
   * Only to be used as context, because changing this will not result in any
   * changes to the final url.
   *
   * @return \Drupal\facets\Result\ResultInterface
   *   The facet result.
   */
  public function getFacetResult() {
    return $this->facetResult;
  }

  /**
   * Get the facet.
   *
   * Only to be used as context, because changing this will not result in any
   * changes to the final url.
   *
   * @return \Drupal\facets\FacetInterface
   *   The facet.
   */
  public function getFacet() {
    return $this->facet;
  }

}
+7 −2
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Url;
use Drupal\facets\Event\ActiveFiltersParsed;
use Drupal\facets\Event\QueryStringCreated;
use Drupal\facets\Event\UrlCreated;
use Drupal\facets\FacetInterface;
use Drupal\facets\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -197,7 +198,7 @@ class QueryString extends UrlProcessorPluginBase {
        }
      }

      // Allow other modules to alter the result url built.
      // Allow other modules to alter the result url query string built.
      $event = new QueryStringCreated($result_get_params, $filter_params, $result, $this->activeFilters, $facet);
      $this->eventDispatcher->dispatch($event);
      $filter_params = $event->getFilterParameters();
@@ -223,7 +224,11 @@ class QueryString extends UrlProcessorPluginBase {
        $url->setOption('query', $new_url_params);
      }

      $result->setUrl($url);
      // Allow other modules to alter the result url built.
      $event = new UrlCreated($url, $result, $facet);
      $this->eventDispatcher->dispatch($event);

      $result->setUrl($event->getUrl());
    }

    // Restore page parameter again. See https://www.drupal.org/node/2726455.