function opigno_moxtra_group_insert in Opigno Moxtra 3.x
Same name and namespace in other branches
- 8 opigno_moxtra.module \opigno_moxtra_group_insert()
Implements hook_ENTITY_TYPE_insert().
Creates a collaborative workspace for the training.
File
- ./
opigno_moxtra.module, line 445 - Contains opigno_moxtra.module.
Code
function opigno_moxtra_group_insert(EntityInterface $entity) {
if (!_opigno_moxtra_is_active() || !$entity instanceof GroupInterface || $entity
->bundle() !== 'learning_path' || !$entity
->get('field_workspace')
->isEmpty()) {
return;
}
$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'] ?? NULL;
// Create workspace.
$workspace = Workspace::create();
$workspace
->setName($entity
->label());
if ($binder_id) {
$workspace
->setBinderId($binder_id);
}
try {
$workspace
->save();
} catch (EntityStorageException $e) {
watchdog_exception('opigno_moxtra_exception', $e);
}
// Attach workspace to the training.
$entity
->set('field_workspace', [
'target_id' => $workspace
->id(),
]);
try {
$entity
->save();
} catch (EntityStorageException $e) {
watchdog_exception('opigno_moxtra_exception', $e);
}
}