Commit 85d12a09 authored by git's avatar git Committed by Sutharsan

Issue #2605874 by dpacassi, casey, nkoporec: Display fieldblocks on entity revision/preview routes

parent 292e62d3
......@@ -51,6 +51,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
*/
protected $routeMatch;
/**
* The entity to be used when displaying the block.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $fieldBlockEntity;
/**
* {@inheritdoc}
*
......@@ -314,10 +321,9 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
$entity_type = $this->getDerivativeId();
$entity = $this->routeMatch->getParameter($entity_type);
$entity = $this->getEntity();
if ($entity instanceof ContentEntityInterface && $entity->getEntityTypeId() === $entity_type && $entity->hasField($this->configuration['field_name'])) {
if ($entity) {
$field = $entity->get($this->configuration['field_name']);
return AccessResult::allowedIf($field->access('view', $account));
}
......@@ -329,10 +335,9 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
*/
public function build() {
$build = [];
$entity_type = $this->getDerivativeId();
$entity = $this->routeMatch->getParameter($entity_type);
$entity = $this->getEntity();
if ($entity instanceof ContentEntityInterface) {
if ($entity) {
$build['field'] = $entity->get($this->configuration['field_name'])->view([
'label' => 'hidden',
'type' => $this->configuration['formatter_id'],
......@@ -350,9 +355,8 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* {@inheritdoc}
*/
public function getCacheTags() {
$entity_type = $this->getDerivativeId();
$entity = $this->routeMatch->getParameter($entity_type);
if ($entity instanceof ContentEntityInterface) {
$entity = $this->getEntity();
if ($entity) {
return $entity->getCacheTags();
}
return parent::getCacheTags();
......@@ -366,4 +370,39 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
// and its own fields.
return ['route'];
}
/**
* Finds the entity to be used when displaying the block.
*
* @return \Drupal\Core\Entity\ContentEntityInterface|null
* The entity to be used when displaying the block.
*/
protected function getEntity() {
if (!isset($this->fieldBlockEntity)) {
$entity_type = $this->getDerivativeId();
$entity = NULL;
$field_name = $this->configuration['field_name'];
$route_name = $this->routeMatch->getRouteName();
$is_canonical_route = $route_name === 'entity.' . $entity_type . '.canonical';
$is_latest_route = $route_name == 'entity.' . $entity_type . '.latest_version';
if ($is_canonical_route || $is_latest_route) {
$entity = $this->routeMatch->getParameter($entity_type);
}
elseif ($entity_type === 'node') {
if ($route_name == 'entity.node.revision') {
$entity_revision = $this->routeMatch->getParameter('node_revision');
$entity = $this->entityManager->getStorage('node')->loadRevision($entity_revision);
}
elseif ($route_name == 'entity.node.preview' && $this->routeMatch->getParameter('view_mode_id') === 'full') {
$entity = $this->routeMatch->getParameter('node_preview');
}
}
if ($entity instanceof ContentEntityInterface && $entity->getEntityTypeId() === $entity_type && $entity->hasField($field_name)) {
$this->fieldBlockEntity = $entity;
}
}
return $this->fieldBlockEntity;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment