-
Notifications
You must be signed in to change notification settings - Fork 0
/
og.api.php
59 lines (50 loc) · 1.91 KB
/
og.api.php
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* @file
* Hooks provided by the Organic Groups module.
*/
declare(strict_types = 1);
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\og\OgAccess;
/**
* @addtogroup hooks
* @{
*/
/**
* Allows modules to alter group level permissions.
*
* @param array $permissions
* The list of group level permissions, passed by reference.
* @param \Drupal\Core\Cache\CacheableMetadata $cacheable_metadata
* The cache metadata.
* @param array $context
* An associative array containing contextual information, with keys:
* - 'permission': The group level permission being checked, as a string.
* - 'group': The group entity on which the permission applies.
* - 'user': The user account for which access is being determined.
*/
function hook_og_user_access_alter(array &$permissions, CacheableMetadata $cacheable_metadata, array $context): void {
// This example implements a use case where a custom module allows site
// builders to toggle a configuration setting that will prevent groups to be
// deleted if they are published.
// Retrieve the module configuration.
$config = \Drupal::config('mymodule.settings');
// Check if the site is configured to allow deletion of published groups.
$published_groups_can_be_deleted = $config->get('delete_published_groups');
// If deletion is not allowed and the group is published, revoke the
// permission.
$group = $context['group'];
if ($group instanceof EntityPublishedInterface && !$group->isPublished() && !$published_groups_can_be_deleted) {
$key = array_search(OgAccess::DELETE_GROUP_PERMISSION, $permissions);
if ($key !== FALSE) {
unset($permissions[$key]);
}
}
// Since our access result depends on our custom module configuration, we need
// to add it to the cache metadata.
$cacheable_metadata->addCacheableDependency($config);
}
/**
* @} End of "addtogroup hooks".
*/