Entity Query Fails with Condition Groups

Created on 6 June 2024, 17 days ago
Updated 11 June 2024, 12 days ago

Problem/Motivation

When attempting to create and execute a query that uses an `orConditionGroup`, the following error is thrown:

TypeError: substr(): Argument #1 ($string) must be of type string, Drupal\civicrm_entity\Entity\Query\CiviCRM\Condition given in substr() (line 40 of .../civicrm_entity/src/Entity/Query/CiviCRM/Query.php).

I've narrowed this down to how an entity query has a condition group structured:

#entityTypeId: "civicrm_action_schedule"
#condition: Drupal\civicrm_entity\Entity\Query\CiviCRM\Condition {
  #conditions: [
    0 => [
      "field" => Drupal\civicrm_entity\Entity\Query\CiviCRM\Condition {
        #conditions: [
          0 => [
            "field" => "body_html"
            "value" => "test value 1"
            "operator" => "CONTAINS"
            "langcode" => null
          ]
          1 => [
            "field" => "body_html"
            "value" => "test value 2"
            "operator" => "CONTAINS"
            "langcode" => null
          ]
        ]
        #conjunction: "or"
        #query: Drupal\civicrm_entity\Entity\Query\CiviCRM\Query {}
        #namespaces: []
      }
      "value" => null
      "operator" => null
      "langcode" => null
    ]
  ]
  #conjunction: "AND"
  #query: Drupal\civicrm_entity\Entity\Query\CiviCRM\Query {}
  #namespaces: []
}

Steps to reproduce

The following code snippet generates the error:

$query = \Drupal::entityQuery('civicrm_action_schedule')->accessCheck();
$group = $query->orConditionGroup()
  ->condition('body_html', 'test value 1', 'CONTAINS')
  ->condition('body_html', 'test value 2', 'CONTAINS');
$query->condition($group)->execute();

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States collinhaines

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @collinhaines
  • πŸ‡ΊπŸ‡ΈUnited States markusa

    Thanks for reporting. The error is a symptom and not a one-line bug.

    This is challenging.
    https://github.com/eileenmcnaughton/civicrm_entity/blob/4.0.x/src/Entity...

    That plugin works for simple things, but when conditions are condition groups, it does not.

    I'm not aware of being able to do condition groups in CiviCRM APIv3

    This maybe possible in a future update when we upgrade CiviCRM Entity to use APIv4, we haven't used APIv4 in this module yet. It maybe possible to attempt in only this plugin, and not modifying the API service, quicker

    I'm calling this a feature request due to the complexity and effort of the solution.

    If you need a solution for your site in the near term, I'd suggest not using the EntityQuery at all, but APIv4 Directly
    https://docs.civicrm.org/dev/en/latest/api/v4/usage/

    \Drupal::service('civicrm')->initialize();
    // APIv4 code here
    
Production build 0.69.0 2024