public function Condition::compile

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Database/Query/Condition.php \Drupal\Core\Database\Query\Condition::compile()
  2. 8.x drupal/core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::compile()
  3. 8.x drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Condition.php \Drupal\field_sql_storage\Entity\Condition::compile()

Implements \Drupal\Core\Entity\Query\ConditionInterface::compile().

Overrides ConditionInterface::compile

File

drupal/core/lib/Drupal/Core/Config/Entity/Query/Condition.php, line 24
Contains \Drupal\Core\Config\Entity\Query\Condition.

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 = array();
  $condition_groups = array();
  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' : '=';
      }
      $single_conditions[] = $condition;
    }
  }
  $return = array();
  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;
}