public function OgMembership::preSave in Organic groups 8
Acts on an entity before the presave hook is invoked.
Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.
Parameters
\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.
Throws
\Exception When there is a problem that should prevent saving the entity.
Overrides ContentEntityBase::preSave
See also
\Drupal\Core\Field\FieldItemListInterface::preSave()
File
- src/
Entity/ OgMembership.php, line 436
Class
- OgMembership
- The membership entity that connects a group and a user.
Namespace
Drupal\og\EntityCode
public function preSave(EntityStorageInterface $storage) {
// Check the value directly rather than using the entity, if there is one.
// This will watch actual empty values and '0'.
if (!($uid = $this
->get('uid')->target_id)) {
// Throw a generic logic exception as this will likely get caught in
// \Drupal\Core\Entity\Sql\SqlContentEntityStorage::save and turned into
// an EntityStorageException anyway.
throw new \LogicException('OG membership can not be created for an empty or anonymous user.');
}
if (!($entity_id = $this
->get('entity_id')->value)) {
// Group was not set.
throw new \LogicException('Membership cannot be set for an empty or an unsaved group.');
}
if (!($group = $this
->getGroup())) {
throw new \LogicException('A group entity is required for creating a membership.');
}
$entity_type_id = $group
->getEntityTypeId();
$bundle = $group
->bundle();
if (!Og::isGroup($entity_type_id, $bundle)) {
// Group is not valid.
throw new \LogicException(sprintf('Entity type %s with ID %s is not an OG group.', $entity_type_id, $group
->id()));
}
// Check if the roles are valid.
foreach ($this
->getRoles() as $role) {
/** @var \Drupal\og\Entity\OgRole $role */
// Make sure we don't save a membership for a non-member.
if ($role
->getName() == OgRoleInterface::ANONYMOUS) {
throw new \LogicException('Cannot save an OgMembership with reference to a non-member role.');
}
elseif ($role
->getName() == OgRoleInterface::AUTHENTICATED) {
$this
->revokeRole($role);
}
elseif (!$this
->isRoleValid($role)) {
throw new \LogicException(sprintf('The role with ID %s does not match the group type of the membership.', $role
->id()));
}
}
// Check for an existing membership.
$query = \Drupal::entityQuery('og_membership');
$query
->condition('uid', $uid)
->condition('entity_id', $entity_id)
->condition('entity_type', $this
->get('entity_type')->value);
if (!$this
->isNew()) {
// Filter out this membership.
$query
->condition('id', $this
->id(), '<>');
}
$count = $query
->range(0, 1)
->count()
->execute();
if ($count) {
throw new \LogicException(sprintf('An OG membership already exists for user ID %d and group of entity type %s and ID %s', $uid, $entity_type_id, $group
->id()));
}
parent::preSave($storage);
}