diff --git a/src/Plugin/Block/EntityField.php b/src/Plugin/Block/EntityField.php index 390f9e0..30a9f4e 100644 --- a/src/Plugin/Block/EntityField.php +++ b/src/Plugin/Block/EntityField.php @@ -8,6 +8,7 @@ namespace Drupal\ctools\Plugin\Block; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\EntityFieldManagerInterface; @@ -145,11 +146,8 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont /** @var \Drupal\Core\Field\FieldItemListInterface $field */ $field = $entity->{$this->fieldName}; - $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); - if ($access_handler->access($entity, 'view') && $access_handler->fieldAccess('view', $this->getFieldDefinition(), NULL, $field)) { - $display_settings = $this->getConfiguration()['formatter']; - $build['field'] = $field->view($display_settings); - } + $display_settings = $this->getConfiguration()['formatter']; + $build['field'] = $field->view($display_settings); } CacheableMetadata::createFromObject($entity) @@ -159,11 +157,23 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont } /** - * @inheritDoc + * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { - $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); - return $access_handler->fieldAccess('view', $this->getFieldDefinition(), $account, NULL, TRUE); + /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ + $entity = $this->getContextValue('entity'); + + if ($entity instanceof FieldableEntityInterface && $entity->hasField($this->fieldName)) { + /** @var \Drupal\Core\Field\FieldItemListInterface $field */ + $field = $entity->{$this->fieldName}; + + $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); + if ($access_handler->access($entity, 'view') && $access_handler->fieldAccess('view', $this->getFieldDefinition(), $account, $field)) { + return AccessResult::allowed(); + } + } + + return AccessResult::forbidden(); } /**