Commit 63e3b85a authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #3292495 by mkalkbrenner: Avoid triggering searches when caching meta data is requested

parent 0dac3026
Loading
Loading
Loading
Loading
+58 −6
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@ namespace Drupal\facets\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\facets\Event\GetFacetCacheContexts;
use Drupal\facets\Event\GetFacetCacheMaxAge;
use Drupal\facets\Event\GetFacetCacheTags;
use Drupal\facets\Exception\Exception;
use Drupal\facets\Exception\InvalidProcessorException;
use Drupal\facets\Exception\InvalidQueryTypeException;
@@ -317,6 +320,8 @@ class Facet extends ConfigEntityBase implements FacetInterface {
   */
  protected $min_count = 1;

  protected $cache_dependencies_calculated = FALSE;

  /**
   * Returns the widget plugin manager.
   *
@@ -881,7 +886,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {

    // Filter processors by status if required. Enabled processors are those
    // which have settings in the processor_configs.
    if ($only_enabled) {
    if ($processors && $only_enabled) {
      $processors_settings = $this->getProcessorConfigs();
      $processors = array_intersect_key($processors, $processors_settings);
    }
@@ -973,8 +978,10 @@ class Facet extends ConfigEntityBase implements FacetInterface {
      'weights' => $processor['weights'],
      'settings' => $processor['settings'],
    ];
    // Sort the processors so we won't have unnecessary changes.
    // Sort the processors, so we won't have unnecessary changes.
    ksort($this->processor_configs);

    $this->cache_dependencies_calculated = FALSE;
  }

  /**
@@ -983,6 +990,8 @@ class Facet extends ConfigEntityBase implements FacetInterface {
  public function removeProcessor($processor_id) {
    unset($this->processor_configs[$processor_id]);
    unset($this->processors[$processor_id]);

    $this->cache_dependencies_calculated = FALSE;
  }

  /**
@@ -1098,15 +1107,57 @@ class Facet extends ConfigEntityBase implements FacetInterface {
  /**
   * {@inheritdoc}
   */
  public function setCacheContexts(array $cacheContexts): void {
     $this->cacheContexts = $cacheContexts;
  public function getCacheTags() {
    $this->calculateCacheDependencies();

    $eventDispatcher = \Drupal::service('event_dispatcher');
    $event = new GetFacetCacheTags(parent::getCacheTags(), $this);
    $eventDispatcher->dispatch($event);
    $this->cacheTags = $event->getCacheTags() ?? $this->cacheTags;

    return array_values($this->cacheTags);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    $this->calculateCacheDependencies();

    $eventDispatcher = \Drupal::service('event_dispatcher');
    $event = new GetFacetCacheContexts(parent::getCacheContexts(), $this);
    $eventDispatcher->dispatch($event);
    $this->cacheContexts = $event->getCacheContexts() ?? $this->cacheContexts;

    return array_values($this->cacheContexts);
  }

  /**
   * {@inheritdoc}
   */
  public function setCacheMaxAge(int $cacheMaxAge): void {
    $this->cacheMaxAge = $cacheMaxAge;
  public function getCacheMaxAge() {
    $this->calculateCacheDependencies();

    $eventDispatcher = \Drupal::service('event_dispatcher');
    $event = new GetFacetCacheMaxAge(parent::getCacheMaxAge(), $this);
    $eventDispatcher->dispatch($event);
    $this->cacheMaxAge = $event->getCacheMaxAge() ?? $this->cacheMaxAge;

    return $this->cacheMaxAge;
  }

  protected function calculateCacheDependencies(): void {
    if (!$this->cache_dependencies_calculated) {
      if ($facet_source = $this->getFacetSource()) {
        $this->addCacheableDependency($facet_source);
      }

      foreach ($this->getProcessors() ?? [] as $processor) {
        $this->addCacheableDependency($processor);
      }

      $this->cache_dependencies_calculated = TRUE;
    }
  }

  /**
@@ -1115,6 +1166,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
  public function __sleep() {
    unset($this->facet_source_instance);
    unset($this->processors);

    return parent::__sleep();
  }

+27 −0
Original line number Diff line number Diff line
@@ -43,4 +43,31 @@ final class FacetsEvents {
   */
  public const POST_BUILD_FACET = PostBuildFacet::class;

  /**
   * This event allows modules to change the cache contexts of a facet.
   *
   * @Event
   *
   * @see \Drupal\facets\Event\GetFacetCacheContexts
   */
  public const GET_FACET_CACHE_CONTEXTS = GetFacetCacheContexts::class;

  /**
   * This event allows modules to change the cache max age of a facet.
   *
   * @Event
   *
   * @see \Drupal\facets\Event\GetFacetCacheMaxAge
   */
  public const GET_FACET_CACHE_MAX_AGE = GetFacetCacheMaxAge::class;

  /**
   * This event allows modules to change the cache tags of a facet.
   *
   * @Event
   *
   * @see \Drupal\facets\Event\GetFacetCacheTags
   */
  public const GET_FACET_CACHE_TAGS = GetFacetCacheTags::class;

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

namespace Drupal\facets\Event;

use Drupal\facets\FacetInterface;
use Drupal\Component\EventDispatcher\Event;

/**
 * Implements the get cache contexts event.
 *
 * This event allows modules to change the cache contexts of a facet if needed.
 */
final class GetFacetCacheContexts extends Event {

  /**
   * The cache contexts.
   *
   * @var string[]
   */
  private $cacheContexts;

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

  /**
   * GetCacheContexts constructor.
   *
   * @param string[] $cacheContexts
   *   The cache contexts.
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   */
  public function __construct($cacheContexts, FacetInterface $facet) {
    $this->cacheContexts = $cacheContexts;
    $this->facet = $facet;
  }

  /**
   * Get the cache contexts.
   *
   * @return string[]
   *   The cache contexts.
   */
  public function getCacheContexts(): array {
    return $this->cacheContexts ?? [];
  }

  /**
   * Get the cache contexts.
   *
   * @param string[] $cacheContexts
   *   The cache contexts.
   */
  public function setCacheContexts($cacheContexts): void {
    $this->cacheContexts = $cacheContexts;
  }

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

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

namespace Drupal\facets\Event;

use Drupal\facets\FacetInterface;
use Drupal\Component\EventDispatcher\Event;

/**
 * Implements the get cache max age event.
 *
 * This event allows modules to change the cache max age of a facet if needed.
 */
final class GetFacetCacheMaxAge extends Event {

  /**
   * The cache max age.
   *
   * @var int
   */
  private $cacheMaxAge;

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

  /**
   * GetCacheMaxAge constructor.
   *
   * @param int $cacheMaxAge
   *   The cache max age.
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   */
  public function __construct($cacheMaxAge, FacetInterface $facet) {
    $this->cacheMaxAge = $cacheMaxAge;
    $this->facet = $facet;
  }

  /**
   * Get the cache max age.
   *
   * @return int
   *   The cache max age.
   */
  public function getCacheMaxAge(): int {
    return $this->cacheMaxAge ?? 0;
  }

  /**
   * Get the cache max age.
   *
   * @param int $cacheMaxAge
   *   The cache max age.
   */
  public function setCacheMaxAge($cacheMaxAge): void {
    $this->cacheMaxAge = $cacheMaxAge;
  }

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

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

namespace Drupal\facets\Event;

use Drupal\facets\FacetInterface;
use Drupal\Component\EventDispatcher\Event;

/**
 * Implements the get cache tags event.
 *
 * This event allows modules to change the cache tags of a facet if needed.
 */
final class GetFacetCacheTags extends Event {

  /**
   * The cache tags.
   *
   * @var string[]
   */
  private $cacheTags;

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

  /**
   * GetCacheTags constructor.
   *
   * @param string[] $cacheTags
   *   The cache tags.
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   */
  public function __construct($cacheTags, FacetInterface $facet) {
    $this->cacheTags = $cacheTags;
    $this->facet = $facet;
  }

  /**
   * Get the cache tags.
   *
   * @return string[]
   *   The cache tags.
   */
  public function getCacheTags(): array {
    return $this->cacheTags ?? [];
  }

  /**
   * Get the cache tags.
   *
   * @param string[] $cacheTags
   *   The cache tags.
   */
  public function setCacheTags($cacheTags): void {
    $this->cacheTags = $cacheTags;
  }

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

}
Loading