class GroupMembership in Group 8
Same name in this branch
- 8 src/GroupMembership.php \Drupal\group\GroupMembership
- 8 src/Plugin/GroupContentEnabler/GroupMembership.php \Drupal\group\Plugin\GroupContentEnabler\GroupMembership
Same name and namespace in other branches
- 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
- class \Drupal\group\GroupMembership implements CacheableDependencyInterface
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\groupView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GroupMembership:: |
protected | property | The group content entity to wrap. | |
GroupMembership:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
|
GroupMembership:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
|
GroupMembership:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
|
GroupMembership:: |
public | function | Returns the group for the membership. | |
GroupMembership:: |
public | function | Returns the fieldable GroupContent entity for the membership. | |
GroupMembership:: |
public | function | Returns the group roles for the membership. | |
GroupMembership:: |
public | function | Returns the user for the membership. | |
GroupMembership:: |
protected | function | Gets the group permission checker. | |
GroupMembership:: |
public | function | Checks whether the member has a permission. | |
GroupMembership:: |
public | function | Constructs a new GroupMembership. |