group_member_mgmt-8.x-1.0-beta7/group_member_mgmt.module
group_member_mgmt.module
<?php
/**
* @file
* Group member management module code.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group_member_mgmt\GroupMembership\GroupMembershipAccessControlHandler;
use Drupal\group_member_mgmt\GroupMembership\GroupMembershipPermissionProvider;
/**
* Implements hook_group_content_info_alter().
*/
function group_member_mgmt_group_content_info_alter(&$info) {
// Alter the content enabler plugin definitions to use our class.
$info['group_membership']['handlers']['access'] = GroupMembershipAccessControlHandler::class;
$info['group_membership']['handlers']['permission_provider'] = GroupMembershipPermissionProvider::class;
}
/**
* Implements hook_module_implements_alter().
*/
function group_member_mgmt_module_implements_alter(&$implementations, $hook) {
// Use this modules implementation of hook_entity_field_access instead of the
// one from the group module.
if ($hook == 'entity_field_access') {
if (isset($implementations['group'])) {
$implementations['group_member_mgmt'] = $implementations['group'];
unset($implementations['group']);
}
}
}
/**
* Implements hook_entity_field_access().
*/
function group_member_mgmt_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Can't retrieve an entity from an empty item list.
if (!isset($items)) {
return AccessResult::neutral();
}
// Group roles field on group content entities.
if (_group_member_mgmt_entity_field_access_group_roles($operation, $field_definition, $account, $items)) {
return AccessResult::allowed();
}
// Fall back to group module's implementation.
return group_entity_field_access($operation, $field_definition, $account, $items);
}
/**
* Field access logic for group roles field.
*
* @param string $operation
* The operation to be performed. See
* \Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess()
* for possible values.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param \Drupal\Core\Session\AccountInterface $account
* The user account to check.
* @param \Drupal\Core\Field\FieldItemListInterface $items
* (optional) The entity field object for which to check access, or NULL if
* access is checked for the field definition, without any specific value
* available. Defaults to NULL.
*
* @return bool
* Indicates if access should be allowed.
*
* @see hook_entity_field_access()
*/
function _group_member_mgmt_entity_field_access_group_roles($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items) {
if ($operation != 'edit') {
return FALSE;
}
/** @var \Drupal\group\Entity\GroupContentInterface $entity */
$entity = $items->getEntity();
if ($entity->getEntityTypeId() != 'group_content') {
return FALSE;
}
if ($entity->getContentPlugin()->getPluginId() != 'group_membership') {
return FALSE;
}
if ($field_definition->getName() != 'group_roles') {
return FALSE;
}
// Must have a group set.
if (!$group = $entity->getGroup()) {
return FALSE;
}
return $items->getEntity()->access('update', $account, FALSE);
}
/**
* Implements hook_form_alter().
*/
function group_member_mgmt_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Implement the same alterations as group module.
// See group_member_mgmt_form_alter() for the applicable group permissions.
if ($form_id == 'user_register_form' && $form_state->has('group')) {
$account = \Drupal::currentUser()->getAccount();
/** @var Drupal\group\Entity\GroupInterface $group */
$group = $form_state->get('group');
$perm = 'create group_membership content group-role:' . $group->getGroupType()->id() . '-member';
if ($group->hasPermission($perm, $account)) {
$form['account']['pass'] = [
'#type' => 'password_confirm',
'#size' => 25,
'#description' => t('Provide a password for the new account in both fields.'),
'#required' => TRUE,
];
$form['account']['notify']['#access'] = TRUE;
$form['#attributes']['data-user-info-from-browser'] = FALSE;
$form['administer_users']['#value'] = TRUE;
if (\Drupal::moduleHandler()->moduleExists('autologout')) {
if (($key = array_search('autologout_user_profile_submit', $form['actions']['submit']['#submit'])) !== FALSE) {
unset($form['actions']['submit']['#submit'][$key]);
}
}
}
}
}
