Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
51 / 51 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| DateFilterForm | |
100.00% |
51 / 51 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getFormId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| buildForm | |
100.00% |
40 / 40 |
|
100.00% |
1 / 1 |
1 | |||
| submitForm | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 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) { |
| 40 | $this->dateFormatter = $date_formatter; |
| 41 | $this->dateRangeService = $date_range_service; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * {@inheritdoc} |
| 46 | */ |
| 47 | public static function create(ContainerInterface $container): DateFilterForm { |
| 48 | return new static( |
| 49 | $container->get('date.formatter'), |
| 50 | $container->get('visitors.date_range') |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritdoc} |
| 56 | */ |
| 57 | public function getFormId() { |
| 58 | return 'visitors_date_filter_form'; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * {@inheritdoc} |
| 63 | */ |
| 64 | public function buildForm(array $form, FormStateInterface $form_state) { |
| 65 | |
| 66 | $form = []; |
| 67 | |
| 68 | $form['visitors_date_filter'] = [ |
| 69 | '#collapsible' => TRUE, |
| 70 | '#title' => $this->dateRangeService->getSummary(), |
| 71 | '#type' => 'details', |
| 72 | '#open' => FALSE, |
| 73 | ]; |
| 74 | |
| 75 | $form['visitors_date_filter']['from'] = [ |
| 76 | '#title' => $this->t('From'), |
| 77 | '#type' => 'date', |
| 78 | '#default_value' => $this->dateRangeService->getStartDate(), |
| 79 | ]; |
| 80 | |
| 81 | $form['visitors_date_filter']['to'] = [ |
| 82 | '#title' => $this->t('To'), |
| 83 | '#type' => 'date', |
| 84 | '#default_value' => $this->dateRangeService->getEndDate(), |
| 85 | '#states' => [ |
| 86 | 'visible' => [ |
| 87 | ':input[name="period"]' => ['value' => 'range'], |
| 88 | ], |
| 89 | ], |
| 90 | ]; |
| 91 | |
| 92 | $form['visitors_date_filter']['period'] = [ |
| 93 | '#type' => 'radios', |
| 94 | '#title' => $this->t('Period'), |
| 95 | '#options' => [ |
| 96 | 'day' => $this->t('Day'), |
| 97 | 'week' => $this->t('Week'), |
| 98 | 'month' => $this->t('Month'), |
| 99 | 'year' => $this->t('Year'), |
| 100 | 'range' => $this->t('Range'), |
| 101 | ], |
| 102 | '#default_value' => $this->dateRangeService->getPeriod(), |
| 103 | ]; |
| 104 | |
| 105 | $form['visitors_date_filter']['submit'] = [ |
| 106 | '#type' => 'submit', |
| 107 | '#button_type' => 'primary', |
| 108 | '#value' => $this->t('Apply'), |
| 109 | ]; |
| 110 | |
| 111 | return $form; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * {@inheritdoc} |
| 116 | */ |
| 117 | public function submitForm(array &$form, FormStateInterface $form_state) { |
| 118 | $period = $form_state->getValue('period'); |
| 119 | $from = $form_state->getValue('from'); |
| 120 | $to = $form_state->getValue('to'); |
| 121 | |
| 122 | $this->dateRangeService->setPeriodAndDates($period, $from, $to); |
| 123 | } |
| 124 | |
| 125 | } |