1

I have several custom entities in my projects.

To check access for certain links, I would like to utilize the checkAccess() and checkCreateAccess() functions in the MyEntityAccessControlHandler classes.

How do I call these from the Controller class or from the module file?

0

1 Answer 1

0

Something like this should do the trick.

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;    

$entity_type_mgr = \Drupal::getContainer()->get('entity_type.manager');
if (!$entity_type_mgr->getAccessControlHandler($type)->createAccess($bundle)) {
  $permission = "create $bundle $type entities";
  throw new AccessDeniedHttpException("You don't have permission to " . $permission);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.