I make some query for views filter for nodes. Use in hook_views_query_alter.

$definition = [
   'table' => 'node__field_characs',
   'field' => 'field_characs_target_id',
   'left_table' => 'taxonomy_index',
   'left_field' => 'tid',
   'operator' => '='
$join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $definition);
$rel = $query->addRelationship('node__field_characs', $join, 'taxonomy_index');
$query->addTable('node__field_characs', $rel, $join, 'node__field_characs');

I add array of conditions.

$q_and = db_and();
foreach($ch_id as $key => $val_array){
   $q_and->condition('node__field_characs.field_characs_target_id', $val_array, 'IN');
$query->addWhere(0, $q_and);

But if array $ch_id have more then 1 elements, it works wrong. It does not show any nodes.

A similar problem is described here Perform a query with an entity field condition with multiple values. But how can this be applied in this case?


you try this code:

$and = $query->andConditionGroup();
foreach($ch_id as $key => $val_array){
   $and->condition('node__field_characs.field_characs_target_id', $val_array, 'IN');


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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