diff --git a/src/GroupManager.php b/src/GroupManager.php index 1ec7386cd..10fc43f29 100644 --- a/src/GroupManager.php +++ b/src/GroupManager.php @@ -8,11 +8,13 @@ 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; use Drupal\og\Event\PermissionEvent; use Drupal\og\Event\PermissionEventInterface; +use Drupal\og\Exception\OgRoleException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -282,17 +284,31 @@ 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. - * @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. + * + * @see \Drupal\og\GroupManager::createPerBundleRoles() + * @see \Drupal\og\GroupManager::createPerGroupRoles() */ - 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; @@ -309,6 +325,31 @@ protected function createRoles($entity_type_id, $bundle_id) { } } + + /** + * 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. * 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', ];