You are here

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

File

modules/pm_timetracking/src/Form/TimeTrackingTypeForm.php
View source
<?php

namespace Drupal\pm_timetracking\Form;

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

/**
 * Provides a form for editing a Timetracking type.
 *
 * @ingroup pm_timetracking
 */
class TimeTrackingTypeForm extends EntityForm {

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

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

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

}

Classes

Namesort descending Description
TimeTrackingTypeForm Provides a form for editing a Timetracking type.