You are here

public function TaxonomyViewsIntegratorSettingsForm::buildForm in Taxonomy Views Integrator 8

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 ConfigFormBase::buildForm

File

src/Form/TaxonomyViewsIntegratorSettingsForm.php, line 71

Class

TaxonomyViewsIntegratorSettingsForm
TVI global settings form.

Namespace

Drupal\tvi\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $config = $this
    ->config('tvi.global_settings');
  $views = Views::getEnabledViews();
  $view_options = [
    '' => ' - Select a View -',
  ];
  $display_options = [
    '' => ' - Select a View Display -',
  ];
  $default_display = '';
  foreach ($views as $view) {
    $view_options[$view
      ->id()] = $view
      ->label();
  }
  if (isset($values['view'])) {
    $display_options += $this
      ->getViewDisplayOptions($values['view']);
  }
  elseif ($config !== NULL) {
    $view = $config
      ->get('view');
    $view_display = $config
      ->get('view_display');
    if (isset($view)) {
      $display_options += $this
        ->getViewDisplayOptions($view);
    }
    if (isset($view_display)) {
      $default_display = $view_display;
    }
  }
  $form['#prefix'] = '<div id="tvi-settings-wrapper">';
  $form['#suffix'] = '</div>';
  $form['tvi'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Global settings'),
    '#open' => TRUE,
    '#description' => $this
      ->t('By enabling taxonomy views integration here, it will apply to all vocabularies and their terms by default.'),
  ];
  $form['tvi']['disable_default_view'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Don't display a view by default."),
    '#description' => $this
      ->t('Checking this field will enable the use of the selected view when displaying this taxonomy page.'),
    '#default_value' => $config
      ->get('disable_default_view'),
  ];
  $form['tvi']['enable_override'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use global view override.'),
    '#description' => $this
      ->t('Checking this field will enable the use of the selected view when displaying this taxonomy page.'),
    '#default_value' => $config
      ->get('enable_override'),
    '#states' => [
      'visible' => [
        ':input[name="disable_default_view"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['tvi']['view'] = [
    '#type' => 'select',
    '#title' => 'Using the view',
    '#description' => $this
      ->t('The default view that you want to use for all vocabularies and terms.'),
    '#default_value' => $config
      ->get('view'),
    '#options' => $view_options,
    '#states' => [
      'visible' => [
        ':input[name="enable_override"]' => [
          'checked' => TRUE,
        ],
        ':input[name="disable_default_view"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
    '#ajax' => [
      'callback' => '::loadDisplayOptions',
      'event' => 'change',
      'wrapper' => 'tvi-settings-wrapper',
      'progress' => [
        'type' => 'throbber',
      ],
    ],
  ];
  $form['tvi']['view_display'] = [
    '#type' => 'select',
    '#title' => 'With this view display',
    '#description' => $this
      ->t('The view display that you want to use from the selected view.'),
    '#default_value' => $default_display,
    '#options' => $display_options,
    '#states' => [
      'visible' => [
        ':input[name="enable_override"]' => [
          'checked' => TRUE,
        ],
        ':input[name="disable_default_view"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
    '#prefix' => '<div id="tvi-view-display">',
    '#suffix' => '</div>',
  ];
  return parent::buildForm($form, $form_state);
}