You are here

public function GroupContent::postSave in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Entity/GroupContent.php \Drupal\group\Entity\GroupContent::postSave()

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update 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.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides ContentEntityBase::postSave

File

src/Entity/GroupContent.php, line 175

Class

GroupContent
Defines the Group content entity.

Namespace

Drupal\group\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // For memberships, we generally need to rebuild the group role cache for
  // the member's user account in the target group.
  $rebuild_group_role_cache = $this
    ->getContentPlugin()
    ->getPluginId() == 'group_membership';
  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();
  }
  elseif ($rebuild_group_role_cache) {
    $new = array_column($this->group_roles
      ->getValue(), 'target_id');
    $old = array_column($this->original->group_roles
      ->getValue(), 'target_id');
    sort($new);
    sort($old);
    $rebuild_group_role_cache = $new != $old;
  }
  if ($rebuild_group_role_cache) {

    /** @var \Drupal\group\Entity\Storage\GroupRoleStorageInterface $role_storage */
    $role_storage = \Drupal::entityTypeManager()
      ->getStorage('group_role');
    $role_storage
      ->resetUserGroupRoleCache($this
      ->getEntity(), $this
      ->getGroup());
  }
}