Loading facets.module +2 −2 Original line number Diff line number Diff line Loading @@ -217,10 +217,10 @@ function facets_preprocess_facets_item_list(array &$variables) { $variables['title'] = $facet->label(); } if (Settings::get('facets_debug_cacheable_metadata', FALSE) && $facet->getFacetSource() instanceof SearchApiFacetSourceInterface) { $variables['cache_hash'] = $hash = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6); $variables['cache_hash'] = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6); $variables['cache_contexts'] = implode(', ', $facet->getCacheContexts()); $variables['cache_tags'] = implode(', ', $facet->getCacheTags()); $variables['cache_max_age'] = $facet->getCacheMaxAge(); $variables['cache_max_age'] = $facet->getCacheMaxAge() . ' seconds'; } } template_preprocess_item_list($variables); Loading modules/facets_summary/facets_summary.module +19 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,9 @@ * Hook implementations for the facets summary module. */ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Site\Settings; use Drupal\facets_summary\Entity\FacetsSummary; use Drupal\search_api\Entity\Index; /** Loading Loading @@ -99,6 +102,22 @@ function facets_summary_theme_suggestions_facets_summary_item_list(array $variab */ function facets_summary_preprocess_facets_summary_item_list(array &$variables) { template_preprocess_item_list($variables); if (Settings::get('facets_debug_cacheable_metadata', FALSE)) { if ($facetsSummary = FacetsSummary::load($variables['facet_summary_id'])) { $facetsSummaryManager = \Drupal::service('facets_summary.manager'); $cacheableMetadata = new CacheableMetadata(); foreach ($facetsSummaryManager->getFacets($facetsSummary) as $facet) { $cacheableMetadata->addCacheableDependency($facet); } $variables['cache_hash'] = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6); $variables['cache_contexts'] = implode(', ', $cacheableMetadata->getCacheContexts()); $variables['cache_tags'] = implode(', ', $cacheableMetadata->getCacheTags()); $variables['cache_max_age'] = $cacheableMetadata->getCacheMaxAge() . ' seconds'; } } } /** Loading modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php +36 −10 Original line number Diff line number Diff line Loading @@ -45,6 +45,11 @@ class DefaultFacetsSummaryManager { */ protected $facetManager; /** * @var \Drupal\facets\FacetInterface[] */ protected $facets = []; /** * Constructs a new instance of the DefaultFacetManager. * Loading Loading @@ -83,24 +88,16 @@ class DefaultFacetsSummaryManager { */ public function build(FacetsSummaryInterface $facets_summary) { // Let the facet_manager build the facets. $facets = $this->getFacets($facets_summary); $facets_config = $facets_summary->getFacets(); $facetsource_id = $facets_summary->getFacetSourceId(); /** @var \Drupal\facets\Entity\Facet[] $facets */ $facets = $this->facetManager->getFacetsByFacetSourceId($facetsource_id); // Get the current results from the facets and let all processors that // trigger on the build step do their build processing. // @see \Drupal\facets\Processor\BuildProcessorInterface. // @see \Drupal\facets\Processor\SortProcessorInterface. $this->facetManager->updateResults($facetsource_id); $facets_config = $facets_summary->getFacets(); // Exclude facets which were not selected for this summary. $facets = array_filter($facets, function ($item) use ($facets_config) { return (isset($facets_config[$item->id()])); } ); foreach ($facets as $facet) { // Do not build the facet in summary if facet is not rendered. if (!$facet->getActiveItems()) { Loading Loading @@ -179,4 +176,33 @@ class DefaultFacetsSummaryManager { return $items; } /** * Get facet entities. * * @param \Drupal\facets_summary\FacetsSummaryInterface $facets_summary * The facet we should build. * * @return \Drupal\facets\FacetInterface[] * The facet entities. * * @throws \Drupal\facets\Exception\InvalidProcessorException */ public function getFacets(FacetsSummaryInterface $facets_summary): array { $facetsource_id = $facets_summary->getFacetSourceId(); if (!isset($this->facets[$facetsource_id])) { $this->facets[$facetsource_id] = $this->facetManager->getFacetsByFacetSourceId($facetsource_id); $facets_config = $facets_summary->getFacets(); // Exclude facets which were not selected for this summary. $facets = array_filter($this->facets[$facetsource_id], function ($item) use ($facets_config) { return (isset($facets_config[$item->id()])); } ); } return $this->facets[$facetsource_id]; } } modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php +46 −5 Original line number Diff line number Diff line Loading @@ -3,7 +3,8 @@ namespace Drupal\facets_summary\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\UncacheableDependencyTrait; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\facets_summary\Entity\FacetsSummary; use Drupal\facets_summary\FacetsSummaryBlockInterface; Loading @@ -20,8 +21,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterface, ContainerFactoryPluginInterface { use UncacheableDependencyTrait; /** * The facet manager service. * Loading @@ -36,6 +35,13 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac */ protected $facetsSummary; /** * The cacheable metadata. * * @var \Drupal\Core\Cache\CacheableMetadata */ protected $cacheableMetadata; /** * Constructs a source summary block. * Loading Loading @@ -96,6 +102,8 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac // Add contextual links only when we have results. if (!empty($build)) { CacheableMetadata::createFromObject($this)->applyTo($build); $build['#contextual_links']['facets_summary'] = [ 'route_parameters' => ['facets_summary' => $facets_summary->id()], ]; Loading @@ -119,8 +127,41 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac /** * {@inheritdoc} */ public function getPreviewFallbackString() { return $this->t('Placeholder for the "@facet_summary" facet summary', ['@facet_summary' => $this->getDerivativeId()]); public function getCacheTags() { $this->calculateCacheDependencies(); return Cache::mergeTags(parent::getCacheTags(), $this->cacheableMetadata->getCacheTags()); } /** * {@inheritdoc} */ public function getCacheContexts() { $this->calculateCacheDependencies(); return Cache::mergeContexts(parent::getCacheContexts(), $this->cacheableMetadata->getCacheContexts()); } /** * {@inheritdoc} */ public function getCacheMaxAge() { $this->calculateCacheDependencies(); return Cache::mergeMaxAges(parent::getCacheMaxAge(), $this->cacheableMetadata->getCacheMaxAge()); } /** * @throws \Drupal\facets\Exception\InvalidProcessorException */ protected function calculateCacheDependencies(): void { if (!$this->cacheableMetadata) { $this->cacheableMetadata = new CacheableMetadata(); foreach ($this->facetsSummaryManager->getFacets($this->getEntity()) as $facet) { $this->cacheableMetadata->addCacheableDependency($facet); } } } } modules/facets_summary/templates/facets-summary-item-list.html.twig +14 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,20 @@ * @ingroup themeable */ #} {% if cache_hash %} <!-- facets cacheable metadata hash: {{ cache_hash }} {% if cache_contexts %} contexts: {{ cache_contexts }} {%- endif %} {% if cache_tags %} tags: {{ cache_tags }} {%- endif %} {% if cache_max_age %} max age: {{ cache_max_age }} {%- endif %} --> {%- endif %} {% if context.list_style %} {%- set attributes = attributes.addClass('item-list__' ~ context.list_style) %} {% endif %} Loading Loading
facets.module +2 −2 Original line number Diff line number Diff line Loading @@ -217,10 +217,10 @@ function facets_preprocess_facets_item_list(array &$variables) { $variables['title'] = $facet->label(); } if (Settings::get('facets_debug_cacheable_metadata', FALSE) && $facet->getFacetSource() instanceof SearchApiFacetSourceInterface) { $variables['cache_hash'] = $hash = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6); $variables['cache_hash'] = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6); $variables['cache_contexts'] = implode(', ', $facet->getCacheContexts()); $variables['cache_tags'] = implode(', ', $facet->getCacheTags()); $variables['cache_max_age'] = $facet->getCacheMaxAge(); $variables['cache_max_age'] = $facet->getCacheMaxAge() . ' seconds'; } } template_preprocess_item_list($variables); Loading
modules/facets_summary/facets_summary.module +19 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,9 @@ * Hook implementations for the facets summary module. */ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Site\Settings; use Drupal\facets_summary\Entity\FacetsSummary; use Drupal\search_api\Entity\Index; /** Loading Loading @@ -99,6 +102,22 @@ function facets_summary_theme_suggestions_facets_summary_item_list(array $variab */ function facets_summary_preprocess_facets_summary_item_list(array &$variables) { template_preprocess_item_list($variables); if (Settings::get('facets_debug_cacheable_metadata', FALSE)) { if ($facetsSummary = FacetsSummary::load($variables['facet_summary_id'])) { $facetsSummaryManager = \Drupal::service('facets_summary.manager'); $cacheableMetadata = new CacheableMetadata(); foreach ($facetsSummaryManager->getFacets($facetsSummary) as $facet) { $cacheableMetadata->addCacheableDependency($facet); } $variables['cache_hash'] = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6); $variables['cache_contexts'] = implode(', ', $cacheableMetadata->getCacheContexts()); $variables['cache_tags'] = implode(', ', $cacheableMetadata->getCacheTags()); $variables['cache_max_age'] = $cacheableMetadata->getCacheMaxAge() . ' seconds'; } } } /** Loading
modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php +36 −10 Original line number Diff line number Diff line Loading @@ -45,6 +45,11 @@ class DefaultFacetsSummaryManager { */ protected $facetManager; /** * @var \Drupal\facets\FacetInterface[] */ protected $facets = []; /** * Constructs a new instance of the DefaultFacetManager. * Loading Loading @@ -83,24 +88,16 @@ class DefaultFacetsSummaryManager { */ public function build(FacetsSummaryInterface $facets_summary) { // Let the facet_manager build the facets. $facets = $this->getFacets($facets_summary); $facets_config = $facets_summary->getFacets(); $facetsource_id = $facets_summary->getFacetSourceId(); /** @var \Drupal\facets\Entity\Facet[] $facets */ $facets = $this->facetManager->getFacetsByFacetSourceId($facetsource_id); // Get the current results from the facets and let all processors that // trigger on the build step do their build processing. // @see \Drupal\facets\Processor\BuildProcessorInterface. // @see \Drupal\facets\Processor\SortProcessorInterface. $this->facetManager->updateResults($facetsource_id); $facets_config = $facets_summary->getFacets(); // Exclude facets which were not selected for this summary. $facets = array_filter($facets, function ($item) use ($facets_config) { return (isset($facets_config[$item->id()])); } ); foreach ($facets as $facet) { // Do not build the facet in summary if facet is not rendered. if (!$facet->getActiveItems()) { Loading Loading @@ -179,4 +176,33 @@ class DefaultFacetsSummaryManager { return $items; } /** * Get facet entities. * * @param \Drupal\facets_summary\FacetsSummaryInterface $facets_summary * The facet we should build. * * @return \Drupal\facets\FacetInterface[] * The facet entities. * * @throws \Drupal\facets\Exception\InvalidProcessorException */ public function getFacets(FacetsSummaryInterface $facets_summary): array { $facetsource_id = $facets_summary->getFacetSourceId(); if (!isset($this->facets[$facetsource_id])) { $this->facets[$facetsource_id] = $this->facetManager->getFacetsByFacetSourceId($facetsource_id); $facets_config = $facets_summary->getFacets(); // Exclude facets which were not selected for this summary. $facets = array_filter($this->facets[$facetsource_id], function ($item) use ($facets_config) { return (isset($facets_config[$item->id()])); } ); } return $this->facets[$facetsource_id]; } }
modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php +46 −5 Original line number Diff line number Diff line Loading @@ -3,7 +3,8 @@ namespace Drupal\facets_summary\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\UncacheableDependencyTrait; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\facets_summary\Entity\FacetsSummary; use Drupal\facets_summary\FacetsSummaryBlockInterface; Loading @@ -20,8 +21,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterface, ContainerFactoryPluginInterface { use UncacheableDependencyTrait; /** * The facet manager service. * Loading @@ -36,6 +35,13 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac */ protected $facetsSummary; /** * The cacheable metadata. * * @var \Drupal\Core\Cache\CacheableMetadata */ protected $cacheableMetadata; /** * Constructs a source summary block. * Loading Loading @@ -96,6 +102,8 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac // Add contextual links only when we have results. if (!empty($build)) { CacheableMetadata::createFromObject($this)->applyTo($build); $build['#contextual_links']['facets_summary'] = [ 'route_parameters' => ['facets_summary' => $facets_summary->id()], ]; Loading @@ -119,8 +127,41 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac /** * {@inheritdoc} */ public function getPreviewFallbackString() { return $this->t('Placeholder for the "@facet_summary" facet summary', ['@facet_summary' => $this->getDerivativeId()]); public function getCacheTags() { $this->calculateCacheDependencies(); return Cache::mergeTags(parent::getCacheTags(), $this->cacheableMetadata->getCacheTags()); } /** * {@inheritdoc} */ public function getCacheContexts() { $this->calculateCacheDependencies(); return Cache::mergeContexts(parent::getCacheContexts(), $this->cacheableMetadata->getCacheContexts()); } /** * {@inheritdoc} */ public function getCacheMaxAge() { $this->calculateCacheDependencies(); return Cache::mergeMaxAges(parent::getCacheMaxAge(), $this->cacheableMetadata->getCacheMaxAge()); } /** * @throws \Drupal\facets\Exception\InvalidProcessorException */ protected function calculateCacheDependencies(): void { if (!$this->cacheableMetadata) { $this->cacheableMetadata = new CacheableMetadata(); foreach ($this->facetsSummaryManager->getFacets($this->getEntity()) as $facet) { $this->cacheableMetadata->addCacheableDependency($facet); } } } }
modules/facets_summary/templates/facets-summary-item-list.html.twig +14 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,20 @@ * @ingroup themeable */ #} {% if cache_hash %} <!-- facets cacheable metadata hash: {{ cache_hash }} {% if cache_contexts %} contexts: {{ cache_contexts }} {%- endif %} {% if cache_tags %} tags: {{ cache_tags }} {%- endif %} {% if cache_max_age %} max age: {{ cache_max_age }} {%- endif %} --> {%- endif %} {% if context.list_style %} {%- set attributes = attributes.addClass('item-list__' ~ context.list_style) %} {% endif %} Loading