diff --git a/src/Plugin/Block/EntityBlock.php b/src/Plugin/Block/EntityBlock.php index 27c1ebb..fd5d983 100644 --- a/src/Plugin/Block/EntityBlock.php +++ b/src/Plugin/Block/EntityBlock.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityDisplayRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -147,12 +148,41 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface { * {@inheritdoc} */ public function build() { - if ($entity_id = $this->configuration['entity']) { + if ($entity = $this->getEntity()) { $view_mode = isset($this->configuration['view_mode']) ? $this->configuration['view_mode'] : 'default'; + return $this->entityViewBuilder->view($entity, $view_mode); + } + else { + return []; + } + } - if ($entity = $this->entityStorage->load($entity_id)) { - return $this->entityViewBuilder->view($entity, $view_mode); - } + /** + * {@inheritdoc} + */ + protected function blockAccess(AccountInterface $account) { + if ($entity = $this->getEntity()) { + return $this->entityTypeManager + ->getAccessControlHandler($this->entityTypeName) + ->access($entity, 'view', $account, TRUE); + } + else { + return parent::blockAccess($account); + } + } + + /** + * Returns the entity to display. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The entity to display, or NULL if none is configured. + */ + protected function getEntity() { + if ($entity_id = $this->configuration['entity']) { + return $this->entityStorage->load($entity_id); + } + else { + return NULL; } }