Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 151 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal: "previous and next" links in each node?

#1
I would like to add previous and next buttons to each node of my Drupal website.

These links are supposed to point to the next node in the website content.

How can I make it ?
Thanks
Reply

#2
The [Custom Pager module][1] does exactly what you want. You can use a view to define which nodes are the next/previous one.

If you're using the Image module, the setup of the custom pager for images is in the documentation.


[1]:

[To see links please register here]

Reply

#3
other way: "book" module in drupal package.
Reply

#4
TIMTOWDI, man. Id use an option that requires no extra modules, but instead some knowledge of how Drupal database is built (plus some basic SQL):

* Create a custom template for your node type,
* In the template, add a database query to fetch the next and previous nodes, according to your necessities (filter by whatever you want),
* Extract the URLs of those two nodes,
* place the links where you need them.

It's slightly complex, but ultimately flexible ;)
Reply

#5
This does both prev/next queries in the same function call and returns prev/next info as an array. My preferred way is to reuse code. The parameters are more flexible as well.

<?php
/**
* Previous / Next function for nodes, ordered by node creation date
*
* @param $current_node: node object or node id
* @param $node_types: array of node types to query
*
* @return array
*/
function mymodule_prev_next_created($current_node = NULL, $node_types = array()) {
// make node object if only node id given
if (!is_object($current_node)) { $current_node = node_load($current_node->nid); }

// make an array if string value was given
if (!is_array($node_types)) { $node_types = array($node_types); }

// previous
$sql = "SELECT n.nid, n.title, n.created FROM {node} n
WHERE n.created < %d AND n.type IN ('%s')
AND n.status = 1 ORDER BY n.created DESC LIMIT 1";
$result = db_query($sql, $current_node->created, implode("','", $node_types));
$prev = db_fetch_object($result);

// next
$sql = "SELECT n.nid, n.title, n.created FROM {node} n
WHERE n.created > %d AND n.type IN ('%s')
AND n.status = 1 ORDER BY n.created ASC LIMIT 1";
$result = db_query($sql, $current_node->created, implode("','", $node_types));
$next = db_fetch_object($result);

return array('prev' => $prev, 'next' => $next);
}
?>
Reply

#6
Here's a D7 version of aterchin's code.

<?php
/**
* Previous / Next function for nodes, ordered by node creation date
*
* @param $current_node: node object or node id
* @param $node_types: array of node types to query
*
* @return array
*
*/
function MODULE_prev_next_node($current_node = NULL, $node_types = array()) {
// make node object if only node id given
if (!is_object($current_node)) { $current_node = node_load($current_node->nid); }

// make an array if string value was given
if (!is_array($node_types)) { $node_types = array($node_types); }

// previous
$prev = db_select('node', 'n')
->fields('n',array('nid','title','created'))
->condition('n.status', 1,'=')
->condition('n.type', $node_types,'IN')
->condition('n.created', $current_node->created,'<')
->orderBy('created','DESC')
->range(0,1)
->execute()
->fetchAssoc();

// next or false if none
$next = db_select('node', 'n')
->fields('n',array('nid','title','created'))
->condition('n.status', 1,'=')
->condition('n.type', $node_types,'IN')
->condition('n.created', $current_node->created,'>')
->orderBy('created','ASC')
->range(0,1)
->execute()
->fetchAssoc();

return array('prev' => $prev, 'next' => $next);
}
?>
Reply

#7
I think the [Previous/Next API][1] module may be what you're looking for.

From the module page:

> An API for browsing next/previous nodes without overloading your database server.
>
> This module allows you to know the previous or next nodes for any given node. This is very useful for providing navigational links to the user without the expensive queries required to dynamically deduce such information on the fly.

[1]:

[To see links please register here]

Reply

#8
Using Drupal 8, I ended up creating a twig extension located in modules/MY_MODULE/src/TwigExtension) (where MODULE_NAME is your module machine name, if you don't know how to create a custom D8 module, [you can refer to this official post][1])

namespace Drupal\MODULE_NAME\TwigExtension;

use Drupal\node\Entity\Node;

class PrevNextNode extends \Twig_Extension {
/**
* Generates a list of all Twig filters that this extension defines.
*/
public function getFunctions() {
return [
new \Twig_SimpleFunction('customPrevious', array($this, 'customPrevious')),
new \Twig_SimpleFunction('customNext', array($this, 'customNext'))
];
}

/**
* Gets a unique identifier for this Twig extension.
*/
public function getName() {
return 'custom.prevnextnode_extension';
}

/**
* Previous node
* @param $prevNextInfo
* @return array|bool
*/
public function customPrevious($prevNextInfo)
{
$node = Node::load($prevNextInfo['nid']);
return $this->getNodeInformation($prevNextInfo['node_type'], $node->getCreatedTime(), '<', 'DESC');
}

/**
* Next node
* @param $prevNextInfo
* @return array|bool
*/
public function customNext($prevNextInfo)
{
$node = Node::load($prevNextInfo['nid']);
return $this->getNodeInformation($prevNextInfo['node_type'], $node->getCreatedTime(), '>', 'ASC');
}

/**
* Get current langcode
* @return string
*/
public function getCurrentLangcode()
{
return \Drupal::languageManager()->getCurrentLanguage()->getId();
}

/**
* Previous or next node
* @param $node_type
* @param $date
* @param $date_comparator
* @param $sort_order
* @return array|bool
*/
public function getNodeInformation($node_type, $date, $date_comparator, $sort_order)
{
$prev_or_next = \Drupal::entityQuery('node')
->condition('type', $node_type)
->condition('status', 1)
->condition('created', $date, $date_comparator)
->sort('created', $sort_order)
->range(0, 1)
->execute()
;

if(!$prev_or_next) return false;

// Get the node itself
$prev_or_next = Node::load(array_values($prev_or_next)[0]);
// Get the available languages for the given node
$available_languages = $prev_or_next->getTranslationLanguages();
// If the current language is defined in the available languages array
if(array_key_exists($this->getCurrentLangcode(), $available_languages)){
// Get the translated node
$translation = $prev_or_next->getTranslation($this->getCurrentLangcode());

// Return the information you need, can be w/e you want.
return [
'title' => $translation->getTitle(),
'id' => $translation->id(),
'path' => $translation->toUrl()->toString()
];
}

return false;
}
}

Then you'll have to register your twig extension as a service (MY_MODULE.services.yml located in modules/MY_MODULE).


services:
# Next / Previous links on selected node pages. class: namespace of your extension
custom.prevnextnode_extension:
class: Drupal\MODULE_NAME\TwigExtension\PrevNextNode
tags:
- { name: twig.extension }

Clear the cache of your Drupal 8 and you'll be able to use your new twig extensions wherever your want in your HTML.

{# make sure you get 'content_type' and 'nid' parameters (a preprocess file would be a good start) #}
{%
set prevNextInfo = { 'node_type': 'content_type', 'nid' : 10 }
%}

{% if prevNextInfo.node_type and prevNextInfo.nid and (customPrevious(prevNextInfo) or customNext(prevNextInfo)) %}
<div id="node-pagination">
{% if customPrevious(prevNextInfo) %}
<a href="{{ customPrevious(prevNextInfo).path }}" class="pagination-btn pagination-prev">{{ 'Previous node' }} : {{ customPrevious(prevNextInfo).title }}</a>
{% endif %}
{% if customNext(prevNextInfo) %}
<a href="{{ customNext(prevNextInfo).path }}" class="pagination-btn pagination-prev">{{ 'Next node' }} : {{ customNext(prevNextInfo).title }}</a>
{% endif %}
</div>
{% endif %}


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through