From 7991dbc81e7daa1b31b3ed9f1acf74cbd70f8f98 Mon Sep 17 00:00:00 2001 From: Francesco Sardara Date: Wed, 24 Apr 2019 22:25:52 +0200 Subject: [PATCH] OPENEUROPA-1798: Add a base metadata plugin for node routes. --- .../PageHeaderMetadata/NewsContentType.php | 30 ++-- .../PageHeaderMetadata/PageContentType.php | 12 +- .../PageHeaderMetadata/NodeViewRouteBase.php | 131 ++++++++++++++++++ 3 files changed, 154 insertions(+), 19 deletions(-) create mode 100644 modules/oe_theme_helper/src/Plugin/PageHeaderMetadata/NodeViewRouteBase.php diff --git a/modules/oe_theme_content_news/src/Plugin/PageHeaderMetadata/NewsContentType.php b/modules/oe_theme_content_news/src/Plugin/PageHeaderMetadata/NewsContentType.php index ec3ab8b55c..a9e842646c 100644 --- a/modules/oe_theme_content_news/src/Plugin/PageHeaderMetadata/NewsContentType.php +++ b/modules/oe_theme_content_news/src/Plugin/PageHeaderMetadata/NewsContentType.php @@ -5,10 +5,11 @@ namespace Drupal\oe_theme_content_news\Plugin\PageHeaderMetadata; use Drupal\Core\Datetime\DateFormatterInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\node\NodeInterface; -use Drupal\oe_theme_helper\Plugin\PageHeaderMetadata\EntityCanonicalRoutePage; +use Drupal\oe_theme_helper\Plugin\PageHeaderMetadata\NodeViewRouteBase; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -20,7 +21,7 @@ * weight = -1 * ) */ -class NewsContentType extends EntityCanonicalRoutePage { +class NewsContentType extends NodeViewRouteBase { use StringTranslationTrait; @@ -42,11 +43,15 @@ class NewsContentType extends EntityCanonicalRoutePage { * The plugin implementation definition. * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match * The current route match. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter * The date formatter. */ - public function __construct(array $configuration, string $plugin_id, $plugin_definition, RouteMatchInterface $current_route_match, DateFormatterInterface $date_formatter) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $current_route_match); + public function __construct(array $configuration, string $plugin_id, $plugin_definition, RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, DateFormatterInterface $date_formatter) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $current_route_match, $entity_type_manager, $entity_repository); $this->dateFormatter = $date_formatter; } @@ -59,6 +64,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('current_route_match'), + $container->get('entity_type.manager'), + $container->get('entity.repository'), $container->get('date.formatter') ); } @@ -67,10 +74,9 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function applies(): bool { - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = $this->getEntityFromCurrentRoute(); + $node = $this->getNode(); - return $entity instanceof NodeInterface && $entity->bundle() === 'oe_news'; + return $node && $node->bundle() === 'oe_news'; } /** @@ -79,9 +85,9 @@ public function applies(): bool { public function getMetadata(): array { $metadata = parent::getMetadata(); - $entity = $this->getEntityFromCurrentRoute(); - if (!$entity->get('oe_news_summary')->isEmpty()) { - $summary = $entity->get('oe_news_summary')->first(); + $node = $this->getNode(); + if (!$node->get('oe_news_summary')->isEmpty()) { + $summary = $node->get('oe_news_summary')->first(); $metadata['introduction'] = [ // We strip the tags because the component expects only one paragraph of // text and the field is using a text format which adds paragraph tags. @@ -98,7 +104,7 @@ public function getMetadata(): array { ]; } - $timestamp = $entity->get('oe_news_publication_date')->date->getTimestamp(); + $timestamp = $node->get('oe_news_publication_date')->date->getTimestamp(); $metadata['metas'] = [ $this->t('News'), $this->dateFormatter->format($timestamp, 'oe_theme_news_date'), diff --git a/modules/oe_theme_content_page/src/Plugin/PageHeaderMetadata/PageContentType.php b/modules/oe_theme_content_page/src/Plugin/PageHeaderMetadata/PageContentType.php index 99ae785edb..2b87096d83 100644 --- a/modules/oe_theme_content_page/src/Plugin/PageHeaderMetadata/PageContentType.php +++ b/modules/oe_theme_content_page/src/Plugin/PageHeaderMetadata/PageContentType.php @@ -4,8 +4,7 @@ namespace Drupal\oe_theme_content_page\Plugin\PageHeaderMetadata; -use Drupal\node\NodeInterface; -use Drupal\oe_theme_helper\Plugin\PageHeaderMetadata\EntityCanonicalRoutePage; +use Drupal\oe_theme_helper\Plugin\PageHeaderMetadata\NodeViewRouteBase; /** * Page header metadata for the OpenEuropa Page content entity. @@ -16,16 +15,15 @@ * weight = -1 * ) */ -class PageContentType extends EntityCanonicalRoutePage { +class PageContentType extends NodeViewRouteBase { /** * {@inheritdoc} */ public function applies(): bool { - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - $entity = $this->getEntityFromCurrentRoute(); + $node = $this->getNode(); - return $entity instanceof NodeInterface && $entity->bundle() === 'oe_page'; + return $node && $node->bundle() === 'oe_page'; } /** @@ -34,7 +32,7 @@ public function applies(): bool { public function getMetadata(): array { $metadata = parent::getMetadata(); - $entity = $this->getEntityFromCurrentRoute(); + $entity = $this->getNode(); if ($entity->get('oe_page_summary')->isEmpty()) { return $metadata; } diff --git a/modules/oe_theme_helper/src/Plugin/PageHeaderMetadata/NodeViewRouteBase.php b/modules/oe_theme_helper/src/Plugin/PageHeaderMetadata/NodeViewRouteBase.php new file mode 100644 index 0000000000..1583a81410 --- /dev/null +++ b/modules/oe_theme_helper/src/Plugin/PageHeaderMetadata/NodeViewRouteBase.php @@ -0,0 +1,131 @@ +currentRouteMatch = $current_route_match; + $this->entityTypeManager = $entity_type_manager; + $this->entityRepository = $entity_repository; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_route_match'), + $container->get('entity_type.manager'), + $container->get('entity.repository') + ); + } + + /** + * {@inheritdoc} + */ + public function getMetadata(): array { + $entity = $this->getNode(); + + $metadata = []; + + $cacheability = new CacheableMetadata(); + $cacheability + ->addCacheableDependency($entity) + ->addCacheContexts(['route']) + ->applyTo($metadata); + + return $metadata; + } + + /** + * Returns the node retrieved from the current route. + * + * @todo Add caching, this method is called 3 times on child classes. + * + * @return \Drupal\node\NodeInterface|null + * The node entity, or NULL if not found. + */ + protected function getNode(): ?NodeInterface { + $supported = [ + 'entity.node.canonical', + 'entity.node.latest', + 'entity.node.revision', + ]; + + if (!in_array($this->currentRouteMatch->getRouteName(), $supported)) { + return NULL; + } + + if ($this->currentRouteMatch->getRouteName() === 'entity.node.revision') { + $node_revision = $this->currentRouteMatch->getParameter('node_revision'); + $node = $this->entityTypeManager->getStorage('node')->loadRevision($node_revision); + + return $node ? $this->entityRepository->getTranslationFromContext($node) : NULL; + } + + // If a node object is present in the route, use that one. + $node = $this->currentRouteMatch->getParameter('node'); + return $node instanceof NodeInterface ? $node : NULL; + } + +}