2010-09-12 71 views
3

我想为我的Drupal网站的每个节点添加上一个和下一个按钮。Drupal:每个节点中的“上一个和下一个”链接?

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

我该怎么做? 谢谢

+0

这里指出确切的问题(节点455有一个链接到节点454和节点456的链接)很可能会做奇怪的事情与其他模块组合。我会建议尝试重写你想要发生的事情,而不使用没有内容类型的“上一个节点”和“下一个节点”。 – Jasper 2010-09-12 12:28:58

+0

是的好吧,我想过滤的内容类型 – aneuryzm 2010-09-13 07:14:04

回答

2

Custom Pager module确实是你想要的。您可以使用视图来定义哪些节点是下一个/上一个节点。

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

1

其他方式:“book”模块在drupal包中。

0

TIMTOWDI,男。标识使用,不需要额外的模块选项,但Drupal数据库是如何构建的,而不是一些知识(加上一些基本的SQL):

  • 为您的节点类型的自定义模板,
  • 在模板中,添加根据您的需要(根据您的需要过滤)获取下一个和前一个节点的数据库查询,
  • 提取这两个节点的网址,
  • 将链接放置在您需要它们的位置。

这是稍微复杂,但最终灵活;)

2

我觉得Previous/Next API模块可能是你在找什么。

从模块页面:

用于浏览下一个/前面的节点,而不会加重数据库服务器的API。

该模块允许您了解任何给定节点的前一个或下一个节点。这对于向用户提供导航链接是非常有用的,而无需动态地动态地推断这些信息所需的昂贵查询。

0

这会在同一个函数调用中执行上一个/下一个查询,并以数组的形式返回prev/next信息。我最喜欢的方式是重用代码。参数也更灵活。

<?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); 
} 
?> 
5

这是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); 
} 
?> 
+0

这是一个整洁的解决方案,因为在其他答案中提到的第三方模块吹了一下。但是我认为在函数的第一行中有一个小错误很容易修复:如果'!is_object(...)'检查是'true',那么你不应该访问'$ current_node-> nid'字段,而是使用'$ current_node'值直接。另外,您也可以将支票更改为'is_numeric(...)'或甚至'is_int(...)'。 – Arvid 2016-03-30 08:39:12

3

使用Drupal的8,我结束了创建位于模块/ MY_MODULE/src目录/ TwigExtension树枝扩展)(其中MODULE_NAME是你的模块的机器名,如果你不知道如何创建一个自定义的D8模块,you can refer to this official post

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; 
    } 
} 

然后,你必须注册你的树枝延伸服务(位于模块/ 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 } 

清除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 %} 
+0

感谢这是一个非常有用的开始。 – 2017-01-14 00:11:43

相关问题