<?php

namespace Drupal\{{ machine_name }}\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form controller for the {{ entity_type_label|lower }} entity edit forms.
 */
class {{ class_prefix }}Form extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $result = parent::save($form, $form_state);

    $entity = $this->getEntity();

    $message_arguments = ['%label' => $entity->toLink()->toString()];
    $logger_arguments = [
      '%label' => $entity->label(),
      'link' => $entity->toLink($this->t('View'))->toString(),
    ];

    switch ($result) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('New {{ entity_type_label|lower }} %label has been created.', $message_arguments));
        $this->logger('{{ machine_name }}')->notice('Created new {{ entity_type_label|lower }} %label', $logger_arguments);
        break;

      case SAVED_UPDATED:
        $this->messenger()->addStatus($this->t('The {{ entity_type_label|lower }} %label has been updated.', $message_arguments));
        $this->logger('{{ machine_name }}')->notice('Updated {{ entity_type_label|lower }} %label.', $logger_arguments);
        break;
    }

{% if canonical %}
    $form_state->setRedirect('entity.{{ entity_type_id }}.canonical', ['{{ entity_type_id }}' => $entity->id()]);
{% else %}
    $form_state->setRedirect('entity.{{ entity_type_id }}.collection');
{% endif %}

    return $result;
  }

}
