Skip to content

Instantly share code, notes, and snippets.

@davereid
Created November 4, 2016 16:23
Show Gist options
  • Save davereid/f950dce02f2641540845cf8bbc2a2ee1 to your computer and use it in GitHub Desktop.
Save davereid/f950dce02f2641540845cf8bbc2a2ee1 to your computer and use it in GitHub Desktop.
<?php
use Drupal\user\Entity\User;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_entity_access().
*/
function entity_delete_protect_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'delete' && $entity->access('view', User::getAnonymousUser()) {
return AccessResult::forbidden();
}
else {
return AccessResult::neutral();
}
}
/**
* Implements hook_entity_predelete().
*/
function entity_delete_protect_entity_predelete(EntityInterface $entity) {
if ($entity->access('view', User::getAnonymousUser()) {
throw new \RuntimeException("Unable to delete entity because it is visible to anonymous users.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment