Skip to content
Snippets Groups Projects

Stop saving an entity when it gets added to a group

Files
3
@@ -2,6 +2,7 @@
namespace Drupal\group\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Attribute\ContentEntityType;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
@@ -24,6 +25,7 @@ use Drupal\group\Entity\Views\GroupRelationshipViewsData;
use Drupal\group\Form\GroupJoinForm;
use Drupal\group\Form\GroupLeaveForm;
use Drupal\user\EntityOwnerTrait;
use Drupal\user\UserInterface;
/**
* Defines the relationship entity.
@@ -270,11 +272,19 @@ class GroupRelationship extends ContentEntityBase implements GroupRelationshipIn
if ($update === FALSE) {
// We want to make sure that the entity we just added to the group behaves
// as a grouped entity. This means we may need to update access records,
// flush some caches containing the entity or perform other operations we
// cannot possibly know about. Lucky for us, all of that behavior usually
// happens when saving an entity so let's re-save the added entity.
$this->getEntity()->save();
// as a grouped entity. Any code that runs "live" should get the new
// information, but we want to invalidate cache entries that listed the
// entity as a cacheable dependency.
//
// One drawback to the above is that not everything gets cached. Some
// things get calculated and stored as entities (or other DB entries) of
// their own without any cacheable metadata attached. One example being
// the Pathauto module generating path aliases upon entity save.
//
// It is up to these modules to listen to hook_group_relationship_insert()
// and call ::getEntity() on the relationship to also run their code on
// the entity that got added to a group.
Cache::invalidateTags($this->getEntity()->getCacheTags());
}
// If a membership gets updated, but the member's roles haven't changed, we
@@ -305,13 +315,14 @@ class GroupRelationship extends ContentEntityBase implements GroupRelationshipIn
foreach ($entities as $group_relationship) {
assert($group_relationship instanceof GroupRelationshipInterface);
if ($entity = $group_relationship->getEntity()) {
// For the same reasons we re-save entities that are added to a group,
// we need to re-save entities that were removed from one. See
// ::postSave(). We only save the entity if it still exists to avoid
// trying to save an entity that just got deleted and triggered the
// deletion of its relationship entities.
// @todo Revisit when https://www.drupal.org/node/2754399 lands.
$entity->save();
// For the same reason we invalidate the cache tags of entities that are
// added to a group, we need to invalidate the cache tags of those that
// were removed from one. See ::postSave().
//
// We can only invalidate the cache tags of entities that still exist
// and not the ones that were just deleted and triggered the deletion of
// their group relationship entities.
Cache::invalidateTags($entity->getCacheTags());
// If a membership gets deleted, we need to reset the internal group
// roles cache for the member in that group, but only if the user still
@@ -319,7 +330,8 @@ class GroupRelationship extends ContentEntityBase implements GroupRelationshipIn
if ($group_relationship->getPluginId() == 'group_membership') {
$role_storage = \Drupal::entityTypeManager()->getStorage('group_role');
assert($role_storage instanceof GroupRoleStorageInterface);
$role_storage->resetUserGroupRoleCache($group_relationship->getEntity(), $group_relationship->getGroup());
assert($entity instanceof UserInterface);
$role_storage->resetUserGroupRoleCache($entity, $group_relationship->getGroup());
}
}
}
Loading