class OgRole in Organic groups 8
Defines the OG user role entity class.
Plugin annotation
@ConfigEntityType(
id = "og_role",
label = @Translation("OG role"),
static_cache = TRUE,
entity_keys = {
"id" = "id",
"label" = "label",
"weight" = "weight"
},
config_export = {
"id",
"label",
"weight",
"is_admin",
"group_type",
"group_bundle",
"group_id",
"permissions",
"role_type"
}
)
Hierarchy
- class \Drupal\Core\Entity\EntityBase implements EntityInterface uses RefinableCacheableDependencyTrait, DependencySerializationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityBase implements ConfigEntityInterface uses SynchronizableEntityTrait, PluginDependencyTrait
- class \Drupal\user\Entity\Role implements RoleInterface
- class \Drupal\og\Entity\OgRole implements OgRoleInterface
- class \Drupal\user\Entity\Role implements RoleInterface
- class \Drupal\Core\Config\Entity\ConfigEntityBase implements ConfigEntityInterface uses SynchronizableEntityTrait, PluginDependencyTrait
Expanded class hierarchy of OgRole
See also
33 files declare their use of OgRole
- AccessByOgMembershipTest.php in tests/
src/ Kernel/ Access/ AccessByOgMembershipTest.php - ActionTestBase.php in tests/
src/ Kernel/ Action/ ActionTestBase.php - AddSingleOgMembershipRole.php in src/
Plugin/ Action/ AddSingleOgMembershipRole.php - AddSingleOgMembershipRoleActionTest.php in tests/
src/ Kernel/ Action/ AddSingleOgMembershipRoleActionTest.php - ChangeSingleOgMembershipRoleBase.php in src/
Plugin/ Action/ ChangeSingleOgMembershipRoleBase.php
File
- src/
Entity/ OgRole.php, line 40
Namespace
Drupal\og\EntityView source
class OgRole extends Role implements OgRoleInterface {
/**
* The role name.
*
* @var string
*/
protected $name;
/**
* Whether or not the parent entity we depend on is being removed.
*
* @var bool
* TRUE if the entity is being removed.
*/
protected $parentEntityIsBeingRemoved = FALSE;
/**
* Constructs an OgRole object.
*
* @param array $values
* An array of values to set, keyed by property name.
*/
public function __construct(array $values) {
parent::__construct($values, 'og_role');
}
/**
* {@inheritdoc}
*/
public function setId($id) {
$this
->set('id', $id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this
->get('label');
}
/**
* {@inheritdoc}
*/
public function setLabel($label) {
$this
->set('label', $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function getGroupType() {
return $this
->get('group_type');
}
/**
* {@inheritdoc}
*/
public function setGroupType($group_type) {
$this
->set('group_type', $group_type);
return $this;
}
/**
* {@inheritdoc}
*/
public function getGroupBundle() {
return $this
->get('group_bundle');
}
/**
* {@inheritdoc}
*/
public function setGroupBundle($group_bundle) {
$this
->set('group_bundle', $group_bundle);
return $this;
}
/**
* {@inheritdoc}
*/
public function getRoleType() {
return $this
->get('role_type') ?: OgRoleInterface::ROLE_TYPE_STANDARD;
}
/**
* {@inheritdoc}
*/
public function setRoleType($role_type) {
if (!in_array($role_type, [
self::ROLE_TYPE_REQUIRED,
self::ROLE_TYPE_STANDARD,
])) {
throw new \InvalidArgumentException("'{$role_type}' is not a valid role type.");
}
return $this
->set('role_type', $role_type);
}
/**
* {@inheritdoc}
*/
public function getName() {
// If the name is not set yet, try to derive it from the ID.
if (empty($this->name) && $this
->id() && $this
->getGroupType() && $this
->getGroupBundle()) {
// Check if the ID matches the pattern '{entity type}-{bundle}-{name}'.
$pattern = preg_quote("{$this->getGroupType()}-{$this->getGroupBundle()}-");
preg_match("/{$pattern}(.+)/", $this
->id(), $matches);
if (!empty($matches[1])) {
$this
->setName($matches[1]);
}
}
return $this
->get('name');
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* {@inheritdoc}
*/
public static function loadByGroupAndName(EntityInterface $group, $name) {
$role_id = "{$group->getEntityTypeId()}-{$group->bundle()}-{$name}";
return self::load($role_id);
}
/**
* {@inheritdoc}
*/
public static function loadByGroupType($group_entity_type_id, $group_bundle_id) {
$properties = [
'group_type' => $group_entity_type_id,
'group_bundle' => $group_bundle_id,
];
return \Drupal::entityTypeManager()
->getStorage('og_role')
->loadByProperties($properties);
}
/**
* {@inheritdoc}
*/
public function save() {
// The ID of a new OgRole has to consist of the entity type ID, bundle ID
// and role name, separated by dashes.
if ($this
->isNew() && $this
->id()) {
$pattern = preg_quote("{$this->getGroupType()}-{$this->getGroupBundle()}-{$this->getName()}");
if (!preg_match("/{$pattern}/", $this
->id())) {
throw new ConfigValueException('The ID should consist of the group entity type ID, group bundle ID and role name, separated by dashes.');
}
}
// If a new OgRole is saved and the ID is not set, construct the ID from
// the entity type ID, bundle ID and role name.
if ($this
->isNew() && !$this
->id()) {
if (!$this
->getGroupType()) {
throw new ConfigValueException('The group type can not be empty.');
}
if (!$this
->getGroupBundle()) {
throw new ConfigValueException('The group bundle can not be empty.');
}
if (!$this
->getName()) {
throw new ConfigValueException('The role name can not be empty.');
}
// When assigning a role to group we need to add a prefix to the ID in
// order to prevent duplicate IDs.
$prefix = $this
->getGroupType() . '-' . $this
->getGroupBundle() . '-';
$this
->setId($prefix . $this
->getName());
}
parent::save();
}
/**
* {@inheritdoc}
*/
public function set($property_name, $value) {
// Prevent the ID, role type, group ID, group entity type or bundle from
// being changed once they are set. These properties are required and
// shouldn't be tampered with.
$is_locked_property = in_array($property_name, [
'id',
'role_type',
'group_id',
'group_type',
'group_bundle',
]);
if (!$is_locked_property || $this
->isNew()) {
return parent::set($property_name, $value);
}
if ($this
->get($property_name) == $value) {
// Locked property hasn't changed, so we can return early.
return $this;
}
throw new OgRoleException("The {$property_name} cannot be changed.");
}
/**
* {@inheritdoc}
*/
public function delete() {
// The default roles are required. Prevent them from being deleted for as
// long as the group still exists, unless the group itself is in the process
// of being removed.
if (!$this->parentEntityIsBeingRemoved && $this
->isRequired() && $this
->groupTypeManager()
->isGroup($this
->getGroupType(), $this
->getGroupBundle())) {
throw new OgRoleException('The default roles "non-member" and "member" cannot be deleted.');
}
parent::delete();
}
/**
* {@inheritdoc}
*/
public function isRequired() {
return static::getRoleTypeByName($this
->getName()) === OgRoleInterface::ROLE_TYPE_REQUIRED;
}
/**
* Maps role names to role types.
*
* The 'anonymous' and 'authenticated' roles should not be changed or deleted.
* All others are standard roles.
*
* @param string $role_name
* The role name for which to return the type.
*
* @return string
* The role type, either OgRoleInterface::ROLE_TYPE_REQUIRED or
* OgRoleInterface::ROLE_TYPE_STANDARD.
*/
public static function getRoleTypeByName($role_name) {
return in_array($role_name, [
OgRoleInterface::ANONYMOUS,
OgRoleInterface::AUTHENTICATED,
]) ? OgRoleInterface::ROLE_TYPE_REQUIRED : OgRoleInterface::ROLE_TYPE_STANDARD;
}
/**
* {@inheritdoc}
*/
public static function getRole($entity_type_id, $bundle, $role_name) {
return self::load($entity_type_id . '-' . $bundle . '-' . $role_name);
}
/**
* Gets the group manager.
*
* @return \Drupal\og\GroupTypeManagerInterface
* The group manager.
*/
protected function groupTypeManager() {
// Returning the group manager by calling the global factory method might
// seem less than ideal, but Entity classes are not designed to work with
// proper dependency injection. The ::create() method only accepts a $values
// array, which is not compatible with ContainerInjectionInterface.
// See for example Entity::uuidGenerator() in the base Entity class, it
// also uses this pattern.
return \Drupal::service('og.group_type_manager');
}
/**
* Gets the OG access service.
*
* @return \Drupal\og\OgAccessInterface
* The OG access service.
*/
protected function ogAccess() {
return \Drupal::service('og.access');
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
parent::calculateDependencies();
// Create a dependency on the group bundle.
$bundle_config_dependency = \Drupal::entityTypeManager()
->getDefinition($this
->getGroupType())
->getBundleConfigDependency($this
->getGroupBundle());
$this
->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
}
/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
// The parent entity we depend on is being removed. Set a flag so we can
// allow removal of required roles.
$this->parentEntityIsBeingRemoved = TRUE;
return parent::onDependencyRemoval($dependencies);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
ConfigEntityBase:: |
private | property | Whether the config is being deleted by the uninstall process. | |
ConfigEntityBase:: |
protected | property | The language code of the entity's default language. | |
ConfigEntityBase:: |
protected | property | The original ID of the configuration entity. | |
ConfigEntityBase:: |
protected | property | The enabled/disabled status of the configuration entity. | 4 |
ConfigEntityBase:: |
protected | property | Third party entity settings. | |
ConfigEntityBase:: |
protected | property | Trust supplied data and not use configuration schema on save. | |
ConfigEntityBase:: |
protected | property | The UUID for this entity. | |
ConfigEntityBase:: |
protected | property | Information maintained by Drupal core about configuration. | |
ConfigEntityBase:: |
protected | function | Overrides \Drupal\Core\Entity\DependencyTrait:addDependency(). | |
ConfigEntityBase:: |
public | function |
Creates a duplicate of the entity. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Disables the configuration entity. Overrides ConfigEntityInterface:: |
1 |
ConfigEntityBase:: |
public | function |
Enables the configuration entity. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Returns the value of a property. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Returns the cache tags that should be used to invalidate caches. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Gets the configuration dependency name. Overrides EntityBase:: |
|
ConfigEntityBase:: |
protected static | function | Gets the configuration manager. | |
ConfigEntityBase:: |
public | function |
Gets the configuration target identifier for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the configuration dependencies. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the original ID. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
protected | function | Gets the typed config manager. | |
ConfigEntityBase:: |
public | function |
Gets whether on not the data is trusted. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
protected static | function |
Override to never invalidate the individual entities' cache tags; the
config system already invalidates them. Overrides EntityBase:: |
|
ConfigEntityBase:: |
protected | function |
Override to never invalidate the entity's cache tag; the config system
already invalidates it. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Checks whether this entity is installable. Overrides ConfigEntityInterface:: |
2 |
ConfigEntityBase:: |
public | function |
Overrides Entity::isNew(). Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Returns whether this entity is being changed during the uninstall process. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Deprecated way of generating a link to the entity. See toLink(). Overrides EntityBase:: |
|
ConfigEntityBase:: |
public static | function |
Acts on entities before they are deleted and before hooks are invoked. Overrides EntityBase:: |
8 |
ConfigEntityBase:: |
public | function |
Sets the original ID. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Sets the status of the configuration entity. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function | ||
ConfigEntityBase:: |
public static | function | Helper callback for uasort() to sort configuration entities by weight and label. | 6 |
ConfigEntityBase:: |
public | function |
Returns whether the configuration entity is enabled. Overrides ConfigEntityInterface:: |
4 |
ConfigEntityBase:: |
public | function |
Gets an array of all property values. Overrides EntityBase:: |
2 |
ConfigEntityBase:: |
public | function |
Gets the URL object for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Sets that the data should be trusted. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the public URL for this entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the URL object for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Overrides EntityBase:: |
4 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | Aliased as: traitSleep | 1 |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. Aliased as: addDependencyTrait | |
EntityBase:: |
protected | property | Boolean indicating whether the entity should be forced to be new. | |
EntityBase:: |
protected | property | The entity type. | |
EntityBase:: |
protected | property | A typed data object wrapping this entity. | |
EntityBase:: |
public | function |
Checks data value access. Overrides AccessibleInterface:: |
1 |
EntityBase:: |
public | function |
Gets the bundle of the entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
public static | function |
Constructs a new entity object, without permanently saving it. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Enforces an entity to be new. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | Gets the entity manager. | |
EntityBase:: |
protected | function | Gets the entity type bundle info service. | |
EntityBase:: |
protected | function | Gets the entity type manager. | |
EntityBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
Gets the key that is used to store configuration dependencies. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the entity type definition. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the ID of the type of the entity. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | The list cache tags to invalidate for this entity. | |
EntityBase:: |
public | function |
Gets a typed data object for this entity object. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Indicates if a link template exists for a given key. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the identifier. Overrides EntityInterface:: |
11 |
EntityBase:: |
public | function |
Gets the label of the entity. Overrides EntityInterface:: |
6 |
EntityBase:: |
public | function |
Gets the language of the entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
protected | function | Gets the language manager. | |
EntityBase:: |
protected | function | Gets an array link templates. | 1 |
EntityBase:: |
public static | function |
Loads an entity. Overrides EntityInterface:: |
|
EntityBase:: |
public static | function |
Loads one or more entities. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Acts on a created entity before hooks are invoked. Overrides EntityInterface:: |
4 |
EntityBase:: |
public static | function |
Acts on deleted entities before the delete hook is invoked. Overrides EntityInterface:: |
16 |
EntityBase:: |
public | function |
Acts on a saved entity before the insert or update hook is invoked. Overrides EntityInterface:: |
14 |
EntityBase:: |
public static | function |
Changes the values of an entity before it is created. Overrides EntityInterface:: |
5 |
EntityBase:: |
public | function |
Gets a list of entities referenced by this entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Generates the HTML for a link to this entity. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets a list of URI relationships supported by this entity. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | Gets an array of placeholders for this entity. | 2 |
EntityBase:: |
public | function |
Gets the entity UUID (Universally Unique Identifier). Overrides EntityInterface:: |
1 |
EntityBase:: |
protected | function | Gets the UUID generator. | |
OgRole:: |
protected | property | The role name. | |
OgRole:: |
protected | property | Whether or not the parent entity we depend on is being removed. | |
OgRole:: |
public | function |
Calculates dependencies and stores them in the dependency property. Overrides ConfigEntityBase:: |
|
OgRole:: |
public | function |
Deletes an entity permanently. Overrides EntityBase:: |
|
OgRole:: |
public | function |
Returns the group bundle. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Returns the group type. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Returns the label. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Returns the role name. Overrides OgRoleInterface:: |
|
OgRole:: |
public static | function |
Get a role by the group's bundle and role name. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Returns the role type. Overrides OgRoleInterface:: |
|
OgRole:: |
public static | function | Maps role names to role types. | |
OgRole:: |
protected | function | Gets the group manager. | |
OgRole:: |
public | function |
Returns if this is a default role which is required and cannot be deleted. Overrides OgRoleInterface:: |
|
OgRole:: |
public static | function |
Returns the role represented by the given group and role name. Overrides OgRoleInterface:: |
|
OgRole:: |
public static | function |
Returns the roles that are associated with the given group type and bundle. Overrides OgRoleInterface:: |
|
OgRole:: |
protected | function | Gets the OG access service. | |
OgRole:: |
public | function |
Informs the entity that entities it depends on will be deleted. Overrides ConfigEntityBase:: |
|
OgRole:: |
public | function |
Saves an entity permanently. Overrides ConfigEntityBase:: |
|
OgRole:: |
public | function |
Sets the value of a property. Overrides ConfigEntityBase:: |
|
OgRole:: |
public | function |
Sets the group bundle. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Sets the group type. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Sets the ID of the role. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Sets the label. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Sets the role name. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Sets the role type. Overrides OgRoleInterface:: |
|
OgRole:: |
public | function |
Constructs an OgRole object. Overrides ConfigEntityBase:: |
|
OgRoleInterface:: |
constant | The role name of the group administrator. | ||
OgRoleInterface:: |
constant | The role name of the group non-member. | ||
OgRoleInterface:: |
constant | The role name of the group member. | ||
OgRoleInterface:: |
constant | Role type for required roles. | ||
OgRoleInterface:: |
constant | Role type for standard roles that are editable and deletable. | ||
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
Role:: |
protected | property | The machine name of this role. | |
Role:: |
protected | property | An indicator whether the role has all permissions. | |
Role:: |
protected | property | The human-readable label of this role. | |
Role:: |
protected | property | The permissions belonging to this role. | |
Role:: |
protected | property | The weight of this role in administrative listings. | |
Role:: |
public | function |
Returns a list of permissions assigned to the role. Overrides RoleInterface:: |
|
Role:: |
public | function |
Returns the weight. Overrides RoleInterface:: |
|
Role:: |
public | function |
Grant permissions to the role. Overrides RoleInterface:: |
|
Role:: |
public | function |
Checks if the role has a permission. Overrides RoleInterface:: |
|
Role:: |
public | function |
Indicates that a role has all available permissions. Overrides RoleInterface:: |
|
Role:: |
public static | function |
Acts on loaded entities. Overrides EntityBase:: |
|
Role:: |
public | function |
Acts on an entity before the presave hook is invoked. Overrides ConfigEntityBase:: |
|
Role:: |
public | function |
Revokes a permissions from the user role. Overrides RoleInterface:: |
|
Role:: |
public | function |
Sets the role to be an admin role. Overrides RoleInterface:: |
|
Role:: |
public | function |
Sets the weight to the given value. Overrides RoleInterface:: |
|
RoleInterface:: |
constant | Role ID for anonymous users; should match the 'role' entity ID. | ||
RoleInterface:: |
constant | Role ID for authenticated users; should match the 'role' entity ID. | ||
SynchronizableEntityTrait:: |
protected | property | Whether this entity is being created, updated or deleted through a synchronization process. | |
SynchronizableEntityTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
public | function |