<?php

declare(strict_types=1);

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.
 */
final class {{ class }}Form extends ContentEntityForm {

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

    $message_args = ['%label' => $this->entity->toLink()->toString()];
    $logger_args = [
      '%label' => $this->entity->label(),
      'link' => $this->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_args));
        $this->logger('{{ machine_name }}')->notice('New {{ entity_type_label|lower }} %label has been created.', $logger_args);
        break;

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

      default:
        throw new \LogicException('Could not save the entity.');
    }

{% if canonical %}
    $form_state->setRedirectUrl($this->entity->toUrl());
{% else %}
    $form_state->setRedirectUrl($this->entity->toUrl('collection'));
{% endif %}

    return $result;
  }

}
