You are here

class GroupMembership in Group 8

Same name in this branch
  1. 8 src/GroupMembership.php \Drupal\group\GroupMembership
  2. 8 src/Plugin/GroupContentEnabler/GroupMembership.php \Drupal\group\Plugin\GroupContentEnabler\GroupMembership
Same name and namespace in other branches
  1. 2.0.x src/GroupMembership.php \Drupal\group\GroupMembership

Wrapper class for a GroupContent entity representing a membership.

Should be loaded through the 'group.membership_loader' service.

Hierarchy

Expanded class hierarchy of GroupMembership

1 file declares its use of GroupMembership
IsGroupMemberCacheContextTest.php in tests/src/Unit/IsGroupMemberCacheContextTest.php

File

src/GroupMembership.php, line 13

Namespace

Drupal\group
View source
class GroupMembership implements CacheableDependencyInterface {

  /**
   * The group content entity to wrap.
   *
   * @var \Drupal\group\Entity\GroupContentInterface
   */
  protected $groupContent;

  /**
   * Constructs a new GroupMembership.
   *
   * @param \Drupal\group\Entity\GroupContentInterface $group_content
   *   The group content entity representing the membership.
   *
   * @throws \Exception
   *   Exception thrown when trying to instantiate this class with a
   *   GroupContent entity that was not based on the GroupMembership content
   *   enabler plugin.
   */
  public function __construct(GroupContentInterface $group_content) {
    if ($group_content
      ->getGroupContentType()
      ->getContentPluginId() == 'group_membership') {
      $this->groupContent = $group_content;
    }
    else {
      throw new \Exception('Trying to create a GroupMembership from an incompatible GroupContent entity.');
    }
  }

  /**
   * Returns the fieldable GroupContent entity for the membership.
   *
   * @return \Drupal\group\Entity\GroupContentInterface
   */
  public function getGroupContent() {
    return $this->groupContent;
  }

  /**
   * Returns the group for the membership.
   *
   * @return \Drupal\group\Entity\GroupInterface
   */
  public function getGroup() {
    return $this->groupContent
      ->getGroup();
  }

  /**
   * Returns the user for the membership.
   *
   * @return \Drupal\user\UserInterface
   */
  public function getUser() {
    return $this->groupContent
      ->getEntity();
  }

  /**
   * Returns the group roles for the membership.
   *
   * @return \Drupal\group\Entity\GroupRoleInterface[]
   *   An array of group roles, keyed by their ID.
   */
  public function getRoles() {

    /** @var \Drupal\group\Entity\Storage\GroupRoleStorageInterface $group_role_storage */
    $group_role_storage = \Drupal::entityTypeManager()
      ->getStorage('group_role');
    return $group_role_storage
      ->loadByUserAndGroup($this
      ->getUser(), $this
      ->getGroup());
  }

  /**
   * Checks whether the member has a permission.
   *
   * @param string $permission
   *   The permission to check for.
   *
   * @return bool
   *   Whether the member has the requested permission.
   */
  public function hasPermission($permission) {
    return $this
      ->groupPermissionChecker()
      ->hasPermissionInGroup($permission, $this
      ->getUser(), $this
      ->getGroup());
  }

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

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

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

  /**
   * Gets the group permission checker.
   *
   * @return \Drupal\group\Access\GroupPermissionCheckerInterface
   */
  protected function groupPermissionChecker() {
    return \Drupal::service('group_permission.checker');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupMembership::$groupContent protected property The group content entity to wrap.
GroupMembership::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
GroupMembership::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
GroupMembership::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
GroupMembership::getGroup public function Returns the group for the membership.
GroupMembership::getGroupContent public function Returns the fieldable GroupContent entity for the membership.
GroupMembership::getRoles public function Returns the group roles for the membership.
GroupMembership::getUser public function Returns the user for the membership.
GroupMembership::groupPermissionChecker protected function Gets the group permission checker.
GroupMembership::hasPermission public function Checks whether the member has a permission.
GroupMembership::__construct public function Constructs a new GroupMembership.