You are here

public function CourseEntityForm::save in Course 3.x

Same name and namespace in other branches
  1. 8.3 src/Form/CourseEntityForm.php \Drupal\course\Form\CourseEntityForm::save()
  2. 8.2 src/Form/CourseEntityForm.php \Drupal\course\Form\CourseEntityForm::save()

Redirect to the outline form after course creation.

Overrides EntityForm::save

File

src/Form/CourseEntityForm.php, line 16

Class

CourseEntityForm

Namespace

Drupal\course\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $course = $this->entity;
  $insert = $course
    ->isNew();
  parent::save($form, $form_state);
  $type = CourseType::load($course
    ->bundle());
  $node_link = $course
    ->toLink($this
    ->t('View'))
    ->toString();
  $context = [
    '@type' => $course
      ->bundle(),
    '%title' => $course
      ->label(),
    'link' => $node_link,
  ];
  $t_args = [
    '@type' => $type
      ->label(),
    '%title' => $course
      ->toLink()
      ->toString(),
  ];
  if ($insert) {
    $this
      ->logger('course')
      ->notice('@type: added %title.', $context);
    $this
      ->messenger()
      ->addStatus($this
      ->t('@type %title has been created.', $t_args));
    $form_state
      ->setRedirect('entity.course.canonical', [
      'course' => $this->entity
        ->id(),
    ]);
  }
  else {
    $this
      ->logger('course')
      ->notice('@type: updated %title.', $context);
    $this
      ->messenger()
      ->addStatus($this
      ->t('@type %title has been updated.', $t_args));
    $form_state
      ->setRedirect('entity.course.canonical', [
      'course' => $this->entity
        ->id(),
    ]);
  }
}