Skip to content
Snippets Groups Projects
Commit baeba614 authored by Erik Stielstra's avatar Erik Stielstra
Browse files

Issue #3016860 by Sutharsan, tregismoreira: Find entity in the route...

Issue #3016860 by Sutharsan, tregismoreira: Find entity in the route parameters (needed for subpaths)
parent a2c775a6
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment