forked from larowlan/block_access
/
block_access.module
39 lines (35 loc) · 1.36 KB
/
block_access.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/**
* @file
* Main module functionality.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\RevisionLogInterface;
/**
* Implements hook_entity_access().
*/
function block_access_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($entity->getEntityTypeId() !== 'block_content') {
return AccessResult::neutral();
}
/** @var \Drupal\block_content\BlockContentInterface $entity */
$any = sprintf('%s any %s block_content', $operation, $entity->bundle());
$own = sprintf('%s own %s block_content', $operation, $entity->bundle());
return AccessResult::allowedIfHasPermission($account, $any)
->orIf(
AccessResult::allowedIfHasPermission($account, $own)
->andIf(AccessResult::allowedIf($entity instanceof RevisionLogInterface && $entity->getRevisionUser() && $entity->getRevisionUser()->id() === $account->id()))
->addCacheableDependency($entity)
);
}
/**
* Implements hook_entity_create_access().
*/
function block_access_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {
if ($context['entity_type_id'] !== 'block_content') {
return AccessResult::neutral();
}
return AccessResult::allowedIfHasPermission($account, sprintf('create %s block_content', $entity_bundle));
}