From b63f027db84aa4bf5db79c8ca6dc837f87628d32 Mon Sep 17 00:00:00 2001 From: Amitaibu Date: Sun, 22 May 2016 14:36:53 +0300 Subject: [PATCH 1/5] Rename OG field to RolesOverride --- .../{AccessField.php => RolesOverride.php} | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) rename src/Plugin/OgFields/{AccessField.php => RolesOverride.php} (55%) diff --git a/src/Plugin/OgFields/AccessField.php b/src/Plugin/OgFields/RolesOverride.php similarity index 55% rename from src/Plugin/OgFields/AccessField.php rename to src/Plugin/OgFields/RolesOverride.php index ce577eca7..ca1896698 100644 --- a/src/Plugin/OgFields/AccessField.php +++ b/src/Plugin/OgFields/RolesOverride.php @@ -1,9 +1,5 @@ FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - 'settings' => [ - 'allowed_values' => [ - 0 => 'Use default roles and permissions', - 1 => 'Override default roles and permissions', - ], - 'allowed_values_function' => '', - ], - 'type' => 'list_integer', + 'type' => 'boolean', ]; return parent::getFieldStorageBaseDefinition($values); @@ -46,11 +34,9 @@ public function getFieldStorageBaseDefinition(array $values = []) { */ public function getFieldBaseDefinition(array $values = []) { $values += [ - 'default_value' => [0 => ['value' => 0]], - 'description' => $this->t('Determine if group should use default roles and permissions.'), - 'display_label' => TRUE, - 'label' => $this->t('Group roles and permissions'), - 'required' => TRUE, + 'description' => $this->t('Determine if group should override the default roles and permissions.'), + 'label' => $this->t('Group roles'), + 'required' => FALSE, ]; return parent::getFieldBaseDefinition($values); @@ -61,8 +47,7 @@ public function getFieldBaseDefinition(array $values = []) { */ public function getFormDisplayDefinition(array $values = []) { $values += [ - 'type' => 'options_select', - 'settings' => [], + 'type' => 'boolean_checkbox', ]; @@ -74,7 +59,7 @@ public function getFormDisplayDefinition(array $values = []) { */ public function getViewDisplayDefinition(array $values = []) { $values += [ - 'type' => 'list_default', + 'type' => 'boolean', 'label' => 'above', ]; From 699a3a5bbee7292a472b8088df8913cde4ba8a2c Mon Sep 17 00:00:00 2001 From: Amitaibu Date: Sun, 22 May 2016 14:48:45 +0300 Subject: [PATCH 2/5] createRoles should accept also group IDs --- src/GroupManager.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/GroupManager.php b/src/GroupManager.php index 1ec7386cd..83982bceb 100644 --- a/src/GroupManager.php +++ b/src/GroupManager.php @@ -13,6 +13,7 @@ use Drupal\og\Entity\OgRole; use Drupal\og\Event\PermissionEvent; use Drupal\og\Event\PermissionEventInterface; +use Drupal\og\Exception\OgRoleException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -286,13 +287,24 @@ public function refresh() { * * @param string $entity_type_id * The entity type ID of the group for which to create default roles. - * @param string $bundle_id - * The bundle ID of the group for which to create default roles. + * @param string|null $bundle_id + * (Optional) The bundle ID of the group for which to create default roles. + * When this property is set it means the roles will be "global" roles + * that apply to any group of the bundle ID. Defaults to NULL. + * @param string|null $group_id + * (Optional) The group ID of the group for which to create default roles. + * When this property is set it means the roles will be "per group" roles + * and apply only to this specific group ID. Defaults to NULL. */ - protected function createRoles($entity_type_id, $bundle_id) { + protected function createRoles($entity_type_id, $bundle_id = NULL, $group_id = NULL) { + if (!$bundle_id && !$group_id) { + throw new OgRoleException('Cannot create OG roles without either bundle ID or the group ID being set.'); + } + $properties = [ 'group_type' => $entity_type_id, 'group_bundle' => $bundle_id, + 'group_id' => $group_id, ]; foreach ([OgRoleInterface::ANONYMOUS, OgRoleInterface::AUTHENTICATED, OgRoleInterface::ADMINISTRATOR] as $role_name) { $properties['id'] = $role_name; From 1b4272a9f4f86e2964637fd6a5cec7c4f8b82048 Mon Sep 17 00:00:00 2001 From: Amitaibu Date: Sun, 22 May 2016 14:50:10 +0300 Subject: [PATCH 3/5] Revert "createRoles should accept also group IDs" This reverts commit 699a3a5bbee7292a472b8088df8913cde4ba8a2c. --- src/GroupManager.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/GroupManager.php b/src/GroupManager.php index 83982bceb..1ec7386cd 100644 --- a/src/GroupManager.php +++ b/src/GroupManager.php @@ -13,7 +13,6 @@ use Drupal\og\Entity\OgRole; use Drupal\og\Event\PermissionEvent; use Drupal\og\Event\PermissionEventInterface; -use Drupal\og\Exception\OgRoleException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -287,24 +286,13 @@ public function refresh() { * * @param string $entity_type_id * The entity type ID of the group for which to create default roles. - * @param string|null $bundle_id - * (Optional) The bundle ID of the group for which to create default roles. - * When this property is set it means the roles will be "global" roles - * that apply to any group of the bundle ID. Defaults to NULL. - * @param string|null $group_id - * (Optional) The group ID of the group for which to create default roles. - * When this property is set it means the roles will be "per group" roles - * and apply only to this specific group ID. Defaults to NULL. + * @param string $bundle_id + * The bundle ID of the group for which to create default roles. */ - protected function createRoles($entity_type_id, $bundle_id = NULL, $group_id = NULL) { - if (!$bundle_id && !$group_id) { - throw new OgRoleException('Cannot create OG roles without either bundle ID or the group ID being set.'); - } - + protected function createRoles($entity_type_id, $bundle_id) { $properties = [ 'group_type' => $entity_type_id, 'group_bundle' => $bundle_id, - 'group_id' => $group_id, ]; foreach ([OgRoleInterface::ANONYMOUS, OgRoleInterface::AUTHENTICATED, OgRoleInterface::ADMINISTRATOR] as $role_name) { $properties['id'] = $role_name; From e70626a79a084273b4c0cfa30b8100fceffb1011 Mon Sep 17 00:00:00 2001 From: Amitaibu Date: Sun, 22 May 2016 14:52:25 +0300 Subject: [PATCH 4/5] Revert "Revert "createRoles should accept also group IDs"" This reverts commit 1b4272a9f4f86e2964637fd6a5cec7c4f8b82048. --- src/GroupManager.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/GroupManager.php b/src/GroupManager.php index 1ec7386cd..83982bceb 100644 --- a/src/GroupManager.php +++ b/src/GroupManager.php @@ -13,6 +13,7 @@ use Drupal\og\Entity\OgRole; use Drupal\og\Event\PermissionEvent; use Drupal\og\Event\PermissionEventInterface; +use Drupal\og\Exception\OgRoleException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -286,13 +287,24 @@ public function refresh() { * * @param string $entity_type_id * The entity type ID of the group for which to create default roles. - * @param string $bundle_id - * The bundle ID of the group for which to create default roles. + * @param string|null $bundle_id + * (Optional) The bundle ID of the group for which to create default roles. + * When this property is set it means the roles will be "global" roles + * that apply to any group of the bundle ID. Defaults to NULL. + * @param string|null $group_id + * (Optional) The group ID of the group for which to create default roles. + * When this property is set it means the roles will be "per group" roles + * and apply only to this specific group ID. Defaults to NULL. */ - protected function createRoles($entity_type_id, $bundle_id) { + protected function createRoles($entity_type_id, $bundle_id = NULL, $group_id = NULL) { + if (!$bundle_id && !$group_id) { + throw new OgRoleException('Cannot create OG roles without either bundle ID or the group ID being set.'); + } + $properties = [ 'group_type' => $entity_type_id, 'group_bundle' => $bundle_id, + 'group_id' => $group_id, ]; foreach ([OgRoleInterface::ANONYMOUS, OgRoleInterface::AUTHENTICATED, OgRoleInterface::ADMINISTRATOR] as $role_name) { $properties['id'] = $role_name; From 96f6ff21f9c35df1b98440f57e159de2e95b3389 Mon Sep 17 00:00:00 2001 From: Amitaibu Date: Sun, 22 May 2016 14:57:07 +0300 Subject: [PATCH 5/5] Add perGroup and perBundle create role methods --- src/GroupManager.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/GroupManager.php b/src/GroupManager.php index 83982bceb..10fc43f29 100644 --- a/src/GroupManager.php +++ b/src/GroupManager.php @@ -8,6 +8,7 @@ namespace Drupal\og; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\og\Entity\OgRole; @@ -283,7 +284,7 @@ public function refresh() { } /** - * Creates default roles for the given group type. + * Creates per bundle or per group OG roles. * * @param string $entity_type_id * The entity type ID of the group for which to create default roles. @@ -295,6 +296,9 @@ public function refresh() { * (Optional) The group ID of the group for which to create default roles. * When this property is set it means the roles will be "per group" roles * and apply only to this specific group ID. Defaults to NULL. + * + * @see \Drupal\og\GroupManager::createPerBundleRoles() + * @see \Drupal\og\GroupManager::createPerGroupRoles() */ protected function createRoles($entity_type_id, $bundle_id = NULL, $group_id = NULL) { if (!$bundle_id && !$group_id) { @@ -321,6 +325,31 @@ protected function createRoles($entity_type_id, $bundle_id = NULL, $group_id = N } } + + /** + * Creates per bundle OG roles. + * + * @param string $entity_type_id + * The entity type ID of the group for which to create default roles. + * @param string $bundle_id + * The bundle ID of the group for which to create default roles. + */ + protected function createPerBundleRoles($entity_type_id, $bundle_id) { + $this->createRoles($entity_type_id, $bundle_id); + } + + + /** + * Creates per group OG roles. + * + * @param Drupal\Core\Entity\EntityInterface $group + * The group entity. + */ + protected function createPerGroupRoles(EntityInterface $group) { + $entity_type_id = $group->getEntityTypeId(); + $this->createRoles($entity_type_id, NULL, $group->id()); + } + /** * Deletes the roles associated with a group type. *