Created
November 4, 2016 16:23
-
-
Save davereid/f950dce02f2641540845cf8bbc2a2ee1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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