You are here

public function Populate::access in Views filters populate 8

Because access() on filters are run earlier than the preQuery(), we use that method to add a mock filter array at the end so that the next iteration of filters, will trigger our mocked filter and that filter will take care of removing both itself and the filters that it should populate so that they behave as optional exposed filters (no added where condition).

We couldn't do this on preQuery() - we tried. The reason is that preQuery() is not able to unset filters unless they are in the end, otherwise the interation on the filters array will continue and a preQuery will bne run on a null object.

Overrides HandlerBase::access

See also

\Drupal\views\ViewExecutable::_initHandler()

\Drupal\views\ViewExecutable::build()

File

src/Plugin/views/filter/Populate.php, line 61

Class

Populate
Filter handler which allows to search on multiple fields.

Namespace

Drupal\views_filters_populate\Plugin\views\filter

Code

public function access(AccountInterface $account) {

  // $filters = $this->displayHandler->getHandlers('filter');
  // start with default value
  $value = $this->value;

  // If exposed, take data from input, as this hasn't reached the views
  // object yet
  if ($this->options['exposed']) {
    $input = $this->view
      ->getExposedInput();
    $identifier = $this->options['expose']['identifier'];
    if (isset($input[$identifier])) {
      $value = $input[$identifier];
    }
  }

  // INTERNAL HACK: Add a mock filter to the view's filters array at the end
  // that will take care of removing the filters and itself on the next
  // preQuery run.
  if (empty($value) && $this->options['exposed']) {
    $mock_filter = new PopulateRemoveEmptyFilterMock($this);
    $this->view->filter[] = $mock_filter;
  }
  return parent::access($account);
}