You are here

class DefaultRoleEvent in Organic groups 8

Event that is fired when default roles are compiled.

This event allows implementing modules to provide their own default roles or alter existing default roles that are provided by other modules.

Hierarchy

Expanded class hierarchy of DefaultRoleEvent

3 files declare their use of DefaultRoleEvent
DefaultRoleEventIntegrationTest.php in tests/src/Kernel/DefaultRoleEventIntegrationTest.php
DefaultRoleEventTest.php in tests/src/Unit/DefaultRoleEventTest.php
OgRoleManager.php in src/OgRoleManager.php

File

src/Event/DefaultRoleEvent.php, line 16

Namespace

Drupal\og\Event
View source
class DefaultRoleEvent extends Event implements DefaultRoleEventInterface {

  /**
   * The list of default roles.
   *
   * @var array
   *   An associative array of default role properties, keyed by role name.
   */
  protected $roles = [];

  /**
   * {@inheritdoc}
   */
  public function getRole($name) {
    if (!isset($this->roles[$name])) {
      throw new \InvalidArgumentException("The '{$name}' role does not exist.'");
    }
    return $this->roles[$name];
  }

  /**
   * {@inheritdoc}
   */
  public function getRoles() {
    return $this->roles;
  }

  /**
   * {@inheritdoc}
   */
  public function addRole(OgRole $role) {
    $this
      ->validate($role);
    if (array_key_exists($role
      ->getName(), $this->roles)) {
      throw new \InvalidArgumentException("The '{$role->getName()}' role already exists.");
    }
    $this->roles[$role
      ->getName()] = $role;
  }

  /**
   * {@inheritdoc}
   */
  public function addRoles(array $roles) {
    foreach ($roles as $role) {
      $this
        ->addRole($role);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setRole(OgRole $role) {
    $this
      ->validate($role);
    $this
      ->deleteRole($role
      ->getName());
    $this
      ->addRole($role);
  }

  /**
   * {@inheritdoc}
   */
  public function setRoles(array $roles) {
    foreach ($roles as $properties) {
      $this
        ->setRole($properties);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteRole($name) {
    unset($this->roles[$name]);
  }

  /**
   * {@inheritdoc}
   */
  public function hasRole($name) {
    return isset($this->roles[$name]);
  }

  /**
   * {@inheritdoc}
   */
  public function offsetGet($key) {
    return $this
      ->getRole($key);
  }

  /**
   * {@inheritdoc}
   */
  public function offsetSet($key, $role) {
    $this
      ->validate($role);
    if ($role
      ->getName() !== $key) {
      throw new \InvalidArgumentException('The key and the "name" property of the role should be identical.');
    }
    $this
      ->setRole($role);
  }

  /**
   * {@inheritdoc}
   */
  public function offsetUnset($key) {
    $this
      ->deleteRole($key);
  }

  /**
   * {@inheritdoc}
   */
  public function offsetExists($key) {
    return $this
      ->hasRole($key);
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    return new \ArrayIterator($this->roles);
  }

  /**
   * Validates that a role that is about to be set or added has a name.
   *
   * The roles are stored locally keyed by role name.
   *
   * @param \Drupal\og\Entity\OgRole $role
   *   The role to validate.
   *
   * @throws \InvalidArgumentException
   *   Thrown when the role name is empty.
   */
  protected function validate(OgRole $role) {
    if (empty($role
      ->getName())) {
      throw new \InvalidArgumentException('Role name is required.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultRoleEvent::$roles protected property The list of default roles.
DefaultRoleEvent::addRole public function Adds a default role. Overrides DefaultRoleEventInterface::addRole
DefaultRoleEvent::addRoles public function Adds multiple default roles. Overrides DefaultRoleEventInterface::addRoles
DefaultRoleEvent::deleteRole public function Deletes the given default role. Overrides DefaultRoleEventInterface::deleteRole
DefaultRoleEvent::getIterator public function
DefaultRoleEvent::getRole public function Returns a single default role. Overrides DefaultRoleEventInterface::getRole
DefaultRoleEvent::getRoles public function Returns all the default role names. Overrides DefaultRoleEventInterface::getRoles
DefaultRoleEvent::hasRole public function Returns whether or not the given role exists. Overrides DefaultRoleEventInterface::hasRole
DefaultRoleEvent::offsetExists public function
DefaultRoleEvent::offsetGet public function
DefaultRoleEvent::offsetSet public function
DefaultRoleEvent::offsetUnset public function
DefaultRoleEvent::setRole public function Sets a default roles. Overrides DefaultRoleEventInterface::setRole
DefaultRoleEvent::setRoles public function Sets multiple default roles. Overrides DefaultRoleEventInterface::setRoles
DefaultRoleEvent::validate protected function Validates that a role that is about to be set or added has a name.
DefaultRoleEventInterface::EVENT_NAME constant The event name.