上一个和下一个链接drupal自定义模块

时间:2019-01-23 08:58:00

标签: hyperlink next

<?php

名称空间Drupal \ dfr_node_custom;

使用Drupal \ node \ Entity \ Node;

TwigExtension类扩展\ Twig_Extension {     / **      *生成此扩展定义的所有Twig过滤器的列表。      * /     公共功能getFunctions(){         返回[             新\ Twig_SimpleFunction('customPrevious',array($ this,'customPrevious')),             新\ Twig_SimpleFunction('customNext',array($ this,'customNext'))         ];     }

/**
 * Gets a unique identifier for this Twig extension.
 */
public function getName() {
    return 'dfr_node_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', 'article')
        ->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;
}

} ?>

  

服务:dfr_node_custom.twig.TwigExtension:       类别:Drupal \ dfr_node_custom \ TwigExtension       标签:         -{名称:twig.extension}

     

{%       设置prevNextInfo = {'node_type':'article','nid':2}%}

     

{%,如果prevNextInfo.no​​de_type和prevNextInfo.nid以及   (customPrevious(prevNextInfo)或customNext(prevNextInfo))%}                  {%如果customPrevious(prevNextInfo)%}               {{'上一个节点'}}:{{   customPrevious(prevNextInfo).title}}           {% 万一 %}           {%如果customNext(prevNextInfo)%}               {{'下一个节点'}}:{{   customNext(prevNextInfo).title}}           {% 万一 %}        {%endif%}

0 个答案:

没有答案