You are here

protected function EntityReferenceFilterViewResult::valueForm in Views Reference Filter 8

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/EntityReferenceFilterViewResult.php, line 420

Class

EntityReferenceFilterViewResult
Filter by entity id using items got from the another view..

Namespace

Drupal\entityreference_filter\Plugin\views\filter

Code

protected function valueForm(&$form, FormStateInterface $form_state) {
  $values = $this
    ->getValueOptions();
  $exposed = $form_state
    ->get('exposed');
  $filter_is_required = $this->options['expose']['required'];
  $filter_is_multiple = $this->options['expose']['multiple'];
  $filter_empty_hide = $this->options['hide_empty_filter'];

  // Autocomplete widget.
  if ($this->options['type'] === 'textfield') {

    // @todo autocomplete widget support
  }
  else {
    $default_value = (array) $this->value;

    // Run time.
    if ($exposed) {
      $identifier = $this->options['expose']['identifier'];
      $user_input = $form_state
        ->getUserInput();

      // Dynamic dependent filters.
      if (isset($this->options['reference_arguments']) && strpos($this->options['reference_arguments'], '[') !== FALSE) {

        // This filter depends on other filters dynamically,
        // store data for configuring drupalSettings and attach the library.
        $form['#attached'] = [
          'library' => [
            'entityreference_filter/entityreference_filter',
          ],
        ];

        // Build js settings on every form rebuild.
        $form['#after_build'][] = [
          $this,
          'afterBuild',
        ];
      }

      // Delete irrelevant default values.
      $default_value = $user_input[$identifier] ?? [];
      if (!is_array($default_value)) {
        $default_value = [
          $default_value,
        ];
      }
      $default_value = array_intersect($default_value, array_keys($values));

      // Single filter selection, recalculate default value.
      if (!$filter_is_multiple) {
        if (!$filter_is_required && empty($default_value)) {
          $default_value = 'All';
        }
        elseif (empty($default_value)) {
          $keys = array_keys($values);
          $default_value = array_shift($keys);
        }
        else {
          $copy = $default_value;
          $default_value = array_shift($copy);
        }
      }
    }
    $form['value'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Reference filter'),
      '#multiple' => TRUE,
      '#options' => $values,
      '#size' => min(9, count($values)),
      '#default_value' => $default_value,
    ];

    // Set user input.
    if ($exposed && isset($identifier)) {
      $user_input[$identifier] = $default_value;
      $form_state
        ->setUserInput($user_input);
    }
  }

  // Hide filter with empty options.
  if (empty($values) && $exposed && $filter_empty_hide) {
    $form['value']['#prefix'] = '<div class="hidden">';
    $form['value']['#suffix'] = '</div>';
  }

  // Views UI. Options form.
  if (!$exposed) {
    $options = [];
    $views_ids = [];

    // Don't show value selection widget.
    $form['value']['#access'] = FALSE;
    $filter_base_table = !empty($this->definition['filter_base_table']) ? $this->definition['filter_base_table'] : NULL;

    // Filter views that list the entity type we want and group the separate
    // displays by view.
    if ($filter_base_table) {
      $views_ids = $this->viewStorage
        ->getQuery()
        ->condition('status', TRUE)
        ->condition('display.*.display_plugin', 'entity_reference')
        ->condition('base_table', $filter_base_table)
        ->execute();
    }
    foreach ($this->viewStorage
      ->loadMultiple($views_ids) as $view) {

      // Check each display to see if it meets the criteria and it is enabled.
      foreach ($view
        ->get('display') as $display_id => $display) {

        // If the key doesn't exist, enabled is assumed.
        $enabled = !empty($display['display_options']['enabled']) || !array_key_exists('enabled', $display['display_options']);
        if ($enabled && $display['display_plugin'] === 'entity_reference') {
          $options[$view
            ->id() . ':' . $display_id] = $view
            ->label() . ' - ' . $display['display_title'];
        }
      }
    }
    $show_reference_arguments_field = TRUE;
    $description = '<p>' . $this
      ->t('Choose the view and display that select the entities that can be referenced. Only views with a display of type "Entity Reference" are eligible.') . '</p>';
    if (empty($options)) {
      $options = [
        $this->options['reference_display'] => $this
          ->t('None'),
      ] + $options;
      $warning = '<em>' . $this
        ->t('No views to use. At first, create a view display type "Entity Reference" with the same entity type as default filter values.') . '</em>';
      $description = $warning;
      $show_reference_arguments_field = FALSE;
    }
    $form['reference_display'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('View used to select the entities'),
      '#required' => TRUE,
      '#options' => $options,
      '#default_value' => $this->options['reference_display'],
      '#description' => $description,
    ];
    if (empty($this->options['reference_display'])) {
      $form['reference_display']['#description'] .= '<p>' . $this
        ->t('Entity list will be available after saving this setting.') . '</p>';
    }
    $form['reference_arguments'] = [
      '#type' => 'textfield',
      '#size' => 50,
      '#maxlength' => 256,
      '#title' => $this
        ->t('Arguments for the view'),
      '#default_value' => $this->options['reference_arguments'],
      '#description' => $this
        ->t('Define arguments to send them to the selected entity reference view, they are received as contextual filter values in the same order.
        Format is arg1/arg2/...argN starting from position 1. Possible arguments types are:') . '<br>' . $this
        ->t('!n - argument number n of the view dynamic URL argument %') . '<br>' . $this
        ->t('#n - argument number n of the contextual filter value') . '<br>' . $this
        ->t('[filter_identifier] - `Filter identifier` of the named exposed filter') . '<br>' . $this
        ->t('and other strings are passed as is.'),
      '#access' => $show_reference_arguments_field,
    ];
    $this->helper
      ->buildOptionsForm($form, $form_state);
  }
}