You are here

public function MixitupFiltersForm::buildForm in MixItUp Views 8.2

Same name and namespace in other branches
  1. 8 src/Form/MixitupFiltersForm.php \Drupal\mixitup_views\Form\MixitupFiltersForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/MixitupFiltersForm.php, line 54

Class

MixitupFiltersForm
Class MixitupFiltersForm.

Namespace

Drupal\mixitup_views\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $options = []) {
  $filters = $this->mixitupFuncService
    ->getPopulatedFilters();
  if ($filters !== NULL) {
    foreach ($filters as $vid => $terms) {

      // Show only selected vocabularies.
      if ($options['restrict_vocab'] === 1 && !isset($options['restrict_vocab_ids'][$vid])) {
        unset($filters[$vid]);
        continue;
      }

      // If all nodes have just one term tagged, it doesn't make sense
      // to show a term and clear filters link.
      if (\count($terms) < 2) {
        unset($filters[$vid]);
        continue;
      }
      $vocab = Vocabulary::load($vid);
      if ($vocab !== NULL) {
        $name = $vocab
          ->get('name');
        if (isset($options['filter_type'])) {
          switch ($options['filter_type']) {
            case 'checkboxes':
              $form['filter_' . $vid] = [
                '#type' => 'checkboxes',
                '#title' => $name,
                '#options' => $terms,
                '#attributes' => [
                  'class' => [
                    'mixitup_views_filter',
                  ],
                  'vid' => $vid,
                ],
                '#multiple' => TRUE,
              ];
              break;
            case 'select':
              $form['filter_' . $vid] = [
                '#type' => 'select',
                '#title' => $name,
                '#options' => [
                  '' => $this
                    ->t('All'),
                ] + $terms,
                '#attributes' => [
                  'class' => [
                    'mixitup_views_filter',
                  ],
                  'vid' => $vid,
                ],
                '#multiple' => FALSE,
              ];
              break;
            case 'buttons':
              $form['filter_' . $vid] = [
                '#type' => 'fieldset',
                '#title' => $name,
                '#attributes' => [
                  'class' => [
                    'mixitup_filter_wrapper',
                  ],
                ],
              ];
              foreach ([
                '' => $this
                  ->t('All'),
              ] + $terms as $term_id => $term) {
                $form['filter_' . $vid]['filter_' . $term] = [
                  '#type' => 'html_tag',
                  '#tag' => 'button',
                  '#value' => $term,
                  '#attributes' => [
                    'type' => 'button',
                    'data-filter' => $term_id,
                    'class' => [
                      'mixitup_views_filter_buttons',
                    ],
                  ],
                ];
              }
              break;
          }
        }
      }
    }
    if ($filters) {
      $form['reset'] = [
        '#markup' => '<a href="#reset" id="reset">' . $this
          ->t('Reset filters') . '</a>',
      ];
    }
  }
  if (isset($options['use_sort']) && $options['use_sort'] === 1 && isset($options['sorts'])) {
    $form['sort'] = [
      '#theme' => 'mixitup_views_sorting',
      '#sorts' => $options['sorts'],
    ];
  }
  if (isset($options['hide_unchecked_chekboxes']) && $options['hide_unchecked_chekboxes'] === 1) {
    $form['#attached']['drupalSettings']['mixitup_views_filters']['hide_unchecked_checkboxes'] = TRUE;
  }
  else {
    $form['#attached']['drupalSettings']['mixitup_views_filters']['hide_unchecked_checkboxes'] = FALSE;
  }
  return $form;
}