diff --git a/amp.routing.yml b/amp.routing.yml index b3c1f7f..0e3fff9 100644 --- a/amp.routing.yml +++ b/amp.routing.yml @@ -7,12 +7,12 @@ amp.settings: _permission: 'administer site configuration' amp.node_amp_page: - path: '/node/{node}/revisions/{node_revision}/view' + path: '/node/{node}' defaults: - _controller: '\Drupal\amp\Controller\ampNodeController::revisionShow' - _title_callback: '\Drupal\node\Controller\NodeController::revisionPageTitle' + _controller: '\Drupal\amp\Controller\ampNodeViewController::view' + _title_callback: '\Drupal\node\Controller\NodeViewController::title' requirements: - _permission: 'access content' + _entity_access: 'node.view' node: \d+ options: _amp_route: FALSE diff --git a/src/Controller/ampNodeController.php b/src/Controller/ampNodeController.php deleted file mode 100644 index 0062897..0000000 --- a/src/Controller/ampNodeController.php +++ /dev/null @@ -1,141 +0,0 @@ -ampContext = $amp_context; - $this->configFactory = $config_factory_interface; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('router.amp_context'), - $container->get('config.factory'), - $container->get('date.formatter'), - $container->get('renderer') - ); - } - - public function warningsOn() - { - // First check the config if library warnings are on - $amp_config = $this->configFactory->get('amp.settings'); - if ($amp_config->get('amp_library_warnings_display')) { - return true; - } - - // Then check the URL if library warnings are enabled - /** @var Request $request */ - $request = \Drupal::request(); - $user_wants_amp_library_warnings = $request->get('warnfix'); - if (isset($user_wants_amp_library_warnings)) { - return true; - } - - return false; - } - - /** - * Displays an AMP node revision. - * - * @param int $node_revision - * The node revision ID. - * - * @return array - * An array suitable for drupal_render(). - */ - public function revisionShow($node_revision) { - // Only use the AMP view mode for content that is AMP enabled. - if ($this->ampContext->isAmpRoute()) { - $node = $this->entityManager()->getStorage('node')->loadRevision($node_revision); - $node_view_controller = new NodeViewController($this->entityManager, $this->renderer); - - $page = $node_view_controller->view($node, 'amp'); - - // Otherwise adding a ?warnfix query parameter at the end of URL will have no effect - $page['#cache']['contexts'] = Cache::mergeContexts($page['#cache']['contexts'], ['url.query_args:warnfix']); - if ($this->warningsOn()) { - $page['#cache']['keys'][] = 'amp-warnings-on'; - } - else { - $page['#cache']['keys'][] = 'amp-warnings-off'; - } - - unset($page['nodes'][$node->id()]['#cache']); - } - // Otherwise return the default view mode. - else { - $page = parent::revisionShow($node_revision); - } - - return $page; - } -} diff --git a/src/Controller/ampNodeViewController.php b/src/Controller/ampNodeViewController.php new file mode 100644 index 0000000..e118a36 --- /dev/null +++ b/src/Controller/ampNodeViewController.php @@ -0,0 +1,153 @@ +ampContext = $amp_context; + $this->configFactory = $config_factory_interface; } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('router.amp_context'), + $container->get('config.factory'), + $container->get('entity.manager'), + $container->get('renderer') + ); + } + + public function warningsOn() + { + // First check the config if library warnings are on + $amp_config = $this->configFactory->get('amp.settings'); + if ($amp_config->get('amp_library_warnings_display')) { + return true; + } + + // Then check the URL if library warnings are enabled + /** @var Request $request */ + $request = \Drupal::request(); + $user_wants_amp_library_warnings = $request->get('warnfix'); + if (isset($user_wants_amp_library_warnings)) { + return true; + } + + return false; + } + + /** + * {@inheritdoc} + */ + public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) { + + // Only use the AMP view mode for content that is AMP enabled and in full + // view mode. + if ($this->ampContext->isAmpRoute() && $view_mode == 'full') { + $build = parent::view($node, 'amp', $langcode); + + // Otherwise adding a ?amp query parameter at the end of URL will have no effect + $build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts'], ['url.query_args:amp']); + // Otherwise adding a ?warnfix query parameter at the end of URL will have no effect + $build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts'], ['url.query_args:warnfix']); + if ($this->warningsOn()) { + $build['#cache']['keys'][] = 'amp-warnings-on'; + } + else { + $build['#cache']['keys'][] = 'amp-warnings-off'; + } + } + // Otherwise return the default view mode. + else { + $build = parent::view($node, $view_mode, $langcode); + } + + foreach ($node->uriRelationships() as $rel) { + // Set the node path as the canonical URL to prevent duplicate content. + $build['#attached']['html_head_link'][] = array( + array( + 'rel' => $rel, + 'href' => $node->url($rel), + ), + TRUE, + ); + + if ($rel == 'canonical') { + // Set the non-aliased canonical path as a default shortlink. + $build['#attached']['html_head_link'][] = array( + array( + 'rel' => 'shortlink', + 'href' => $node->url($rel, array('alias' => TRUE)), + ), + TRUE, + ); + } + } + + return $build; + } +}