From baeba6148944b31e558a59f76a67eea018d8a221 Mon Sep 17 00:00:00 2001 From: Erik Stielstra <40452-sutharsan@users.noreply.drupalcode.org> Date: Wed, 1 Feb 2023 17:32:26 +0100 Subject: [PATCH] Issue #3016860 by Sutharsan, tregismoreira: Find entity in the route parameters (needed for subpaths) --- src/Plugin/Block/FieldBlock.php | 63 +++++++++++++++++---------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/Plugin/Block/FieldBlock.php b/src/Plugin/Block/FieldBlock.php index 394d800..aed3d10 100644 --- a/src/Plugin/Block/FieldBlock.php +++ b/src/Plugin/Block/FieldBlock.php @@ -443,42 +443,43 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface { * * @return \Drupal\Core\Entity\ContentEntityInterface|null * The entity to be used when displaying the block. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ 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'); - if (is_numeric($entity_revision)) { - $entity = $this->entityTypeManager->getStorage('node')->loadRevision($entity_revision); - } - else { - $entity = $entity_revision; - } - } - elseif ($route_name == 'entity.node.preview' && $this->routeMatch->getParameter('view_mode_id') === 'full') { - $entity = $this->routeMatch->getParameter('node_preview'); - } - } + if (isset($this->fieldBlockEntity)) { + return $this->fieldBlockEntity; + } + + $entity_type = $this->getDerivativeId(); + $field_name = $this->configuration['field_name']; - if ($entity instanceof ContentEntityInterface && $entity->getEntityTypeId() === $entity_type && $entity->hasField($field_name)) { - $this->fieldBlockEntity = $entity; + $route = $this->routeMatch->getRouteObject(); + if (empty($route)) { + return NULL; + } + + $parameters = $route->getOption('parameters'); + if (empty($parameters)) { + return NULL; + } + + // Check if any of the route parameters represents an entity. Use the + // first one that is of the right type. + foreach ($parameters as $name => $options) { + if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) { + $entity = $this->routeMatch->getParameter($name); + + if ($entity instanceof ContentEntityInterface + && $entity->hasLinkTemplate('canonical') + && $entity->getEntityTypeId() === $entity_type + && $entity->hasField($field_name) + ) { + $this->fieldBlockEntity = $entity; + return $this->fieldBlockEntity; + } } } - return $this->fieldBlockEntity; + + return NULL; } } -- GitLab