You are here

function opigno_moxtra_group_insert in Opigno Moxtra 8

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

Implements hook_ENTITY_TYPE_insert().

Creates a collaborative workspace for the training.

File

./opigno_moxtra.module, line 443
Contains opigno_moxtra.module.

Code

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

  /** @var \Drupal\group\Entity\Group $entity */
  if ($entity
    ->bundle() === 'learning_path') {
    $has_workspace = !$entity
      ->get('field_workspace')
      ->isEmpty();
    if (!$has_workspace) {
      $moxtra_api = _opigno_moxtra_get_moxtra_api();
      $user = \Drupal::currentUser();
      $user_id = $user
        ->id();
      $name = $entity
        ->label();
      $response = $moxtra_api
        ->createWorkspace($user_id, $name);
      $binder_id = $response['data']['id'];

      // Create workspace.
      $workspace = Workspace::create();
      $workspace
        ->setName($entity
        ->label());
      $workspace
        ->setBinderId($binder_id);
      $workspace
        ->save();

      // Attach workspace to the training.
      $entity
        ->set('field_workspace', [
        'target_id' => $workspace
          ->id(),
      ]);
      $entity
        ->save();
    }
  }
}