Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Group content entity operations ~~hook~~ event #684

Merged
merged 11 commits into from
Aug 12, 2020
Prev Previous commit
Next Next commit
Document access hooks with examples.
  • Loading branch information
pfrenssen committed Aug 7, 2020
commit a02029ad340d0c44f6e039587116cf8d6c6f3015
102 changes: 102 additions & 0 deletions og.api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/**
* @file
* Hooks provided by the Organic Groups module.
*/

declare(strict_types = 1);

use Drupal\Core\Access\AccessResultInterface;
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
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, passed by reference.
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
* @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 {
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
// 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 = \Drupal::config('mymodule')->get('delete_published_groups');
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved

// 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);
}

/**
* Allows to alter access to entity operations performed on group content.
*
* @param \Drupal\Core\Access\AccessResultInterface $access_result
* The access result being altered.
* @param \Drupal\Core\Cache\CacheableMetadata $cacheable_metadata
* The cache metadata, passed by reference.
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
* @param array $context
* An associative array containing contextual information, with keys:
* - 'operation': The entity operation being performed on the group content.
* - 'group': The group entity to which the group content belongs.
* - 'group_content': The group content entity upon which the operation is
* performed.
* - 'user': The user account for which access is being determined.
*/
function hook_og_user_access_entity_operation_alter(AccessResultInterface &$access_result, CacheableMetadata &$cacheable_metadata, array $context): void {
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
// This example implements a use case where a custom module allows site
// builders to toggle a configuration setting that will allow users with the
// site wide 'edit and delete comments in all groups' permission to edit and
// delete all comments in all groups, even if they are not a group member.
/** @var \Drupal\Core\Session\AccountProxyInterface $user */
$user = $context['user'];
$group_content = $context['group_content'];

// Retrieve the module configuration.
$config = \Drupal::config('mymodule.settings');

// If comment moderation is allowed and the user has the permission, grant
// access to the 'update' and 'delete' operations on comment entities.
$is_comment = $group_content->getEntityTypeId() === 'comment';
$user_can_moderate_comments = $user->hasPermission('edit and delete comments in all groups');
$comment_moderation_is_enabled = \Drupal::config('mymodule')->get('comment_moderation_enabled');
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved

if ($is_comment && $user_can_moderate_comments && $comment_moderation_is_enabled) {
$access_result = new AccessResultAllowed();
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
}

// 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".
*/