You are here

protected function MediaDirectory::valueForm in Media Directories 8

Same name and namespace in other branches
  1. 3.x src/Plugin/views/filter/MediaDirectory.php \Drupal\media_directories\Plugin\views\filter\MediaDirectory::valueForm()
  2. 2.x src/Plugin/views/filter/MediaDirectory.php \Drupal\media_directories\Plugin\views\filter\MediaDirectory::valueForm()

Options form subform for setting options.

This should be overridden by all child classes and it must define $form['value']

Overrides ManyToOne::valueForm

See also

buildOptionsForm()

File

src/Plugin/views/filter/MediaDirectory.php, line 105

Class

MediaDirectory
Filter media by directory.

Namespace

Drupal\media_directories\Plugin\views\filter

Code

protected function valueForm(&$form, FormStateInterface $form_state) {
  $config = $this->configFactory
    ->get('media_directories.settings');
  $vid = $config
    ->get('directory_taxonomy');
  if (empty($vid)) {
    $settings_url = Url::fromRoute('media_directories.config_form');
    $form['markup'] = [
      '#markup' => '<div class="js-form-item form-item">' . $this
        ->t('Vocabulary is not selected. Please select it in the <a href=":url">settings</a>.', [
        ':url' => $settings_url
          ->toString(),
      ]) . '</div>',
    ];
    $form['value'] = [
      '#type' => 'hidden',
      '#default_value' => MEDIA_DIRECTORY_ROOT,
    ];
    return;
  }
  $vocabulary = $this->vocabularyStorage
    ->load($vid);
  $tree = $this->termStorage
    ->loadTree($vocabulary
    ->id(), 0, NULL, TRUE);
  $options = [];
  if ($tree) {
    foreach ($tree as $term) {
      $choice = new \stdClass();
      $choice->option = [
        $term
          ->id() => str_repeat('−', $term->depth + 1) . ' ' . $this->entityRepository
          ->getTranslationFromContext($term)
          ->label(),
      ];
      $options[] = $choice;
    }
  }
  $default_value = (array) $this->value;
  if ($exposed = $form_state
    ->get('exposed')) {
    $identifier = $this->options['expose']['identifier'];
    if (!empty($this->options['expose']['reduce'])) {
      $options = $this
        ->reduceValueOptions($options);
      if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
        $default_value = [];
      }
    }
    if (empty($this->options['expose']['multiple'])) {
      if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
        $default_value = 'All';
      }
      elseif (empty($default_value)) {
        $keys = array_keys($options);
        $default_value = array_shift($keys);
      }
      elseif ($default_value == [
        '',
      ]) {
        $default_value = 'All';
      }
      else {
        $copy = $default_value;
        $default_value = array_shift($copy);
      }
    }
  }
  $form['value'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select terms from vocabulary @voc', [
      '@voc' => $vocabulary
        ->label(),
    ]),
    '#multiple' => TRUE,
    '#options' => $options,
    '#size' => min(9, count($options)),
    '#default_value' => $default_value,
  ];
  $user_input = $form_state
    ->getUserInput();
  if ($exposed && isset($identifier) && !isset($user_input[$identifier])) {
    $user_input[$identifier] = $default_value;
    $form_state
      ->setUserInput($user_input);
  }
  if (!$form_state
    ->get('exposed')) {

    // Retain the helper option.
    $this->helper
      ->buildOptionsForm($form, $form_state);

    // Show help text if not exposed to end users.
    $form['value']['#description'] = t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".');
  }
}