From aef42134961455b14b8d52ac9550ee5af48be116 Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Thu, 13 Oct 2022 16:27:11 +0530 Subject: [PATCH 1/8] Pass membership data in form_state for roles add/remove form. --- src/Form/OgChangeMultipleRolesFormBase.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Form/OgChangeMultipleRolesFormBase.php b/src/Form/OgChangeMultipleRolesFormBase.php index c63d6fb89..8c341e69b 100644 --- a/src/Form/OgChangeMultipleRolesFormBase.php +++ b/src/Form/OgChangeMultipleRolesFormBase.php @@ -93,6 +93,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => $this->t('Submit'), ]; + $form_state->setTemporaryValue('memberships', $this->getMemberships()); + return $form; } From 8083db910a486b0efec45c0a516d8be0203b1970 Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Thu, 13 Oct 2022 17:46:23 +0530 Subject: [PATCH 2/8] Pass group and membership data in form_state for roles add/remove form. --- src/Form/OgChangeMultipleRolesFormBase.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Form/OgChangeMultipleRolesFormBase.php b/src/Form/OgChangeMultipleRolesFormBase.php index 8c341e69b..2f4f4476d 100644 --- a/src/Form/OgChangeMultipleRolesFormBase.php +++ b/src/Form/OgChangeMultipleRolesFormBase.php @@ -93,7 +93,16 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => $this->t('Submit'), ]; - $form_state->setTemporaryValue('memberships', $this->getMemberships()); + // Set selected memberships & group in form state. This can be used by + // developers to alter the roles form. + $memberships = $this->getMemberships(); + if (empty($memberships)) { + return $form; + } + $membership = current($memberships); + $group = $membership->getGroup(); + $form_state->setTemporaryValue('selected_memberships', $memberships); + $form_state->setTemporaryValue('current_group', $group); return $form; } From 0d50c5216fef61bdc8848ebb11a0b4bde2342739 Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Mon, 25 Sep 2023 18:00:36 +0530 Subject: [PATCH 3/8] Add examples. --- og.api.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/og.api.php b/og.api.php index 203da84a0..803104ed9 100644 --- a/og.api.php +++ b/og.api.php @@ -9,6 +9,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\EntityPublishedInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\og\OgAccess; /** @@ -54,6 +55,34 @@ function hook_og_user_access_alter(array &$permissions, CacheableMetadata $cache $cacheable_metadata->addCacheableDependency($config); } +/** + * Implements hook_form_FORM_ID_alter(). + */ +function hook_form_og_membership_remove_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { + // Get access to current group and selected memberships when on role remove + // form. + $memberships = $form_state->getTemporaryValue('selected_memberships'); + $group = $form_state->getTemporaryValue('current_group'); + + $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships, $group) { + // Code to filter out options based on data from memberships and group. + }, ARRAY_FILTER_USE_KEY); +} + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function hook_form_og_membership_add_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { + // Get access to current group and selected memberships when on role add + // form. + $memberships = $form_state->getTemporaryValue('selected_memberships'); + $group = $form_state->getTemporaryValue('current_group'); + + $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships, $group) { + // Code to filter out options based on data from memberships and group. + }, ARRAY_FILTER_USE_KEY); +} + /** * @} End of "addtogroup hooks". */ From ae4ef6f63589dec9a3c80fb3e3b64d1830b443ce Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Tue, 26 Sep 2023 09:23:08 +0530 Subject: [PATCH 4/8] Update og.api.php Co-authored-by: Amitai Burstein --- og.api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/og.api.php b/og.api.php index 803104ed9..e27d75a6a 100644 --- a/og.api.php +++ b/og.api.php @@ -59,7 +59,7 @@ function hook_og_user_access_alter(array &$permissions, CacheableMetadata $cache * Implements hook_form_FORM_ID_alter(). */ function hook_form_og_membership_remove_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { - // Get access to current group and selected memberships when on role remove + // Get access to current group and selected memberships when we're on role remove // form. $memberships = $form_state->getTemporaryValue('selected_memberships'); $group = $form_state->getTemporaryValue('current_group'); From 927a0efab8e6583383fe4946111415139e0dd2f0 Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Tue, 26 Sep 2023 09:23:18 +0530 Subject: [PATCH 5/8] Update src/Form/OgChangeMultipleRolesFormBase.php Co-authored-by: Amitai Burstein --- src/Form/OgChangeMultipleRolesFormBase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form/OgChangeMultipleRolesFormBase.php b/src/Form/OgChangeMultipleRolesFormBase.php index 2f4f4476d..7d2d68fca 100644 --- a/src/Form/OgChangeMultipleRolesFormBase.php +++ b/src/Form/OgChangeMultipleRolesFormBase.php @@ -93,8 +93,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => $this->t('Submit'), ]; - // Set selected memberships & group in form state. This can be used by - // developers to alter the roles form. + // Set selected memberships in the form state. This can be used by + // to alter the roles form. $memberships = $this->getMemberships(); if (empty($memberships)) { return $form; From 551b3923f10dc13316e1637f781196770ca82724 Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Tue, 26 Sep 2023 09:25:22 +0530 Subject: [PATCH 6/8] Pass membership data in form_state for roles add/remove form. --- og.api.php | 14 ++++++-------- src/Form/OgChangeMultipleRolesFormBase.php | 4 +--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/og.api.php b/og.api.php index e27d75a6a..bc9b73902 100644 --- a/og.api.php +++ b/og.api.php @@ -59,12 +59,11 @@ function hook_og_user_access_alter(array &$permissions, CacheableMetadata $cache * Implements hook_form_FORM_ID_alter(). */ function hook_form_og_membership_remove_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { - // Get access to current group and selected memberships when we're on role remove - // form. + // Get access to current group and selected memberships when we're on the role + // remove form. $memberships = $form_state->getTemporaryValue('selected_memberships'); - $group = $form_state->getTemporaryValue('current_group'); - $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships, $group) { + $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships) { // Code to filter out options based on data from memberships and group. }, ARRAY_FILTER_USE_KEY); } @@ -73,12 +72,11 @@ function hook_form_og_membership_remove_multiple_roles_action_form_alter(array & * Implements hook_form_FORM_ID_alter(). */ function hook_form_og_membership_add_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { - // Get access to current group and selected memberships when on role add - // form. + // Get access to current group and selected memberships when we're on the role + // add form. $memberships = $form_state->getTemporaryValue('selected_memberships'); - $group = $form_state->getTemporaryValue('current_group'); - $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships, $group) { + $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships) { // Code to filter out options based on data from memberships and group. }, ARRAY_FILTER_USE_KEY); } diff --git a/src/Form/OgChangeMultipleRolesFormBase.php b/src/Form/OgChangeMultipleRolesFormBase.php index 7d2d68fca..04650ccb1 100644 --- a/src/Form/OgChangeMultipleRolesFormBase.php +++ b/src/Form/OgChangeMultipleRolesFormBase.php @@ -99,10 +99,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { if (empty($memberships)) { return $form; } - $membership = current($memberships); - $group = $membership->getGroup(); + $form_state->setTemporaryValue('selected_memberships', $memberships); - $form_state->setTemporaryValue('current_group', $group); return $form; } From 008abf0378f3a7eb5211e3573ed46d4245e132fb Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Tue, 26 Sep 2023 09:26:45 +0530 Subject: [PATCH 7/8] Update comments. --- src/Form/OgChangeMultipleRolesFormBase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form/OgChangeMultipleRolesFormBase.php b/src/Form/OgChangeMultipleRolesFormBase.php index 04650ccb1..fd403f0da 100644 --- a/src/Form/OgChangeMultipleRolesFormBase.php +++ b/src/Form/OgChangeMultipleRolesFormBase.php @@ -93,8 +93,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => $this->t('Submit'), ]; - // Set selected memberships in the form state. This can be used by - // to alter the roles form. + // Set selected memberships in the form state. This can be used to alter + // the roles add/remove form. $memberships = $this->getMemberships(); if (empty($memberships)) { return $form; From 92a57ab7350d57165f2186b81645fbd30ea83d7f Mon Sep 17 00:00:00 2001 From: Dipak Yadav Date: Tue, 26 Sep 2023 12:58:16 +0530 Subject: [PATCH 8/8] PHPCS fixes. --- og.api.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/og.api.php b/og.api.php index bc9b73902..2536b0d95 100644 --- a/og.api.php +++ b/og.api.php @@ -61,10 +61,16 @@ function hook_og_user_access_alter(array &$permissions, CacheableMetadata $cache function hook_form_og_membership_remove_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { // Get access to current group and selected memberships when we're on the role // remove form. + /** @var \Drupal\og\OgMembershipInterface[] $memberships */ $memberships = $form_state->getTemporaryValue('selected_memberships'); - $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships) { + $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use (&$form, $memberships) { // Code to filter out options based on data from memberships and group. + $membership = reset($memberships); + $group = $membership->getGroup(); + if ($group->id() != 9999) { + unset($form['roles']['#options'][$key]); + } }, ARRAY_FILTER_USE_KEY); } @@ -74,10 +80,16 @@ function hook_form_og_membership_remove_multiple_roles_action_form_alter(array & function hook_form_og_membership_add_multiple_roles_action_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { // Get access to current group and selected memberships when we're on the role // add form. + /** @var \Drupal\og\OgMembershipInterface[] $memberships */ $memberships = $form_state->getTemporaryValue('selected_memberships'); - $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use($memberships) { + $form['roles']['#options'] = array_filter($form['roles']['#options'], function ($key) use (&$form, $memberships) { // Code to filter out options based on data from memberships and group. + $membership = reset($memberships); + $group = $membership->getGroup(); + if ($group->id() != 9999) { + unset($form['roles']['#options'][$key]); + } }, ARRAY_FILTER_USE_KEY); }