You are here

protected function EntityQueryAlter::mapConditions in Entity API 8

Maps an entity type's access conditions to SQL conditions.

Parameters

\Drupal\entity\QueryAccess\ConditionGroup $conditions: The access conditions.

\Drupal\Core\Database\Query\SelectInterface $query: The SQL query.

bool $nested_inside_or: Whether the access conditions are nested inside an OR condition.

Return value

\Drupal\Core\Database\Query\ConditionInterface The SQL conditions.

1 call to EntityQueryAlter::mapConditions()
EntityQueryAlter::alter in src/QueryAccess/EntityQueryAlter.php
Alters the select query for the given entity type.

File

src/QueryAccess/EntityQueryAlter.php, line 132

Class

EntityQueryAlter
Defines a class for altering entity queries.

Namespace

Drupal\entity\QueryAccess

Code

protected function mapConditions(ConditionGroup $conditions, SelectInterface $query, $nested_inside_or = FALSE) {
  $sql_condition = $query
    ->conditionGroupFactory($conditions
    ->getConjunction());
  $tables = new Tables($query);
  $nested_inside_or = $nested_inside_or || $conditions
    ->getConjunction() == 'OR';
  foreach ($conditions
    ->getConditions() as $condition) {
    if ($condition instanceof ConditionGroup) {
      $nested_sql_conditions = $this
        ->mapConditions($condition, $query, $nested_inside_or);
      $sql_condition
        ->condition($nested_sql_conditions);
    }
    else {

      // Access conditions don't specify a langcode.
      $langcode = NULL;
      $type = $nested_inside_or || $condition
        ->getOperator() === 'IS NULL' ? 'LEFT' : 'INNER';
      $sql_field = $tables
        ->addField($condition
        ->getField(), $type, $langcode);
      $value = $condition
        ->getValue();
      $operator = $condition
        ->getOperator();

      // Using LIKE/NOT LIKE ensures a case insensitive comparison.
      // @see \Drupal\Core\Entity\Query\Sql\Condition::translateCondition().
      $case_sensitive = $tables
        ->isFieldCaseSensitive($condition
        ->getField());
      $operator_map = [
        '=' => 'LIKE',
        '<>' => 'NOT LIKE',
      ];
      if ($case_sensitive === FALSE && isset($operator_map[$operator])) {
        $operator = $operator_map[$operator];
        $value = $query
          ->escapeLike($value);
      }
      $sql_condition
        ->condition($sql_field, $value, $operator);
    }
  }
  return $sql_condition;
}