You are here

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

Create the year select with AJAX callback.

Parameters

array $data: Rows data to get available years.

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

Return value

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

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

File

src/StatisticsPageTrait.php, line 453

Class

StatisticsPageTrait
Common helper methods for a statistics pages.

Namespace

Drupal\opigno_statistics

Code

protected function createYearSelect(array $data, FormStateInterface $form_state) : array {
  $years = [];
  foreach ($data as $row) {
    $year = $row->year;
    if (!isset($years[$year])) {
      $years[$year] = $year;
    }
  }
  $max_year = $years ? max(array_keys($years)) : date('Y');
  $year_selected = $form_state
    ->getValue('year', $max_year);
  return [
    '#type' => 'select',
    '#title' => $this
      ->t('Year'),
    '#title_display' => 'invisible',
    '#options' => $years,
    '#default_value' => $year_selected,
    '#ajax' => [
      'event' => 'change',
      'callback' => '::updateTrainingProgressAjax',
    ],
    '#attributes' => [
      'class' => [
        'selectpicker',
      ],
    ],
  ];
}