public function OpignoModuleForm::save in Opigno module 8
Same name and namespace in other branches
- 3.x src/Form/OpignoModuleForm.php \Drupal\opigno_module\Form\OpignoModuleForm::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/ OpignoModuleForm.php, line 311
Class
- OpignoModuleForm
- Form controller for Module edit forms.
Namespace
Drupal\opigno_module\FormCode
public function save(array $form, FormStateInterface $form_state) {
$entity =& $this->entity;
$status = parent::save($form, $form_state);
$values = $form_state
->getValues();
$route_params = current(\Drupal::routeMatch()
->getParameters());
$module_id = $entity
->id();
$group_id = FALSE;
$open_date = !empty($values['open_date_training_module']) ? $values['open_date_training_module']
->getTimestamp() : 0;
$close_date = !empty($values['close_date_training_module']) ? $values['close_date_training_module']
->getTimestamp() : 0;
if (isset($route_params['group'])) {
if ($route_params['group']
->getGroupType()
->id() == 'opigno_course') {
$build_info = $form_state
->getBuildInfo();
if (!empty($build_info['args'][0]['opigno_group_info']['learning_path'])) {
$group_id = $build_info['args'][0]['opigno_group_info']['learning_path'];
}
}
else {
$group_id = $route_params['group']
->id();
}
}
if ($module_id && $group_id) {
$availability_entity = LPModuleAvailability::loadByProperties([
'group_id' => $group_id,
'entity_id' => $module_id,
]);
if (empty($availability_entity)) {
try {
$availability_entity = LPModuleAvailability::createWithValues($group_id, $module_id, $values['availability'], $open_date, $close_date);
} catch (\Exception $e) {
\Drupal::logger('opigno_module')
->error($e
->getMessage());
\Drupal::messenger()
->addMessage($e
->getMessage(), 'error');
}
}
else {
$availability_entity = current($availability_entity);
$availability_entity
->set('availability', $values['availability']);
$availability_entity
->set('open_date', $open_date);
$availability_entity
->set('close_date', $close_date);
}
if (!empty($availability_entity)) {
try {
$availability_entity
->save();
} catch (\Exception $e) {
\Drupal::logger('opigno_module')
->error($e
->getMessage());
\Drupal::messenger()
->addMessage($e
->getMessage(), 'error');
}
}
}
switch ($status) {
case SAVED_NEW:
\Drupal::messenger()
->addMessage($this
->t('Created the %label Module.', [
'%label' => $entity
->label(),
]));
/* @todo Find better way to save results options */
// Save results options.
$entity
->insertResultsOptions($form_state);
break;
default:
\Drupal::messenger()
->addMessage($this
->t('Saved the %label Module.', [
'%label' => $entity
->label(),
]));
/* @todo Find better way to save results options */
// Save results options.
$entity
->updateResultsOptions($form_state);
}
$form_state
->setRedirect('opigno_module.modules', [
'opigno_module' => $entity
->id(),
]);
}