From 12ba90ec5377e6d676fde1ce856aebdbe8051485 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 4 Mar 2024 15:27:33 -0400 Subject: [PATCH] Start thinking to roll in more cache headers; however, shouldn't be necessary. --- embargo.services.yml | 4 ++ .../ViewsCacheEventSubscriber.php | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/EventSubscriber/ViewsCacheEventSubscriber.php diff --git a/embargo.services.yml b/embargo.services.yml index 38b4738..0401c2c 100644 --- a/embargo.services.yml +++ b/embargo.services.yml @@ -31,3 +31,7 @@ services: - '@url_generator' tags: - { name: 'event_subscriber' } + embargo.search_api.pre_execute_subscriber: + class: Drupal\embargo\EventSubscriber\ViewsCacheEventSubscriber + tags: + - { name: 'event_subscriber' } diff --git a/src/EventSubscriber/ViewsCacheEventSubscriber.php b/src/EventSubscriber/ViewsCacheEventSubscriber.php new file mode 100644 index 0000000..5f8ac0c --- /dev/null +++ b/src/EventSubscriber/ViewsCacheEventSubscriber.php @@ -0,0 +1,63 @@ + 'preExecute', + //'search_api.query_pre_execute.alter_cache_metadata' => ['alterCacheMetadata'], + ]; + } + + public function preExecute(QueryPreExecuteEvent $event) { + dsm($event->getQuery()->getTags()); + if ($event->getQuery()->hasTag('alter_cache_metadata')) { + $this->alterCacheMetadata($event); + } + + $query = $event->getQuery(); + if ($query instanceof RefinableCacheableDependencyInterface) { + dsm($query->getCacheContexts()); + dsm($query->getCacheTags()); + dsm($query->getCacheMaxAge()); + } + } + + /** + * Alter cache metadata. + * + * @param \Drupal\search_api\Event\QueryPreExecuteEvent $event + * The pre-execution event. + */ + public function alterCacheMetadata(QueryPreExecuteEvent $event) : void { + $query = $event->getQuery(); + if (!($query instanceof RefinableCacheableDependencyInterface)) { + dsm('blah'); + // Cannot add metadata to it. + return; + } + if ($this->containsRelevantIndex($query)) { + //$query->add + } + } + + protected function containsRelevantIndex(QueryInterface $query) : bool { + dsm($query->getTags()); + return FALSE; + } + +}