2010-09-12 70 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

相關問題