EntityQueryAlter.php in Entity API 8
File
src/QueryAccess/EntityQueryAlter.php
View source
<?php
namespace Drupal\entity\QueryAccess;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\Sql\Tables;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class EntityQueryAlter implements ContainerInjectionInterface {
protected $entityFieldManager;
protected $entityTypeManager;
protected $renderer;
protected $requestStack;
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, RequestStack $request_stack) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'), $container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('request_stack'));
}
public function alter(SelectInterface $query, EntityTypeInterface $entity_type) {
if (!$entity_type
->hasHandlerClass('query_access')) {
return;
}
$entity_type_id = $entity_type
->id();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
if (!$storage instanceof SqlContentEntityStorage) {
return;
}
$query_access = $this->entityTypeManager
->getHandler($entity_type_id, 'query_access');
$conditions = $query_access
->getConditions($query
->getMetaData('op') ?: 'view');
if ($conditions
->isAlwaysFalse()) {
$query
->where('1 = 0');
}
elseif (count($conditions)) {
$sql_conditions = $this
->mapConditions($conditions, $query);
$query
->condition($sql_conditions);
}
$this
->applyCacheability(CacheableMetadata::createFromObject($conditions));
}
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 {
$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();
$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;
}
protected function applyCacheability(CacheableMetadata $cacheable_metadata) {
$request = $this->requestStack
->getCurrentRequest();
if ($request
->isMethodCacheable() && $this->renderer
->hasRenderContext() && $this
->hasCacheableMetadata($cacheable_metadata)) {
$build = [];
$cacheable_metadata
->applyTo($build);
$this->renderer
->render($build);
}
}
protected function hasCacheableMetadata(CacheableMetadata $cacheable_metadata) {
return $cacheable_metadata
->getCacheMaxAge() !== Cache::PERMANENT || count($cacheable_metadata
->getCacheContexts()) > 0 || count($cacheable_metadata
->getCacheTags()) > 0;
}
}