You are here

protected function StatisticsPageTrait::createMonthSelect in Opigno statistics 3.x

Create the month select with AJAX callback.

Parameters

array $data: Rows data to get available months.

int $year_selected: The previously selected year.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

Return value

array Render array to add the months select to the form.

2 calls to StatisticsPageTrait::createMonthSelect()
DashboardForm::buildForm in src/Form/DashboardForm.php
Form constructor.
TrainingForm::buildForm in src/Form/TrainingForm.php
Form constructor.

File

src/StatisticsPageTrait.php, line 494

Class

StatisticsPageTrait
Common helper methods for a statistics pages.

Namespace

Drupal\opigno_statistics

Code

protected function createMonthSelect(array $data, int $year_selected, FormStateInterface $form_state) : array {
  $months = [];
  foreach ($data as $row) {
    $month = $row->month;
    if (!isset($months[$month]) && $row->year == $year_selected) {
      $timestamp = mktime(0, 0, 0, $month, 1);
      $months[$month] = $this->dateFormatter
        ->format($timestamp, 'custom', 'F');
    }
  }
  $max_month = $months ? max(array_keys($months)) : date('n');
  $month = $form_state
    ->getValue('month', $max_month);
  return [
    '#type' => 'select',
    '#title' => $this
      ->t('Month'),
    '#title_display' => 'invisible',
    '#options' => $months,
    '#default_value' => $month,
    '#ajax' => [
      'event' => 'change',
      'callback' => '::updateTrainingProgressAjax',
    ],
    '#attributes' => [
      'class' => [
        'selectpicker',
      ],
    ],
  ];
}