diff --git a/amp.module b/amp.module index 8d7194d..7d00448 100644 --- a/amp.module +++ b/amp.module @@ -58,12 +58,8 @@ function amp_entity_view_alter(array &$build, EntityInterface $entity, EntityVie // Check if entity is a node with an enabled amp view mode. if ($entity->getEntityType()->id() == 'node' && isset($view_modes['amp'])) { - // Otherwise adding 'amp' and 'warnfix' query parameters in URL will have no effect. - $build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts'], ['url.query_args:warnfix']); - if ($build['#view_mode'] == 'amp') { - // Otherwise changing 'amp_library_warnings_display' will have no effect. - $build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], ['config:amp.settings']); - } + // Catches any variations based on 'amp' query parameter in URL. + $build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts'], ['url.amp']); if (!empty($build['#attached']['html_head_link'])) { foreach ($build['#attached']['html_head_link'] as $key => $config) { diff --git a/amp.services.yml b/amp.services.yml index 5e86797..0a26491 100644 --- a/amp.services.yml +++ b/amp.services.yml @@ -1,4 +1,14 @@ services: + cache_context.url.amp: + class: Drupal\amp\Cache\AmpUrlAmpCacheContext + arguments: ['@request_stack'] + tags: + - { name: cache.context } + cache_context.url.warnfix: + class: Drupal\amp\Cache\AmpUrlWarnfixCacheContext + arguments: ['@request_stack'] + tags: + - { name: cache.context } router.amp_context: class: Drupal\amp\Routing\AmpContext arguments: ['@amp.entity_type', '@current_route_match'] diff --git a/src/Cache/AmpUrlAmpCacheContext.php b/src/Cache/AmpUrlAmpCacheContext.php new file mode 100644 index 0000000..53ed9da --- /dev/null +++ b/src/Cache/AmpUrlAmpCacheContext.php @@ -0,0 +1,65 @@ +requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public static function getLabel() { + return t("AMP"); + } + + /** + * {@inheritdoc} + */ + public function getContext() { + $show_amp = $this->requestStack->getCurrentRequest()->get('amp'); + if (isset($show_amp)) { + return 'url.amp:true'; + } + else { + return 'url.amp:false'; + } + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + +} diff --git a/src/Cache/AmpUrlWarnfixCacheContext.php b/src/Cache/AmpUrlWarnfixCacheContext.php new file mode 100644 index 0000000..8ec32b0 --- /dev/null +++ b/src/Cache/AmpUrlWarnfixCacheContext.php @@ -0,0 +1,65 @@ +requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public static function getLabel() { + return t("AMP warnfix"); + } + + /** + * {@inheritdoc} + */ + public function getContext() { + $show_amp_library_warnings = $this->requestStack->getCurrentRequest()->get('warnfix'); + if (isset($show_amp_library_warnings)) { + return 'url.warnfix:true'; + } + else { + return 'url.warnfix:false'; + } + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + +} diff --git a/src/Element/AmpAnalytics.php b/src/Element/AmpAnalytics.php index 1c2f236..02d46bf 100644 --- a/src/Element/AmpAnalytics.php +++ b/src/Element/AmpAnalytics.php @@ -37,7 +37,7 @@ class AmpAnalytics extends RenderElement { ), '#theme' => 'amp_analytics', '#cache' => [ - 'contexts' => ['url.query_args:amp'] + 'contexts' => ['url.amp'] ] ); } diff --git a/src/Element/AmpIframe.php b/src/Element/AmpIframe.php index fdeeb22..850e747 100644 --- a/src/Element/AmpIframe.php +++ b/src/Element/AmpIframe.php @@ -44,7 +44,7 @@ class AmpIframe extends ProcessedText { ), '#theme' => 'amp_iframe', '#cache' => [ - 'contexts' => ['url.query_args:amp', 'url.query_args:warnfix'], + 'contexts' => ['url.amp', 'url.warnfix'], 'tags' => ['config:amp.settings'] ] ); diff --git a/src/Element/AmpProcessedText.php b/src/Element/AmpProcessedText.php index 16ab906..bc81102 100644 --- a/src/Element/AmpProcessedText.php +++ b/src/Element/AmpProcessedText.php @@ -35,7 +35,7 @@ class AmpProcessedText extends ProcessedText { array($class, 'preRenderAmpText'), ), '#cache' => [ - 'contexts' => ['url.query_args:amp', 'url.query_args:warnfix'], + 'contexts' => ['url.amp', 'url.warnfix'], 'tags' => ['config:amp.settings'] ] );