You are here

ExpenseTypeForm.php in Drupal PM (Project Management) 4.x

File

modules/pm_expense/src/Form/ExpenseTypeForm.php
View source
<?php

namespace Drupal\pm_expense\Form;

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

/**
 * Provides a form for editing a Expense type.
 *
 * @ingroup pm_expense
 */
class ExpenseTypeForm extends EntityForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $pm_expense_type = $this->entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $pm_expense_type
        ->label(),
      '#description' => $this
        ->t("Label for the Expense type."),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $pm_expense_type
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\pm_expense\\Entity\\ExpenseType::load',
      ],
      '#disabled' => !$pm_expense_type
        ->isNew(),
    ];

    /* You will need additional form elements for your custom properties. */
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $pm_expense_type = $this->entity;
    $status = $pm_expense_type
      ->save();
    switch ($status) {
      case SAVED_NEW:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Created the %label Expense type.', [
          '%label' => $pm_expense_type
            ->label(),
        ]));
        break;
      default:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label Expense type.', [
          '%label' => $pm_expense_type
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($pm_expense_type
      ->toUrl('collection'));
  }

}

Classes

Namesort descending Description
ExpenseTypeForm Provides a form for editing a Expense type.