Coverage index

src/Form/DateFilterForm.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Form;
4
5
use Drupal\Core\Datetime\DateFormatterInterface;
6
use Drupal\Core\Form\FormBase;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\visitors\Service\DateRangeService;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Date Filter form.
13
 */
14
class DateFilterForm extends FormBase {
15
16
17
  /**
18
   * The date service.
19
   *
20
   * @var \Drupal\Core\Datetime\DateFormatterInterface
21
   */
22
  protected $dateFormatter;
23
24
  /**
25
   * The date range service.
26
   *
27
   * @var \Drupal\visitors\Service\DateRangeService
28
   */
29
  protected $dateRangeService;
30
31
  /**
32
   * Date Filter Form constructor.
33
   *
34
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
35
   *   The date formatter service.
36
   * @param \Drupal\visitors\Service\DateRangeService $date_range_service
37
   *   The date range service.
38
   */
39
  public function __construct(DateFormatterInterface $date_formatter, DateRangeService $date_range_service) {
405
    $this->dateFormatter = $date_formatter;
415
    $this->dateRangeService = $date_range_service;
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public static function create(ContainerInterface $container): DateFilterForm {
485
    return new static(
495
      $container->get('date.formatter'),
505
      $container->get('visitors.date_range')
515
    );
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public function getFormId() {
582
    return 'visitors_date_filter_form';
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function buildForm(array $form, FormStateInterface $form_state) {
65
662
    $form = [];
67
682
    $form['visitors_date_filter'] = [
692
      '#collapsible' => TRUE,
702
      '#title' => $this->dateRangeService->getSummary(),
712
      '#type' => 'details',
722
      '#open' => FALSE,
732
    ];
74
752
    $form['visitors_date_filter']['from'] = [
762
      '#title' => $this->t('From'),
772
      '#type' => 'date',
782
      '#default_value' => $this->dateRangeService->getStartDate(),
792
    ];
80
812
    $form['visitors_date_filter']['to'] = [
822
      '#title'            => $this->t('To'),
832
      '#type' => 'date',
842
      '#default_value'    => $this->dateRangeService->getEndDate(),
852
      '#states' => [
862
        'visible' => [
872
          ':input[name="period"]' => ['value' => 'range'],
882
        ],
892
      ],
902
    ];
91
922
    $form['visitors_date_filter']['period'] = [
932
      '#type' => 'radios',
942
      '#title' => $this->t('Period'),
952
      '#options' => [
962
        'day' => $this->t('Day'),
972
        'week' => $this->t('Week'),
982
        'month' => $this->t('Month'),
992
        'year' => $this->t('Year'),
1002
        'range' => $this->t('Range'),
1012
      ],
1022
      '#default_value' => $this->dateRangeService->getPeriod(),
1032
    ];
104
1052
    $form['visitors_date_filter']['submit'] = [
1062
      '#type' => 'submit',
1072
      '#button_type' => 'primary',
1082
      '#value' => $this->t('Apply'),
1092
    ];
110
1112
    return $form;
112
  }
113
114
  /**
115
   * {@inheritdoc}
116
   */
117
  public function submitForm(array &$form, FormStateInterface $form_state) {
1181
    $period = $form_state->getValue('period');
1191
    $from = $form_state->getValue('from');
1201
    $to = $form_state->getValue('to');
121
1221
    $this->dateRangeService->setPeriodAndDates($period, $from, $to);
123
  }
124
125
}