You are here

public function Condition::compile in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  2. 9 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  3. 9 core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  4. 9 core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()

Compiles this conditional clause.

Parameters

$query: The query object this conditional clause belongs to.

Overrides ConditionInterface::compile

File

core/lib/Drupal/Core/Config/Entity/Query/Condition.php, line 19

Class

Condition
Defines the condition class for the config entity query.

Namespace

Drupal\Core\Config\Entity\Query

Code

public function compile($configs) {
  $and = strtoupper($this->conjunction) == 'AND';
  $single_conditions = [];
  $condition_groups = [];
  foreach ($this->conditions as $condition) {
    if ($condition['field'] instanceof ConditionInterface) {
      $condition_groups[] = $condition;
    }
    else {
      if (!isset($condition['operator'])) {
        $condition['operator'] = is_array($condition['value']) ? 'IN' : '=';
      }

      // Lowercase condition value(s) for case-insensitive matches.
      if (is_array($condition['value'])) {
        $condition['value'] = array_map('mb_strtolower', $condition['value']);
      }
      elseif (!is_bool($condition['value'])) {
        $condition['value'] = mb_strtolower($condition['value']);
      }
      $single_conditions[] = $condition;
    }
  }
  $return = [];
  if ($single_conditions) {
    foreach ($configs as $config_name => $config) {
      foreach ($single_conditions as $condition) {
        $match = $this
          ->matchArray($condition, $config, explode('.', $condition['field']));

        // If AND and it's not matching, then the rest of conditions do not
        // matter and this config object does not match.
        // If OR and it is matching, then the rest of conditions do not
        // matter and this config object does match.
        if ($and != $match) {
          break;
        }
      }
      if ($match) {
        $return[$config_name] = $config;
      }
    }
  }
  elseif (!$condition_groups || $and) {

    // If there were no single conditions then either:
    // - Complex conditions, OR: need to start from no entities.
    // - Complex conditions, AND: need to start from all entities.
    // - No complex conditions (AND/OR doesn't matter): need to return all
    //   entities.
    $return = $configs;
  }
  foreach ($condition_groups as $condition) {
    $group_entities = $condition['field']
      ->compile($configs);
    if ($and) {
      $return = array_intersect_key($return, $group_entities);
    }
    else {
      $return = $return + $group_entities;
    }
  }
  return $return;
}