From 7a9386f7f3211790782ee98aee67166ef1909d91 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 5 Jan 2024 12:10:17 +0100 Subject: [PATCH] feat: Remove global editors from all groups Refs: RWR-400 --- .../hr_paragraphs/hr_paragraphs.install | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/html/modules/custom/hr_paragraphs/hr_paragraphs.install b/html/modules/custom/hr_paragraphs/hr_paragraphs.install index 6179bccd..f518f720 100644 --- a/html/modules/custom/hr_paragraphs/hr_paragraphs.install +++ b/html/modules/custom/hr_paragraphs/hr_paragraphs.install @@ -7,6 +7,7 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; +use Drupal\user\Entity\User; /** * Change content type to page. @@ -227,3 +228,39 @@ function hr_paragraphs_update_9016() { ->installFieldStorageDefinition($field_name, 'linkcheckerlink', 'linkcheckerlink', $storage_definition); } } + +/** + * Remove admins and global editors from groups. + */ +function hr_paragraphs_update_9017() { + $roles = [ + 'administrator', + 'global_editor', + ]; + + $query = \Drupal::entityQuery('user') + ->accessCheck(FALSE) + ->condition('roles', $roles, 'IN'); + + $uids = $query->execute(); + + // Add admin role. + $uids[1] = "1"; + + if (!empty($uids)) { + /** @var \Drupal\group\GroupMembershipLoaderInterface */ + $group_membership_service = \Drupal::service('group.membership_loader'); + + foreach ($uids as $uid) { + /** @var \Drupal\user\Entity\User */ + $user = User::load($uid); + + $memberships = $group_membership_service->loadByUser($user); + if (!empty($memberships)) { + foreach ($memberships as $membership) { + $membership->getGroup()->removeMember($user); + } + } + } + } +}