You are here

CourseEntityForm.php in Course 3.x

Same filename and directory in other branches
  1. 8.3 src/Form/CourseEntityForm.php
  2. 8.2 src/Form/CourseEntityForm.php

Namespace

Drupal\course\Form

File

src/Form/CourseEntityForm.php
View source
<?php

namespace Drupal\course\Form;

use Drupal\course\Entity\CourseType;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class CourseEntityForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   *
   * Redirect to the outline form after course creation.
   */
  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(),
      ]);
    }
  }

}

Classes

Namesort descending Description
CourseEntityForm