You are here

function _tvi_settings_form in Taxonomy Views Integrator 8

Helper function to construct the forms for both term and vocab edit forms.

Parameters

array $form: Settings form render array.

\Drupal\Core\Form\FormStateInterface $form_state: Submitted form state.

2 calls to _tvi_settings_form()
tvi_form_taxonomy_term_form_alter in ./tvi.module
Implements hook_form_BASE_FORM_ID_alter().
tvi_form_taxonomy_vocabulary_form_alter in ./tvi.module
Implements hook_form_BASE_FORM_ID_alter().

File

./tvi.module, line 38
Allow views to be used instead of default taxonomy term page behavior.

Code

function _tvi_settings_form(array &$form, FormStateInterface $form_state) {
  $entity = $form_state
    ->getBuildInfo()['callback_object']
    ->getEntity();
  $entity_type = $entity
    ->getEntityType()
    ->id();
  $config = Drupal::configFactory()
    ->getEditable('tvi.' . $entity_type . '.' . $entity
    ->id());
  $values = $form_state
    ->getValues();
  $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['tvi_view'])) {
    $display_options += tvi_get_view_displays($values['tvi_view']);
  }
  elseif ($config !== NULL) {
    $view = $config
      ->get('view');
    $view_display = $config
      ->get('view_display');
    if (isset($view)) {
      $display_options += tvi_get_view_displays($view);
    }
    if (isset($view_display)) {
      $default_display = $view_display;
    }
  }
  $form['tvi'] = [
    '#type' => 'details',
    '#title' => t('Taxonomy Views Integrator Settings'),
    '#open' => TRUE,
  ];
  $form['tvi']['tvi_enable_override'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable taxonomy views integrator to override presentation.'),
    '#default_value' => $config
      ->get('enable_override'),
  ];
  $form['tvi']['tvi_view'] = [
    '#type' => 'select',
    '#title' => 'Using the view',
    '#default_value' => $config
      ->get('view'),
    '#options' => $view_options,
    '#states' => [
      'visible' => [
        ':input[name="tvi_enable_override"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#ajax' => [
      'callback' => 'tvi_view_display_ajax_handler',
      'event' => 'change',
      'wrapper' => 'tvi-view-display',
      'progress' => [
        'type' => 'throbber',
      ],
    ],
  ];
  $form['tvi']['tvi_view_display'] = [
    '#type' => 'select',
    '#title' => 'With this view display',
    '#description' => t('The view display that you want to use from the selected view.'),
    '#default_value' => $default_display,
    '#options' => $display_options,
    '#states' => [
      'visible' => [
        ':input[name="tvi_enable_override"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#prefix' => '<div id="tvi-view-display">',
    '#suffix' => '</div>',
  ];
  $form['tvi']['tvi_inherit_settings'] = [
    '#id' => 'tvi-inherit-check',
    '#type' => 'checkbox',
    '#title' => t('Child terms will use these settings.'),
    '#default_value' => $config
      ->get('inherit_settings'),
    '#description' => t('Checking this field will allow you to define a view used by the current term and display on all of its children, unless they have their own settings configured.'),
    '#states' => [
      'disabled' => [
        ':input[name="tvi_enable_override"]' => [
          'checked' => FALSE,
        ],
      ],
      'visible' => [
        ':input[name="tvi_enable_override"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['tvi']['tvi_pass_arguments'] = [
    '#id' => 'tvi-pass-arguments',
    '#type' => 'checkbox',
    '#title' => t('Pass all arguments to views.'),
    '#description' => t('Enable this checkbox, and your views display will receive all arguments going after /taxonomy/term/ in the path. If disabled, your views display will only receive tid, and tid_depth.'),
    '#default_value' => $config
      ->get('pass_arguments'),
    '#states' => [
      'disabled' => [
        ':input[name="tvi_enable_override"]' => [
          'checked' => FALSE,
        ],
      ],
      'visible' => [
        ':input[name="tvi_enable_override"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if ($entity_type == 'taxonomy_term') {
    $form['tvi']['#description'] = t('Override the default presentation this term page.');
    $form['tvi']['tvi_view']['#description'] = t('The default view to use for this term page.');
  }
  if ($entity_type == 'taxonomy_vocabulary') {
    $form['tvi']['#description'] = t('Override the default presentation for terms in this vocabulary.');
    $form['tvi']['tvi_view']['#description'] = t('The default view to use for this term page.');
  }
  $form['#validate'][] = 'tvi_form_config_validate';
  $form['actions']['submit']['#submit'][] = 'tvi_submit_handler';
}