Commit 3c4b657e authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #3282075 by mkalkbrenner: Dispatch an event to adjust cacheable metadata...

Issue #3282075 by mkalkbrenner: Dispatch an event to adjust cacheable metadata after a facet is built
parent aa3ed112
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ use Drupal\facets\FacetInterface;
 *     "only_visible_when_facet_source_is_visible",
 *     "processor_configs",
 *     "empty_behavior",
 *     "show_title"
 *     "show_title",
 *   },
 *   links = {
 *     "collection" = "/admin/config/search/facets",
@@ -1095,6 +1095,30 @@ class Facet extends ConfigEntityBase implements FacetInterface {
    $container->get('plugin.manager.block')->clearCachedDefinitions();
  }

  /**
   * Set cache contexts.
   *
   * @param array $cacheContexts
   *   The cache contexts.
   *
   * @return void
   */
  public function setCacheContexts(array $cacheContexts): void {
     $this->cacheContexts = $cacheContexts;
  }

  /**
   * Set cache max age in seconds.
   *
   * @param int $cacheMaxAge
   *   Max age in seconds.
   *
   * @return void
   */
  public function setCacheMaxAge(int $cacheMaxAge): void {
    $this->cacheMaxAge = $cacheMaxAge;
  }

  /**
   * Remove the facet lazy built data when the facet is serialized.
   */
+10 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ final class FacetsEvents {
  public const ACTIVE_FILTERS_PARSED = ActiveFiltersParsed::class;

  /**
   * This event allows modules to change the facet links's URL if needed.
   * This event allows modules to change the facet links' URL if needed.
   *
   * @Event
   *
@@ -34,4 +34,13 @@ final class FacetsEvents {
   */
  public const URL_CREATED = UrlCreated::class;

  /**
   * This event allows modules to modify a facet after it is built.
   *
   * @Event
   *
   * @see \Drupal\facets\Event\PostBuildFacet
   */
  public const POST_BUILD_FACET = PostBuildFacet::class;

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

namespace Drupal\facets\Event;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\facets\FacetInterface;
use Drupal\Component\EventDispatcher\Event;

/**
 * Implements the PostBuildFacet event.
 *
 * This event allows modules to modify a facet after it is built and before it
 * will be cached and rendered.
 */
final class PostBuildFacet extends Event {

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

  /**
   * PreAddFacetSourceCacheableDependencies constructor.
   *
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   */
  public function __construct(FacetInterface $facet) {
    $this->facet = $facet;
  }

  /**
   * Get the facet.
   *
   * @return \Drupal\facets\FacetInterface
   *   The facet.
   */
  public function getFacet(): FacetInterface {
    return $this->facet;
  }

  /**
   * Set the facet.
   *
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   *
   * @return void
   */
  public function setFacet(FacetInterface $facet): void {
    $this->facet = $facet;
  }

}
+6 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\facets\Event\PostBuildFacet;
use Drupal\facets\Exception\InvalidProcessorException;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
@@ -343,7 +344,11 @@ class DefaultFacetManager {

      $facet->setResults($results);

      $this->builtFacets[$facet->getFacetSourceId()][$facet->id()] = $facet;
      $eventDispatcher = \Drupal::service('event_dispatcher');
      $event = new PostBuildFacet($facet);
      $eventDispatcher->dispatch($event);

      $this->builtFacets[$facet->getFacetSourceId()][$facet->id()] = $event->getFacet();
    }

    return $this->builtFacets[$facet->getFacetSourceId()][$facet->id()];