Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENEUROPA-1798: Page header content is not reflecting changes made before node is published #215

Merged
merged 14 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\NodeViewRoutesBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

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

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,11 @@ public function applies(): bool {
public function getMetadata(): array {
$metadata = parent::getMetadata();

$entity = $this->getEntityFromCurrentRoute();
if (!$entity->get('oe_summary')->isEmpty()) {
$summary = $entity->get('oe_summary')->first();
$node = $this->getNode();
if (!($node->get('oe_summary')->isEmpty())) {

$summary = $node->get('oe_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 +106,7 @@ public function getMetadata(): array {
];
}

$timestamp = $entity->get('oe_publication_date')->date->getTimestamp();
$timestamp = $node->get('oe_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\NodeViewRoutesBase;

/**
* Page header metadata for the OpenEuropa Page content entity.
Expand All @@ -16,16 +15,15 @@
* weight = -1
* )
*/
class PageContentType extends EntityCanonicalRoutePage {
class PageContentType extends NodeViewRoutesBase {

/**
* {@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,12 +32,12 @@ public function applies(): bool {
public function getMetadata(): array {
$metadata = parent::getMetadata();

$entity = $this->getEntityFromCurrentRoute();
if ($entity->get('oe_summary')->isEmpty()) {
$node = $this->getNode();
if ($node->get('oe_summary')->isEmpty()) {
return $metadata;
}

$summary = $entity->get('oe_summary')->first();
$summary = $node->get('oe_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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class PageHeaderContext implements ContextProviderInterface {
use StringTranslationTrait;

/**
* The page header metadata plugin mananger.
* The page header metadata plugin manager.
*
* @var \Drupal\oe_theme_helper\PageHeaderMetadataPluginManager
*/
protected $metadataPluginManager;

/**
* Instanciates a new PageHeader context object.
* Instantiates a new PageHeader context object.
*
* @param \Drupal\oe_theme_helper\PageHeaderMetadataPluginManager $metadata_plugin_manager
* The page header metadata plugin manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* id = "oe_theme_helper_page_header",
* admin_label = @Translation("Page header"),
* category = @Translation("OpenEuropa"),
* context = {
* context_definitions = {
* "page_header" = @ContextDefinition("map", label = @Translation("Page header metadata"))
* }
* )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* id = "entity_canonical_route",
* label = @Translation("Default entity metadata extractor")
* )
*
* @deprecated in 1.x, will be removed in 2.0.
*/
class EntityCanonicalRoutePage extends PageHeaderMetadataPluginBase implements ContainerFactoryPluginInterface {

Expand Down Expand Up @@ -62,9 +64,7 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function applies(): bool {
$entity = $this->getEntityFromCurrentRoute();

return !empty($entity);
return FALSE;
}

/**
Expand Down Expand Up @@ -95,8 +95,13 @@ public function getEntityFromCurrentRoute(): ?ContentEntityInterface {
foreach ($parameters as $name => $options) {
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
$entity = $this->currentRouteMatch->getParameter($name);
if ($entity instanceof ContentEntityInterface && $this->currentRouteMatch->getRouteName() === "entity.{$entity->getEntityTypeId()}.canonical") {
return $entity;
if ($entity instanceof ContentEntityInterface) {
if ($this->currentRouteMatch->getRouteName() === "entity.{$entity->getEntityTypeId()}.canonical") {
return $entity;
}
elseif ($this->currentRouteMatch->getRouteName() === "entity.{$entity->getEntityTypeId()}.revision") {
return $entity;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?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 NodeViewRoutesBase 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.
*
* @return \Drupal\node\NodeInterface|null
* The node entity, or NULL if not found.
*/
protected function getNode(): ?NodeInterface {
$supported = [
'entity.node.canonical',
'entity.node.latest_version',
'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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types = 1);

namespace Drupal\page_header_metadata_test\Plugin\PageHeaderMetadata;

use Drupal\oe_theme_helper\Plugin\PageHeaderMetadata\NodeViewRoutesBase;

/**
* Test implementation of a metadata plugin for nodes of bundle "test".
*
* @PageHeaderMetadata(
* id = "node_view_routes_test_plugin",
* label = @Translation("Node view routes metadata test plugin")
* )
*/
class NodeViewRoutesTestPlugin extends NodeViewRoutesBase {

/**
* {@inheritdoc}
*/
public function applies(): bool {
$node = $this->getNode();

return $node && $node->getType() === 'test';
}

/**
* {@inheritdoc}
*/
public function getMetadata(): array {
$metadata = parent::getMetadata();

$metadata['introduction'] = $this->getNode()->get('body')->value;

return $metadata;
}

}
Loading