You are here

protected function WizardPluginBase::defaultDisplayFiltersUser in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php \Drupal\views\Plugin\views\wizard\WizardPluginBase::defaultDisplayFiltersUser()
  2. 9 core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php \Drupal\views\Plugin\views\wizard\WizardPluginBase::defaultDisplayFiltersUser()

Retrieves filter information based on user input for the default display.

Parameters

array $form: The full wizard form array.

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

Return value

array An array of filter arrays keyed by ID. A sort array contains the options accepted by a filter handler.

2 calls to WizardPluginBase::defaultDisplayFiltersUser()
Node::defaultDisplayFiltersUser in core/modules/node/src/Plugin/views/wizard/Node.php
Retrieves filter information based on user input for the default display.
WizardPluginBase::defaultDisplayFilters in core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php
Retrieves all filter information used by the default display.
2 methods override WizardPluginBase::defaultDisplayFiltersUser()
Node::defaultDisplayFiltersUser in core/modules/node/src/Plugin/views/wizard/Node.php
Retrieves filter information based on user input for the default display.
NodeRevision::defaultDisplayFiltersUser in core/modules/node/src/Plugin/views/wizard/NodeRevision.php
Retrieves filter information based on user input for the default display.

File

core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php, line 919

Class

WizardPluginBase
Base class for Views wizard plugins.

Namespace

Drupal\views\Plugin\views\wizard

Code

protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
  $filters = [];
  if (($type = $form_state
    ->getValue([
    'show',
    'type',
  ])) && $type != 'all') {
    $bundle_key = $this->entityType
      ->getKey('bundle');

    // Figure out the table where $bundle_key lives. It may not be the same as
    // the base table for the view; the taxonomy vocabulary machine_name, for
    // example, is stored in taxonomy_vocabulary, not taxonomy_term_data.
    \Drupal::moduleHandler()
      ->loadInclude('views_ui', 'inc', 'admin');
    $fields = Views::viewsDataHelper()
      ->fetchFields($this->base_table, 'filter');
    $table = FALSE;
    if (isset($fields[$this->base_table . '.' . $bundle_key])) {
      $table = $this->base_table;
    }
    else {
      foreach ($fields as $field_name => $value) {
        if ($pos = strpos($field_name, '.' . $bundle_key)) {
          $table = substr($field_name, 0, $pos);
          break;
        }
      }
    }

    // Some entities have bundles but don't provide their bundle data on the
    // base table. In this case the entities wizard should provide a
    // relationship to the relevant data.
    // @see \Drupal\node\Plugin\views\wizard\NodeRevision
    if (!empty($table)) {
      $table_data = Views::viewsData()
        ->get($table);

      // If the 'in' operator is being used, map the values to an array.
      $handler = $table_data[$bundle_key]['filter']['id'];
      $handler_definition = Views::pluginManager('filter')
        ->getDefinition($handler);
      if ($handler == 'in_operator' || is_subclass_of($handler_definition['class'], 'Drupal\\views\\Plugin\\views\\filter\\InOperator')) {
        $value = [
          $type => $type,
        ];
      }
      else {
        $value = $type;
      }
      $filters[$bundle_key] = [
        'id' => $bundle_key,
        'table' => $table,
        'field' => $bundle_key,
        'value' => $value,
        'entity_type' => $table_data['table']['entity type'] ?? NULL,
        'entity_field' => $table_data[$bundle_key]['entity field'] ?? NULL,
        'plugin_id' => $handler,
      ];
    }
  }
  return $filters;
}