回答
Custom Pager module確實是你想要的。您可以使用視圖來定義哪些節點是下一個/上一個節點。
如果您使用的是圖像模塊,圖像自定義傳呼機的設置位於文檔中。
其他方式:「book」模塊在drupal包中。
TIMTOWDI,男。標識使用,不需要額外的模塊選項,但Drupal數據庫是如何構建的,而不是一些知識(加上一些基本的SQL):
- 爲您的節點類型的自定義模板,
- 在模板中,添加根據您的需要(根據您的需要過濾)獲取下一個和前一個節點的數據庫查詢,
- 提取這兩個節點的網址,
- 將鏈接放置在您需要它們的位置。
這是稍微複雜,但最終靈活;)
我覺得Previous/Next API模塊可能是你在找什麼。
從模塊頁面:
用於瀏覽下一個/前面的節點,而不會加重數據庫服務器的API。
該模塊允許您瞭解任何給定節點的前一個或下一個節點。這對於向用戶提供導航鏈接是非常有用的,而無需動態地動態地推斷這些信息所需的昂貴查詢。
這會在同一個函數調用中執行上一個/下一個查詢,並以數組的形式返回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);
}
?>
這是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);
}
?>
這是一個整潔的解決方案,因爲在其他答案中提到的第三方模塊吹了一下。但是我認爲在函數的第一行中有一個小錯誤很容易修復:如果'!is_object(...)'檢查是'true',那麼你不應該訪問'$ current_node-> nid'字段,而是使用'$ current_node'值直接。另外,您也可以將支票更改爲'is_numeric(...)'或甚至'is_int(...)'。 – Arvid 2016-03-30 08:39:12
使用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 %}
感謝這是一個非常有用的開始。 – 2017-01-14 00:11:43
- 1. Drupal上一個和下一個鏈接
- 2. 鏈接列表,但每個「下一個」指針指向下一個節點的「下一個」指針
- 3. XmlTextReader跳過每個下一個節點
- 4. 獲取下一個節點/ Drupal 7
- 5. 下一個節點*下一個和節點*有區別嗎?
- 6. Drupal的:路由節點的鏈接到一個視圖
- 7. 使用MvcSiteMap如何創建到下一個和前一個節點的鏈接?
- 8. 一個和下一個鏈接
- 9. 上一個和下一個鏈接在WordPress的同一類別
- 10. 上點擊一個鏈接
- 11. 如何在WordPress中顯示上一個和下一個鏈接
- 12. 下一個節點
- 13. 總結兩個鏈接列表,其中每個節點是一個數字
- 14. 填充下一個和上一個鏈接的href
- 15. WordPress的下一個和上一個鏈接修改
- 16. 抓取的一個/下一個節點
- 17. 下一個/上一個錨鏈接,當用戶滾動時如何更新下一個/上一個鏈接
- 18. 每個文件使用Drupal創建一個節點7
- 19. 點擊兩個鏈接,點擊一下
- 20. Drupal節點表單|每個節點類型都有一個.tpl主題文件?
- 21. 複製鏈接列表,每個節點都有一個變量,它隨機指向另一個節點列表
- 22. 鏈接列表的一個節點中的三個元素
- 23. 在Drupal中,如何在一個節點中包含另一個節點?
- 24. C#雙向鏈表 - 上一個和下一個不會連接
- 25. Laravel 5.3上一個和下一個記錄鏈接
- 26. PHP:只保留下一個和上一個分頁鏈接
- 27. 搜索結果和上一個/下一個項目鏈接
- 28. 創建下一個圖像點擊:上一個和下一個
- 29. 焦點的下一個鏈接與jQuery
- 30. 如何鏈接一個xml文件與幾個節點和子節點?
這裏指出確切的問題(節點455有一個鏈接到節點454和節點456的鏈接)很可能會做奇怪的事情與其他模塊組合。我會建議嘗試重寫你想要發生的事情,而不使用沒有內容類型的「上一個節點」和「下一個節點」。 – Jasper 2010-09-12 12:28:58
是的好吧,我想過濾的內容類型 – aneuryzm 2010-09-13 07:14:04