You are here

public function OpignoActivityForm::save in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/OpignoActivityForm.php \Drupal\opigno_module\Form\OpignoActivityForm::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/OpignoActivityForm.php, line 242

Class

OpignoActivityForm
Form controller for Activity edit forms.

Namespace

Drupal\opigno_module\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $activity =& $this->entity;

  // Get URL parameters.
  $params = \Drupal::request()->query
    ->all();

  // Save Activity entity.
  $status = parent::save($form, $form_state);

  // Reset usage of activity for activities which not in skills modules.
  $values = $form_state
    ->getValues();
  if ($values['auto_skills']['value'] == 0) {
    $activity
      ->set('usage_activity', 'local');
    if ($values['manual_skills_management'] == 0) {
      $activity
        ->setSkillId(NULL);
    }
    $activity
      ->save();
  }

  // Update video filename.
  if (!empty($values['field_video'][0]['fids'])) {
    $fid = $values['field_video'][0]['fids'][0];
    $file = File::load($fid);
    $this
      ->renameFile($file);
  }
  switch ($status) {
    case SAVED_NEW:
      if (isset($params['module_id']) && !empty($params['module_id'] && $params['module_vid'])) {
        $opigno_module = \Drupal::entityTypeManager()
          ->getStorage('opigno_module')
          ->load($params['module_id']);
        $opigno_module_obj = \Drupal::service('opigno_module.opigno_module');
        $opigno_module_obj
          ->activitiesToModule([
          $activity,
        ], $opigno_module);
      }
      \Drupal::messenger()
        ->addMessage($this
        ->t('Created the %label Activity.', [
        '%label' => $activity
          ->label(),
      ]));
      break;
    default:
      \Drupal::messenger()
        ->addMessage($this
        ->t('Saved the %label Activity.', [
        '%label' => $activity
          ->label(),
      ]));
  }
  $form_state
    ->setRedirect('entity.opigno_activity.canonical', [
    'opigno_activity' => $activity
      ->id(),
  ]);
}