You are here

public function Section::query in Workbench Access 8

Same name in this branch
  1. 8 src/Plugin/views/filter/Section.php \Drupal\workbench_access\Plugin\views\filter\Section::query()
  2. 8 src/Plugin/views/field/Section.php \Drupal\workbench_access\Plugin\views\field\Section::query()

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides InOperator::query

File

src/Plugin/views/filter/Section.php, line 254

Class

Section
Filter by assigned section.

Namespace

Drupal\workbench_access\Plugin\views\filter

Code

public function query() {
  $helper = new ManyToOneHelper($this);

  // The 'All' selection must be filtered by user sections.
  if (empty($this->value) || strtolower(current($this->value)) == 'all') {
    if ($this->manager
      ->userInAll($this->scheme)) {
      return;
    }
    else {

      // This method will get all user sections and children.
      $values = $this->userSectionStorage
        ->getUserSections($this->scheme);
    }
  }
  if (!empty($this->table)) {
    $alias = $this->query
      ->ensureTable($this->table);
    foreach ($this->scheme
      ->getAccessScheme()
      ->getViewsJoin($this
      ->getEntityType(), $this->realField, $alias) as $configuration) {

      // Allow subquery JOINs, which Menu uses.
      $type = 'standard';
      if (isset($configuration['left_query'])) {
        $type = 'subquery';
      }
      $join = Views::pluginManager('join')
        ->createInstance($type, $configuration);
      $this->tableAlias = $helper
        ->addTable($join, $configuration['table_alias']);
      $this->realField = $configuration['real_field'];
    }

    // If 'All' was not selected, fetch the query values.
    if (!isset($values)) {
      $values = $this->value;
    }
    if (!empty($this->options['section_filter']['show_hierarchy'])) {
      $values = $this
        ->getChildren($values);
    }

    // @TODO: This is probably correct, because user data is stored with
    // differerent context than entity field data.
    if ($this->table == 'users') {
      $new_values = [];
      $scheme = $this->scheme
        ->getAccessScheme();
      foreach ($values as $id) {
        $section_storage = \Drupal::service('entity_type.manager')
          ->getStorage('section_association');
        if ($association = $section_storage
          ->loadSection($this->scheme
          ->id(), $id)) {
          $new_values[] = $association
            ->id();
        }
      }
      $values = $new_values;
    }

    // If values, add our standard where clause.
    if (!empty($values)) {
      $this->scheme
        ->getAccessScheme()
        ->addWhere($this, $values);
    }
    else {
      $this->query
        ->addWhereExpression($this->options['group'], '1 = 0');
    }
  }
}