work_time-1.0.x-dev/src/Form/WorkTimeConfigurationForm.php

src/Form/WorkTimeConfigurationForm.php
<?php

namespace Drupal\work_time\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configuration form for a work time entity type.
 */
class WorkTimeConfigurationForm extends ConfigFormBase {

  /**
   * Name of the config.
   *
   * @var string
   */
  public static $configName = 'work_time.settings';

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [self::$configName];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'work_time_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $config = $this->config(self::$configName);
    $form['holidays'] = [
      '#title' => $this->t('General Holidays'),
      '#description' => $this->t('Enter holidays separated by, Example "01/01,01/05,09/04/2023,... Format d/m or d/m/Y"'),
      '#type' => 'textarea',
      '#default_value' => $config->get('holidays'),
    ];
    $form['hours'] = [
      '#title' => $this->t('Total hours per day'),
      '#description' => $this->t('To defined overtime, Example: 8 (hours a day)'),
      '#type' => 'number',
      '#default_value' => $config->get('hours'),
    ];
    $form['filter'] = [
      '#title' => $this->t('Filter mode'),
      '#description' => $this->t('Filter default with'),
      '#type' => 'select',
      '#options' => ['week' => $this->t('Week'), 'month' => $this->t('Month')],
      '#default_value' => $config->get('filter'),
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Retrieve the configuration.
    $this->config(self::$configName)
      // Set the submitted configuration setting.
      ->set('holidays', $form_state->getValue('holidays'))
      ->set('hours', $form_state->getValue('hours'))
      ->set('filter', $form_state->getValue('filter'))
      ->save();
    $this->messenger()
      ->addStatus($this->t('The configuration has been updated.'));
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc