You are here

protected function FieldBlock::getEntity in Field as Block 8.2

Finds the entity to be used when displaying the block.

Return value

\Drupal\Core\Entity\ContentEntityInterface|null The entity to be used when displaying the block.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

3 calls to FieldBlock::getEntity()
FieldBlock::blockAccess in src/Plugin/Block/FieldBlock.php
Indicates whether the block should be shown.
FieldBlock::build in src/Plugin/Block/FieldBlock.php
Builds and returns the renderable array for this block plugin.
FieldBlock::getCacheTags in src/Plugin/Block/FieldBlock.php
The cache tags associated with this object.

File

src/Plugin/Block/FieldBlock.php, line 446

Class

FieldBlock
Provides a fieldblock.

Namespace

Drupal\fieldblock\Plugin\Block

Code

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->entityTypeManager
          ->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;
}