Score:0

EntityQuery to filter values returned based on List(text) field

jp flag

I have a List(text) field that can have multiple values. I am trying to create an entityQuery that returns nodes with specific items are checked within the field. The variable "availableServices" is an array of available services. I tried adding an andconditiongroup but that results in no items being returned. How do I check if a field has multiple values selected?

I've checked and when I search for items separately it returns values, but multiple items returns nothing. The nodes however have multiple items checked so it should return nodes.

if(isset($availableServices)) {
  $and = $query->andConditionGroup();
  foreach($availableServices as $service) {
    $and->condition('field_available_services', $service);
  }
  $query->condition($and);
}

----Updated---

Basically I need to convert this into an entityquery:

SELECT "node_field_data"."created" AS "node_field_data_created", "node_field_data"."nid" AS "nid"
FROM
{node_field_data} "node_field_data"
INNER JOIN {node__field_available_services} "node__field_available_services_value_0" ON node_field_data.nid = node__field_available_services_value_0.entity_id AND node__field_available_services_value_0.field_available_services_value = 'Deposit-taking ATM'
INNER JOIN {node__field_available_services} "node__field_available_services_value_1" ON node_field_data.nid = node__field_available_services_value_1.entity_id AND node__field_available_services_value_1.field_available_services_value = 'Check Cashing'
INNER JOIN {node__field_location_type} "node__field_location_type" ON node_field_data.nid = node__field_location_type.entity_id AND node__field_location_type.deleted = '0'
WHERE ((node__field_location_type.field_location_type_value IN('Branch', 'ATM', 'Branch & ATM'))) AND (("node_field_data"."status" = '1') AND ("node_field_data"."type" IN ('branch_location')) AND (("node__field_available_services_value_0"."field_available_services_value" = 'Deposit-taking ATM') AND ("node__field_available_services_value_1"."field_available_services_value" = 'Check Cashing')))
ORDER BY "node_field_data_created" DESC
apaderno avatar
us flag
Either `$availableServices` is a string or it is an array. You cannot compare an array to a string, nor pass a string to `foreach`.
user1470118 avatar
jp flag
@apaderno It's an array. That's what I said in the comment.
Hodba Khalaf avatar
eg flag
I'm not sure this can be done in one query. Did you try to set a base condition with "IN" condition and then loop through the results to keep only those that have all the specified available services?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.