Drupal:每个节点中的"上一个和下一个"链接?

ane*_*yzm 4 drupal drupal-6

我想在Drupal网站的每个节点上添加上一个和下一个按钮.

这些链接应该指向网站内容中的下一个节点.

我该怎么做?谢谢

小智 8

这是aterchin代码的D7版本.

<?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);
}
?>
Run Code Online (Sandbox Code Playgroud)


ZyD*_*ver 5

使用Drupal 8,我最终创建了一个位于模块/ MY_MODULE / src / TwigExtension中的树枝扩展名(其中MODULE_NAME是您的模块机器名称),如果您不知道如何创建自定义D8模块,则可以参考此官方文档。发布

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您必须将树枝扩展注册为服务(位于模块/ MY_MODULE中的MY_MODULE.services.yml)。

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 }
Run Code Online (Sandbox Code Playgroud)

清除Drupal 8的缓存,您就可以在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 %}
Run Code Online (Sandbox Code Playgroud)


Mad*_*ist 3

自定义寻呼机模块正是您想要的。您可以使用视图来定义哪些节点是下一个/上一个节点。

如果您使用的是图像模块,则图像的自定义寻呼机的设置位于文档中。