Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prevnext
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
prevnext
Merge requests
!21
3221508 - Change query
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
3221508 - Change query
issue/prevnext-3221508:3221508-performance-query
into
2.0.x
Overview
1
Commits
2
Pipelines
0
Changes
1
Closed
Viktor Holovachek
requested to merge
issue/prevnext-3221508:3221508-performance-query
into
2.0.x
1 year ago
Overview
1
Commits
2
Pipelines
0
Changes
1
Expand
Closes
#3221508
0
0
Merge request reports
Compare
2.0.x
version 1
29f41b2b
1 year ago
2.0.x (base)
and
latest version
latest version
c3574af7
2 commits,
1 year ago
version 1
29f41b2b
1 commit,
1 year ago
1 file
+
40
−
21
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/PrevNextService.php
+
40
−
21
Options
@@ -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
;
}
}
Loading