You are here

function opigno_moxtra_group_content_delete in Opigno Moxtra 8

Same name and namespace in other branches
  1. 3.x opigno_moxtra.module \opigno_moxtra_group_content_delete()

Implements hook_ENTITY_TYPE_delete().

File

./opigno_moxtra.module, line 675
Contains opigno_moxtra.module.

Code

function opigno_moxtra_group_content_delete(EntityInterface $entity) {
  if (!_opigno_moxtra_is_active()) {
    return;
  }

  /** @var \Drupal\group\Entity\GroupContentInterface $entity */
  $content = $entity
    ->getEntity();
  if (!isset($content)) {

    // If related entity is already deleted.
    return;
  }
  $bundle = $entity
    ->bundle();
  $type = $content
    ->getEntityTypeId();
  if ($bundle === 'learning_path-group_membership') {

    // Removes user from the collaborative workspace of the training.
    $group = $entity
      ->getGroup();
    $workspace = _opigno_moxtra_get_workspace($group);
    if ($workspace !== NULL) {
      $binder_id = $workspace
        ->getBinderId();
      if (!empty($binder_id)) {
        $moxtra_api = _opigno_moxtra_get_moxtra_api();
        $owner_id = $workspace
          ->getOwnerId();

        /** @var \Drupal\user\Entity\User $user */
        $user = $content;
        $user_id = $user
          ->id();

        // Remove user from collaborative workspace.
        $workspace
          ->removeMember($user_id);
        $workspace
          ->save();

        // Remove in the Moxtra.
        $moxtra_api
          ->removeUserFromWorkspace($owner_id, $binder_id, $user_id);
      }
    }
  }
  elseif ($type === 'opigno_moxtra_meeting') {

    // Removes the related training field in the meeting.

    /** @var \Drupal\opigno_moxtra\MeetingInterface $meeting */
    $meeting = $content;
    $meeting
      ->setTrainingId(NULL);
    $meeting
      ->save();
  }
}