View source
<?php
declare (strict_types=1);
namespace Drupal\og\Entity;
use Drupal\Core\Config\ConfigValueException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\og\Exception\OgRoleException;
use Drupal\og\OgRoleInterface;
use Drupal\user\Entity\Role;
class OgRole extends Role implements OgRoleInterface {
protected $name;
protected $parentEntityIsBeingRemoved = FALSE;
public function __construct(array $values) {
parent::__construct($values, 'og_role');
}
public function setId($id) {
$this
->set('id', $id);
return $this;
}
public function getLabel() {
return $this
->get('label');
}
public function setLabel($label) {
$this
->set('label', $label);
return $this;
}
public function getGroupType() {
return $this
->get('group_type');
}
public function setGroupType($group_type) {
$this
->set('group_type', $group_type);
return $this;
}
public function getGroupBundle() {
return $this
->get('group_bundle');
}
public function setGroupBundle($group_bundle) {
$this
->set('group_bundle', $group_bundle);
return $this;
}
public function getRoleType() {
return $this
->get('role_type') ?: OgRoleInterface::ROLE_TYPE_STANDARD;
}
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);
}
public function getName() {
if (empty($this->name) && $this
->id() && $this
->getGroupType() && $this
->getGroupBundle()) {
$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');
}
public function setName($name) {
$this->name = $name;
return $this;
}
public static function loadByGroupAndName(EntityInterface $group, $name) {
$role_id = "{$group->getEntityTypeId()}-{$group->bundle()}-{$name}";
return self::load($role_id);
}
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);
}
public function save() {
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 ($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.');
}
$prefix = $this
->getGroupType() . '-' . $this
->getGroupBundle() . '-';
$this
->setId($prefix . $this
->getName());
}
parent::save();
}
public function set($property_name, $value) {
$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) {
return $this;
}
throw new OgRoleException("The {$property_name} cannot be changed.");
}
public function delete() {
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();
}
public function isRequired() {
return static::getRoleTypeByName($this
->getName()) === OgRoleInterface::ROLE_TYPE_REQUIRED;
}
public static function getRoleTypeByName($role_name) {
return in_array($role_name, [
OgRoleInterface::ANONYMOUS,
OgRoleInterface::AUTHENTICATED,
]) ? OgRoleInterface::ROLE_TYPE_REQUIRED : OgRoleInterface::ROLE_TYPE_STANDARD;
}
public static function getRole($entity_type_id, $bundle, $role_name) {
return self::load($entity_type_id . '-' . $bundle . '-' . $role_name);
}
protected function groupTypeManager() {
return \Drupal::service('og.group_type_manager');
}
protected function ogAccess() {
return \Drupal::service('og.access');
}
public function calculateDependencies() {
parent::calculateDependencies();
$bundle_config_dependency = \Drupal::entityTypeManager()
->getDefinition($this
->getGroupType())
->getBundleConfigDependency($this
->getGroupBundle());
$this
->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
}
public function onDependencyRemoval(array $dependencies) {
$this->parentEntityIsBeingRemoved = TRUE;
return parent::onDependencyRemoval($dependencies);
}
}