Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_query_alter().
 */
function your_module_name_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'your_view_id') {
    // Get the user-selected values for the Country and City filters.
    $country_filter = $view->exposed_raw_input['country_filter'];
    $city_filter = $view->exposed_raw_input['city_filter'];

    // Create an OR condition group to filter based on either the Country or City field.
    $or_condition_group = $query->orConditionGroup();

    // Add conditions for the Country and City fields to the OR group.
    $or_condition_group->condition('node__field_country.field_country_value', $country_filter, 'CONTAINS');
    $or_condition_group->condition('node__field_city.field_city_value', $city_filter, 'CONTAINS');

    // Add the OR condition group to the query.
    $query->condition($or_condition_group);
  }
}


---------------------------------------------------------

use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_query_alter().
 */
function your_module_name_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'your_view_id') {
    // Get the user-selected values for the Country and City filters.
    $country_filter = $view->exposed_raw_input['country_filter'];
    $city_filter = $view->exposed_raw_input['city_filter'];

    // Create an OR condition to filter based on either the Country or City field.
    $query->addWhereExpression('OR', "(node__field_country.field_country_value LIKE :country_filter ESCAPE '\\\\' OR node__field_city.field_city_value LIKE :city_filter ESCAPE '\\\\')", [':country_filter' => "%$country_filter%", ':city_filter' => "%$city_filter%"]);
  }
}