function EntityField::blockAccess
Same name in other branches
- 8.x-3.x modules/ctools_block/src/Plugin/Block/EntityField.php \Drupal\ctools_block\Plugin\Block\EntityField::blockAccess()
Overrides BlockPluginTrait::blockAccess
File
-
modules/
ctools_block/ src/ Plugin/ Block/ EntityField.php, line 156
Class
- EntityField
- Provides a block to a field on an entity.
Namespace
Drupal\ctools_block\Plugin\BlockCode
protected function blockAccess(AccountInterface $account) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->getContextValue('entity');
// Make sure we have access to the entity.
$access = $entity->access('view', $account, TRUE);
if ($access->isAllowed()) {
// Check that the entity in question has this field.
if ($entity instanceof FieldableEntityInterface && $entity->hasField($this->fieldName)) {
// Check field access.
$field_access = $this->entityTypeManager
->getAccessControlHandler($this->entityTypeId)
->fieldAccess('view', $this->getFieldDefinition(), $account);
if ($field_access) {
// Build a renderable array for the field.
$build = $entity->get($this->fieldName)
->view($this->configuration['formatter']);
// If there are actual renderable children, grant access.
if (Element::children($build)) {
return AccessResult::allowed();
}
}
}
// Entity doesn't have this field, so access is denied.
return AccessResult::forbidden();
}
// If we don't have access to the entity, return the forbidden result.
return $access;
}