Skip to content
Snippets Groups Projects
Commit 37fa1cd5 authored by Viktor Holovachek's avatar Viktor Holovachek Committed by Adriano
Browse files

Issue #3221508 by AstonVictor, pawel_r: Re-implement...

Issue #3221508 by AstonVictor, pawel_r: Re-implement PrevnextService::getNodesOfType - performance issue
parent 164b036c
No related branches found
No related tags found
No related merge requests found
......@@ -40,39 +40,58 @@ class PrevNextService implements PrevNextServiceInterface {
* {@inheritdoc}
*/
public function getPreviousNext(NodeInterface $node) {
$nodes = $this->getNodesOfType($node);
$current_nid = $node->id();
$current_key = array_search($current_nid, $nodes);
$this->prevnext['prev'] = ($current_key == 0) ? '' : $nodes[$current_key - 1];
$this->prevnext['next'] = ($current_key == count($nodes) - 1) ? '' : $nodes[$current_key + 1];
$this->prevnext['prev'] = $this->getNodesOfType($node, 'prev');
$this->prevnext['next'] = $this->getNodesOfType($node, 'next');
return $this->prevnext;
}
/**
* Retrieves all nodes of the same type and language of given.
* Retrieves the prev and next nid filtered by the provided node.
*
* @param \Drupal\node\NodeInterface $node
* The node entity.
* @return string $type
* A prev/next nid filtered by type, status and language.
*
* @return array
* An array of nodes filtered by type, status and language.
* @return string
* A prev/next nid filtered by type, status and language.
*/
protected function getNodesOfType(NodeInterface $node) {
$query = $this->entityTypeManager->getStorage('node')->getQuery();
protected function getNodesOfType(NodeInterface $node, $type) {
$bundle = $node->bundle();
$langcode = $node->language()->getId();
$nodes = $query->condition('status', NodeInterface::PUBLISHED)
->condition('type', $bundle)
->condition('langcode', $langcode)
->addMetaData('type', $bundle)
->addMetaData('langcode', $langcode)
->addTag('prev_next_nodes_type')
->accessCheck(TRUE)
->execute();
return array_values($nodes);
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->accessCheck();
$query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', $bundle);
$query->condition('langcode', $langcode);
$query->range(0, 1);
$query->addMetaData('type', $bundle);
$query->addMetaData('langcode', $langcode);
$query->addTag('prev_next_nodes_type');
switch ($type) {
case 'prev':
$query->condition('nid', $node->id(), '<');
$query->sort('nid', 'DESC');
$query->addTag('prev_next_nodes_type_prev');
break;
case 'next':
$query->condition('nid', $node->id(), '>');
$query->sort('nid');
$query->addTag('prev_next_nodes_type_next');
break;
}
$id = '';
if ($results = $query->execute()) {
$id = reset($results);
}
return $id;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment