Skip to content
Snippets Groups Projects
Commit f33f3712 authored by Viktor Holovachek's avatar Viktor Holovachek
Browse files

Resolve #3221508 "Performance query"

parent cd27fcd1
Branches
No related tags found
1 merge request!26Resolve #3221508 "Performance query"
......@@ -35,7 +35,7 @@ class PrevNextService implements PrevNextServiceInterface {
/**
* Previous / Next ids.
*
* @var array
* @var array{prev: ?string, next: ?string}|array{prev: ?int, next: ?int}
*/
public $prevnext;
......@@ -141,31 +141,30 @@ class PrevNextService implements PrevNextServiceInterface {
* {@inheritdoc}
*/
public function getPreviousNext(EntityInterface $entity) {
$entities = $this->getEntitiesOfType($entity);
$id = $entity->id();
$key = array_search($id, $entities);
$this->prevnext['prev'] = ($key == 0) ? '' : $entities[$key - 1];
$this->prevnext['next'] = ($key == count($entities) - 1) ? '' : $entities[$key + 1];
$this->prevnext['prev'] = $this->getEntitiesOfType($entity, 'prev');
$this->prevnext['next'] = $this->getEntitiesOfType($entity, 'next');
return $this->prevnext;
}
/**
* Retrieves all entities of the same type and language of given.
* Retrieves the previous or next ID for the provided entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
* @param string $order
* The order of the query, 'prev' or 'next'.
*
* @return array
* An array of entities filtered by type, status and language.
* @return string|int|null
* The next/previous entity ID after filtering by type, status and language.
*/
protected function getEntitiesOfType(EntityInterface $entity) {
protected function getEntitiesOfType(EntityInterface $entity, $order) {
$definition = $this->entityTypeManager->getDefinition($entity->getEntityTypeId());
$query = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->getQuery();
$query->condition('status', 1);
$query->accessCheck(TRUE);
$query->range(0, 1);
$query->accessCheck();
$query->addTag("prev_next_{$entity->getEntityTypeId()}_type");
$bundle = $entity->bundle();
......@@ -174,13 +173,33 @@ class PrevNextService implements PrevNextServiceInterface {
$query->addMetaData($type, $bundle);
}
$langcode = $entity->language()->getId();
if ($lang = $definition->getKey('langcode')) {
$langcode = $entity->language()->getId();
$query->condition($lang, $langcode);
$query->addMetaData($lang, $langcode);
}
return array_values($query->execute());
if ($id = $definition->getKey('id')) {
switch ($order) {
case 'prev':
$query->condition($id, $entity->id(), '<');
$query->sort($id, 'DESC');
break;
case 'next':
$query->condition($id, $entity->id(), '>');
$query->sort($id);
break;
}
}
$result = NULL;
if ($results = $query->execute()) {
$result = reset($results);
}
return $result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment