Skip to content

Commit

Permalink
OPENEUROPA-1798: Add a base metadata plugin for node routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
brummbar committed Apr 24, 2019
1 parent f6c9532 commit 7991dbc
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -20,7 +21,7 @@
* weight = -1
* )
*/
class NewsContentType extends EntityCanonicalRoutePage {
class NewsContentType extends NodeViewRouteBase {

use StringTranslationTrait;

Expand All @@ -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;
}

Expand All @@ -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')
);
}
Expand All @@ -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';
}

/**
Expand All @@ -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.
Expand All @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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';
}

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_theme_helper\Plugin\PageHeaderMetadata;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\oe_theme_helper\PageHeaderMetadataPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Base plugin to handle metadata for node view routes.
*
* This is a base plugin as it should be extended to return extra metadata
* for the node.
*/
abstract class NodeViewRouteBase extends PageHeaderMetadataPluginBase implements ContainerFactoryPluginInterface {

/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $currentRouteMatch;

/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Creates a new NodeViewRouteBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* 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.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->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;
}

}

0 comments on commit 7991dbc

Please sign in to comment.