public function WorkspaceForm::save in Opigno Moxtra 8
Same name and namespace in other branches
- 3.x src/Form/WorkspaceForm.php \Drupal\opigno_moxtra\Form\WorkspaceForm::save()
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Form/ WorkspaceForm.php, line 199
Class
- WorkspaceForm
- Provides a form for creating/editing a opigno_moxtra_workspace entity.
Namespace
Drupal\opigno_moxtra\FormCode
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\opigno_moxtra\WorkspaceInterface $entity */
$entity = $this->entity;
$is_new = FALSE;
$storage = $form_state
->getStorage();
if ($entity
->isNew() && $entity
->hasField('training') && !empty($storage['training'])) {
// Set Workspace related Training if been creating from training page.
$entity
->set('training', [
[
'target_id' => $storage['training'],
],
]);
}
$binder_id = $entity
->getBinderId();
if (empty($binder_id)) {
// Create binder in the Moxtra.
$user_id = $this
->currentUser()
->id();
$name = $form_state
->getValue('name');
$response = $this->moxtraService
->createWorkspace($user_id, $name);
$entity
->setBinderId($response['data']['id']);
$is_new = TRUE;
}
$auto_register = $form_state
->getValue('auto_register');
// Set auto_register false for workspaces not in groups.
if (empty($auto_register) && !$is_new) {
$entity
->setAutoRegister(0);
$auto_register = 0;
}
// Update list of members if auto registration of members is switched off.
if (!$is_new && $auto_register == 0) {
$new_list_of_members = $form_state
->getValue('training_users');
$old_list_of_members = $entity
->getMembersIds();
$owner_id = $entity
->getOwnerId();
// Check if owner of the group tried to delete himself.
// If yes then add him back to the members.
// Owner of the group must be in the members list.
if (!in_array($owner_id, $new_list_of_members)) {
$new_list_of_members[$owner_id] = $owner_id;
}
// Set updated list of members.
$entity
->setMembers($new_list_of_members);
// Add users to workspace.
foreach ($new_list_of_members as $member_id) {
if (!in_array($member_id, $old_list_of_members)) {
// Add user to the binder in the Moxtra.
$moxtra_api = _opigno_moxtra_get_moxtra_api();
$moxtra_api
->addUsersToWorkspace($owner_id, $binder_id, [
$member_id,
]);
}
}
// Remove users from workspace.
foreach ($old_list_of_members as $member_id) {
if (!in_array($member_id, $new_list_of_members)) {
// Add user to the binder in the Moxtra.
$moxtra_api = _opigno_moxtra_get_moxtra_api();
$moxtra_api
->removeUserFromWorkspace($owner_id, $binder_id, $member_id);
}
}
}
elseif (!$is_new && $auto_register == 1) {
// Add all members of group with access to the Moxtra into the workspace.
$workspace_id = $entity
->id();
$query = \Drupal::service('entity.query')
->get('group')
->condition('field_workspace', $workspace_id);
$result = $query
->execute();
$group_id = array_values($result)[0];
$group = Group::load($group_id);
$group_members = $group
->getMembers();
$current_workspace_members = $entity
->getMembersIds();
// Find users which needs to add into workspace.
foreach ($group_members as $group_member) {
$group_user = $group_member
->getUser();
if (_opigno_moxtra_is_user_enabled($group_user)) {
$new_list_of_members[$group_user
->id()] = $group_user
->id();
$owner_id = $entity
->getOwnerId();
if (!in_array($group_user
->id(), $current_workspace_members)) {
$moxtra_api = _opigno_moxtra_get_moxtra_api();
$moxtra_api
->addUsersToWorkspace($owner_id, $binder_id, [
$group_user
->id(),
]);
}
}
}
$entity
->setMembers($new_list_of_members);
}
// Save entity.
$status = parent::save($form, $form_state);
// Set status message.
$workspace_link = $entity
->toLink()
->toString();
if ($status == SAVED_UPDATED) {
$message = $this
->t('The Collaborative Workspace %workspace has been updated.', [
'%workspace' => $workspace_link,
]);
}
else {
$message = $this
->t('The Collaborative Workspace %workspace has been created.', [
'%workspace' => $workspace_link,
]);
}
$this
->messenger()
->addMessage($message);
// Set redirect.
$form_state
->setRedirect('opigno_moxtra.workspace', [
'opigno_moxtra_workspace' => $entity
->id(),
]);
return $status;
}