function entity_reference_integrity_enforce_entity_access in Entity Reference Integrity 8
Implements hook_entity_access().
File
- modules/
entity_reference_integrity_enforce/ entity_reference_integrity_enforce.module, line 40 - Module file.
Code
function entity_reference_integrity_enforce_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Only check access for delete operations.
if ($operation != 'delete') {
return AccessResult::neutral();
}
// Only check access for valid routes. This can return NULL.
$route = \Drupal::routeMatch()
->getRouteObject();
if (empty($route)) {
return AccessResult::neutral();
}
// Only check access for API endpoints by checking the route _format.
// @todo Check formats for other API endpoints such as GraphQL and REST.
$api_formats = [
'api_json',
];
$format = $route
->getRequirement('_format');
if (empty($format) || !in_array($format, $api_formats)) {
return AccessResult::neutral();
}
/** @var \Drupal\entity_reference_integrity\EntityReferenceIntegrityEntityHandler $entity_reference_integrity_handler */
$entity_reference_integrity_handler = \Drupal::entityTypeManager()
->getHandler($entity
->getEntityTypeId(), 'entity_reference_integrity');
$enabled_entity_type_ids = \Drupal::configFactory()
->get('entity_reference_integrity_enforce.settings')
->get('enabled_entity_type_ids');
// Finally deny access if the entity has dependents.
if (in_array($entity
->getEntityTypeId(), $enabled_entity_type_ids, TRUE) && $entity_reference_integrity_handler
->hasDependents($entity)) {
$reason = EntityReferenceIntegrityEntityHandler::getAccessDeniedReason($entity, FALSE);
return new AccessResultForbidden($reason);
}
return AccessResult::neutral();
}