Last active
January 31, 2022 19:23
-
-
Save laradevitt/61ba944b1f7b9ab9b1593faff83a6500 to your computer and use it in GitHub Desktop.
(Drupal 8) A simple back/next pager (based on a views result) for node default display mode.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\views\Views; | |
// Optional, if using filter values passed via URL (see example below). | |
use Drupal\Component\Utility\Html; | |
/** | |
* Preprocesses variables for node--NODE_TYPE.html.twig. | |
* | |
* Replace 'mytheme' with the name of your theme, and NODE_TYPE and VIEW_NAME | |
* with actual machine names. | |
*/ | |
function mytheme_preprocess_node__NODE_TYPE(&$variables) { | |
$node = $variables['node']; | |
// Execute the view that provides the context. | |
$view = \Drupal\views\Views::getView('VIEW_NAME'); | |
// Optionally set a contextual filter value passed in via the URL; | |
// e.g. path-to-node?vid=456. You will need to add the parameter to the pager | |
// URLs in your Twig template. | |
$variables['arg'] = Html::escape(\Drupal::request()->query->get('vid')); | |
$view->setArguments([$variables['arg']]); | |
$view->execute(); | |
// Iterate through the results to find the results index for previous, | |
// current, and next nodes. | |
$match = FALSE; | |
foreach ($view->result as $k => $row) { | |
// Break out of iterator when we find the next row. | |
if ($match !== FALSE) { | |
$next = $k; | |
break; | |
} | |
// This result matches our currently loaded node. | |
if ($row->nid == $node->id()) { | |
$match = $k; | |
} | |
// As long as we haven't reached a match, let's keep updating $previous. | |
if ($match === FALSE) { | |
$previous = $k; | |
} | |
} | |
// If a match is found, set up our template variables. | |
if ($match !== FALSE) { | |
$variables['node_view_pager'] = []; | |
$variables['node_view_pager']['num_results'] = count($view->result); | |
$variables['node_view_pager']['index'] = $match + 1; | |
$controller = \Drupal::entityManager()->getStorage('node'); | |
if (isset($previous)) { | |
$variables['node_view_pager']['previous'] = $controller->load($view->result[$previous]->nid); | |
} | |
if (isset($next)) { | |
$variables['node_view_pager']['next'] = $controller->load($view->result[$next]->nid); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
/** | |
* @file node--NODE_TYPE.html.twig | |
* | |
* Custom variables: | |
* - node_view_pager: An array containing the pager information. | |
* - node_view_pager.previous: The previous node object. | |
* - node_view_pager.next: The next node object. | |
* - node_view_pager.index: The index of the view result (incremented by 1 so | |
* it starts at '1' instead of '0'). | |
* - node_view_pager.num_results: The total number of results returned. | |
*/ | |
#} | |
<article{{ attributes }}> | |
{{ title_prefix }} | |
{% if not page %} | |
<h2{{ title_attributes }}> | |
<a href="{{ url }}" rel="bookmark">{{ label }}</a> | |
</h2> | |
{% endif %} | |
{{ title_suffix }} | |
{% if display_submitted %} | |
<footer> | |
{{ author_picture }} | |
<div{{ author_attributes }}> | |
{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %} | |
{{ metadata }} | |
</div> | |
</footer> | |
{% endif %} | |
<div{{ content_attributes }}> | |
{{ content }} | |
</div> | |
{# Begin node view pager #} | |
{% if node_view_pager %} | |
<nav class="node-pager"> | |
<ul role="navigation" aria-label="Pagination"> | |
<li> | |
{% if node_view_pager %} | |
{% if node_view_pager.previous %} | |
<a href="{{ path('entity.node.canonical', {'node': node_view_pager.previous.id}) }}"><span>{{ 'Back'|t }}</span></a> | |
{% else %} | |
<span>{{ 'Back'|t }}</span> | |
{% endif %} | |
{% endif %} | |
</li> | |
<li> | |
{% trans %} | |
({{ node_view_pager.index }}/{{ node_view_pager.num_results }}) | |
{% endtrans %} | |
</li> | |
<li> | |
{% if node_view_pager %} | |
{% if node_view_pager.next %} | |
<a href="{{ path('entity.node.canonical', {'node': node_view_pager.next.id}) }}"><span>{{ 'Next'|t }}</span></a> | |
{% else %} | |
<span>{{ 'Next'|t }}</span> | |
{% endif %} | |
{% endif %} | |
</li> | |
</ul> | |
</nav> | |
{% endif %} | |
{# End node view pager #} | |
</article> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment