1

How can I configure a Drupal 10 View with a multi-select exposed filter to display teachers who exclusively teach selected subjects?

Currently, the filter shows teachers who teach any of the selected subjects (math, computer science), but I want it to only display teachers who teach all of the selected subjects simultaneously, such as both math and computer science.

I tried change it in hook_views_query_alter(), but it fails.

In hook_views_query_alter() comes an array of subjects.

$selected_subjects = 
$view->exposed_data['field_teacher_technology_target_id'];

$selected_subjects contains [26:'26', 65:'65']; 26 and 65 are the subject IDs.

How can I change the code to get only the accounts who who have both the values?

In view:

WHERE ("node_field_data"."type" IN ('teacher')) AND 
(("paragraph__field_teacher_technology_value_0"."field_teacher_technology_target_id" = '26') AND 
("paragraph__field_teacher_technology_value_1"."field_teacher_technology_target_id" = '65')) 

I also tried change '#conjunction' from 'OR' to 'AND', but it doen't helped.

Please help.

In Default view query I have: SELECT "node_field_data"."title" AS "node_field_data_title", "node__field_teacher_profile_picture"."field_teacher_profile_picture_target_id" AS "node__field_teacher_profile_picture_field_teacher_profile_pict", "node__field_teacher_surname"."field_teacher_surname_value" AS "node__field_teacher_surname_field_teacher_surname_value", "node__field_teacher_phone"."field_teacher_phone_value" AS "node__field_teacher_phone_field_teacher_phone_value", "node__field_teacher_english_level"."field_teacher_english_level_value" AS "node__field_teacher_english_level_field_teacher_english_level_", "node_field_data"."status" AS "node_field_data_status", "node__field_teacher_type_activity"."field_teacher_type_activity_target_id" AS "node__field_teacher_type_activity_field_teacher_type_activity_", "node__field_teacher_status"."field_teacher_status_value" AS "node__field_teacher_status_field_teacher_status_value", "node__field_teacher_agreement"."field_teacher_agreement_value" AS "node__field_teacher_agreement_field_teacher_agreement_value", "paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_currency"."field_currency_target_id" AS "paragraphs_item_field_data_node__field_teacher_fee__paragraph", "paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_fee_price"."field_fee_price_value" AS "paragraphs_item_field_data_node__field_teacher_fee__paragraph_1", "node__field_teacher_working_days_hours"."field_teacher_working_days_hours_value" AS "node__field_teacher_working_days_hours_field_teacher_working_d", "node__field_teacher_hours_per_week"."field_teacher_hours_per_week_value" AS "node__field_teacher_hours_per_week_field_teacher_hours_per_wee", "node__field_teacher_timezone"."field_teacher_timezone_value" AS "node__field_teacher_timezone_field_teacher_timezone_value", "node__field_teacher_timezone_numeric"."field_teacher_timezone_numeric_value" AS "node__field_teacher_timezone_numeric_field_teacher_timezone_nu", MAX(paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_level.field_teacher_level_value) AS "paragraphs_item_field_data_node__field_teacher_technologies__", STDDEV_POP(paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_proven.field_teacher_proven_value) AS "paragraphs_item_field_data_node__field_teacher_technologies___1", MIN(node_field_data.nid) AS "nid_1", MIN(paragraphs_item_field_data_node__field_teacher_technologies.id) AS "paragraphs_item_field_data_node__field_teacher_technologies_i", MIN(paragraphs_item_field_data_node__field_teacher_fee.id) AS "paragraphs_item_field_data_node__field_teacher_fee_id" FROM {node_field_data} "node_field_data" LEFT JOIN {node__field_teacher_technologies} "node__field_teacher_technologies" ON node_field_data.nid = node__field_teacher_technologies.entity_id AND node__field_teacher_technologies.deleted = '0' LEFT JOIN {paragraphs_item_field_data} "paragraphs_item_field_data_node__field_teacher_technologies" ON node__field_teacher_technologies.field_teacher_technologies_target_revision_id = paragraphs_item_field_data_node__field_teacher_technologies.revision_id LEFT JOIN {node__field_teacher_fee} "node__field_teacher_fee" ON node_field_data.nid = node__field_teacher_fee.entity_id AND node__field_teacher_fee.deleted = '0' LEFT JOIN {paragraphs_item_field_data} "paragraphs_item_field_data_node__field_teacher_fee" ON node__field_teacher_fee.field_teacher_fee_target_revision_id = paragraphs_item_field_data_node__field_teacher_fee.revision_id LEFT JOIN {paragraph__field_teacher_technology} "paragraph__field_teacher_technology_value_0" ON paragraphs_item_field_data_node__field_teacher_technologies.id = paragraph__field_teacher_technology_value_0.entity_id AND paragraph__field_teacher_technology_value_0.field_teacher_technology_target_id = '26' LEFT JOIN {paragraph__field_teacher_technology} "paragraph__field_teacher_technology_value_1" ON paragraphs_item_field_data_node__field_teacher_technologies.id = paragraph__field_teacher_technology_value_1.entity_id AND paragraph__field_teacher_technology_value_1.field_teacher_technology_target_id = '65' LEFT JOIN {paragraph__field_fee_price} "paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_fee_price" ON paragraphs_item_field_data_node__field_teacher_fee.id = paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_fee_price.entity_id AND paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_fee_price.deleted = '0' LEFT JOIN {node__field_teacher_timezone_numeric} "node__field_teacher_timezone_numeric" ON node_field_data.nid = node__field_teacher_timezone_numeric.entity_id AND node__field_teacher_timezone_numeric.deleted = '0' LEFT JOIN {node__field_teacher_profile_picture} "node__field_teacher_profile_picture" ON node_field_data.nid = node__field_teacher_profile_picture.entity_id AND node__field_teacher_profile_picture.deleted = '0' LEFT JOIN {node__field_teacher_surname} "node__field_teacher_surname" ON node_field_data.nid = node__field_teacher_surname.entity_id AND node__field_teacher_surname.deleted = '0' LEFT JOIN {node__field_teacher_phone} "node__field_teacher_phone" ON node_field_data.nid = node__field_teacher_phone.entity_id AND node__field_teacher_phone.deleted = '0' LEFT JOIN {node__field_teacher_email} "node__field_teacher_email" ON node_field_data.nid = node__field_teacher_email.entity_id AND node__field_teacher_email.deleted = '0' LEFT JOIN {node__field_teacher_payment_method} "node__field_teacher_payment_method" ON node_field_data.nid = node__field_teacher_payment_method.entity_id AND node__field_teacher_payment_method.deleted = '0' LEFT JOIN {node__field_teacher_english_level} "node__field_teacher_english_level" ON node_field_data.nid = node__field_teacher_english_level.entity_id AND node__field_teacher_english_level.deleted = '0' LEFT JOIN {node__field_teacher_type_activity} "node__field_teacher_type_activity" ON node_field_data.nid = node__field_teacher_type_activity.entity_id AND node__field_teacher_type_activity.deleted = '0' LEFT JOIN {node__field_teacher_status} "node__field_teacher_status" ON node_field_data.nid = node__field_teacher_status.entity_id AND node__field_teacher_status.deleted = '0' LEFT JOIN {node__field_teacher_agreement} "node__field_teacher_agreement" ON node_field_data.nid = node__field_teacher_agreement.entity_id AND node__field_teacher_agreement.deleted = '0' LEFT JOIN {paragraph__field_currency} "paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_currency" ON paragraphs_item_field_data_node__field_teacher_fee.id = paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_currency.entity_id AND paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_currency.deleted = '0' LEFT JOIN {node__field_teacher_working_days_hours} "node__field_teacher_working_days_hours" ON node_field_data.nid = node__field_teacher_working_days_hours.entity_id AND node__field_teacher_working_days_hours.deleted = '0' LEFT JOIN {node__field_teacher_hours_per_week} "node__field_teacher_hours_per_week" ON node_field_data.nid = node__field_teacher_hours_per_week.entity_id AND node__field_teacher_hours_per_week.deleted = '0' LEFT JOIN {node__field_teacher_working_days} "node__field_teacher_working_days" ON node_field_data.nid = node__field_teacher_working_days.entity_id AND node__field_teacher_working_days.deleted = '0' LEFT JOIN {node__field_teacher_timezone} "node__field_teacher_timezone" ON node_field_data.nid = node__field_teacher_timezone.entity_id AND node__field_teacher_timezone.deleted = '0' LEFT JOIN {paragraph__field_teacher_level} "paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_level" ON paragraphs_item_field_data_node__field_teacher_technologies.id = paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_level.entity_id AND paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_level.deleted = '0' LEFT JOIN {paragraph__field_teacher_proven} "paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_proven" ON paragraphs_item_field_data_node__field_teacher_technologies.id = paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_proven.entity_id AND paragraphs_item_field_data_node__field_teacher_technologies__paragraph__field_teacher_proven.deleted = '0' WHERE ("node_field_data"."type" IN ('teacher')) AND (("paragraph__field_teacher_technology_value_0"."field_teacher_technology_target_id" = '26') OR ("paragraph__field_teacher_technology_value_1"."field_teacher_technology_target_id" = '65')) AND ("paragraphs_item_field_data_node__field_teacher_fee__paragraph__field_fee_price"."field_fee_price_value" BETWEEN '0' AND '1000') AND ("node__field_teacher_timezone_numeric"."field_teacher_timezone_numeric_value" BETWEEN '-12' AND '12') GROUP BY "node_field_data"."nid", "node_field_data_title", "node__field_teacher_profile_picture_field_teacher_profile_pict", "node__field_teacher_surname_field_teacher_surname_value", "node__field_teacher_phone_field_teacher_phone_value", "node__field_teacher_english_level_field_teacher_english_level_", "node_field_data_status", "node__field_teacher_type_activity_field_teacher_type_activity_", "node__field_teacher_status_field_teacher_status_value", "node__field_teacher_agreement_field_teacher_agreement_value", "paragraphs_item_field_data_node__field_teacher_fee__paragraph", "paragraphs_item_field_data_node__field_teacher_fee__paragraph_1", "node__field_teacher_working_days_hours_field_teacher_working_d", "node__field_teacher_hours_per_week_field_teacher_hours_per_wee", "node__field_teacher_timezone_field_teacher_timezone_value", "node__field_teacher_timezone_numeric_field_teacher_timezone_nu" ORDER BY "paragraphs_item_field_data_node__field_teacher_technologies__" DESC, "paragraphs_item_field_data_node__field_teacher_technologies___1" DESC, "paragraphs_item_field_data_node__field_teacher_fee__paragraph_1" ASC LIMIT 10 OFFSET 0 It shows all teachers who have technologies 26 or 65. 2 teachers have 26 (one of them have also 65 technology), 1 teacher only have 65.

I changed 'WHERE' condition to (("paragraph__field_teacher_technology_value_0"."field_teacher_technology_target_id" = '26') AND ("paragraph__field_teacher_technology_value_1"."field_teacher_technology_target_id" = '65'))

But now I don't see any teacher. Why? I want see as rezult only teachers who have just 26 AND 65 What I do wrong?

2
  • Posting your code will help to fix your code.
    – Hudri
    Commented Oct 2, 2023 at 12:29
  • Please review. @Hudri Commented Oct 9, 2023 at 9:47

0

Your Answer

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

Browse other questions tagged or ask your own question.