0

We have a Search API View that we need to dynamically include results to the query that where filtered out.

The use case is that the Search Index View shows a filtered list of node but we have a checkbox to "Include National Listings" where nodes that are marked as "National" should show in the results even if the do not meet the current search or facet filters.

The below code is probably on the right track but it filters all results and produces no output:

 if ($view->id() == 'services_search_index'){

    $query->setGroupOperator('OR');
    $query->setWhereGroup('OR', 1);
    $operator = '=';
    $field = 'field_resource_location_type';
    $value = 'National';
    $query->addWhere(1, $field, $value, $operator);

  }

Here's the Views Query output with the above alter added:

Index: services
Keys: NULL
Conditions:
  field_resource_location_type = 'National'
Sorting: administrative_area ASC, title ASC
Options: array (
    'search_api_view' => 'object (Drupal\\views\\ViewExecutable)',
  )
0

1 Answer 1

0

You may be overwriting your original "where" by forcing the index of 1. The method setWhereGroup() will return a unique Where group id if you don't specify one.

if ($view->id() == 'services_search_index') {
  // Accept any where group success.
  $query->setGroupOperator('OR');
  // Get a unique where group id.
  $groupId = $query->setWhereGroup('AND');
  $operator = '=';
  $field = 'field_resource_location_type';
  $value = 'National';
  // Add a new condition group for filtering.
  $query->addWhere($groupId, $field, $value, $operator);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.