function Condition::compile

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  2. 11.x core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  3. 11.x core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  2. 9 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\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()
  5. 8.9.x core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  6. 8.9.x core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  7. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  8. 8.9.x core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()
  9. 10 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  10. 10 core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  11. 10 core/lib/Drupal/Core/Entity/Query/Sql/Condition.php \Drupal\Core\Entity\Query\Sql\Condition::compile()
  12. 10 core/lib/Drupal/Core/Entity/Query/Null/Condition.php \Drupal\Core\Entity\Query\Null\Condition::compile()

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' : '=';
            }
            // Process the value for operator that use it.
            if (!in_array($condition['operator'], [
                'IS NULL',
                'IS NOT NULL',
            ], TRUE)) {
                // 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.