grav-learn icon indicating copy to clipboard operation
grav-learn copied to clipboard

nextSibling returning collection

Open PZ01 opened this issue 8 years ago • 2 comments

I have a page variable that shows up like this when I dump it : {{ dump(page) }}

Grav\Common\Page\Page {#189
  #name: "news-item.md"
  #folder: "slicing-meat-perfectly"
  #path: "/home/pz/Code/pp/user/pages/02.news"
  #extension: ".md"
  #url_extension: ""
  #id: "14796693254b05940b2b54fa4d02652305bc165011"
  #parent: "/home/pz/Code/pp/user/pages/02.news"
  #template: "news-item"
  #expires: null
  #visible: false
  #published: true
  #publish_date: null
  #unpublish_date: null
  #slug: "slicing-meat-perfectly"
  #route: "/news/slicing-meat-perfectly"
  #raw_route: "/news/slicing-meat-perfectly"
  #url: null
  #routes: null
  #routable: true
  #modified: 1479669325
  #redirect: null
  #external_url: null
  #items: null
  #header: {#187
    +"title": "Slicing Meat Perfectly"
    +"date": "09:34 07/01/2015"
    +"description": "Not sure where to start! It can go left, it can go right, it can go up, it can go down! The head spins and spins, it never stops!"
    +"imageMainBackground": "individual-backgrounds/outside.jpg"
    +"taxonomy": array:3 [
      "category" => "Food"
      "tag" => array:2 [
        0 => "reflection"
        1 => "thoughts"
      ]
      "author" => "ksmith"
    ]
  }
  ...

When I call {{ dump(page.nextSibling()) }} I get a collection :

Grav\Common\Page\Collection {#622
  #pages: Grav\Common\Page\Pages {#100
    #grav: Grav\Common\Grav {#3
      #processors: array:13 [
        0 => "siteSetupProcessor"
        1 => "configurationProcessor"
        2 => "errorsProcessor"
        3 => "debuggerInitProcessor"
        4 => "initializeProcessor"
        5 => "pluginsProcessor"
        6 => "themesProcessor"
        7 => "tasksProcessor"
        8 => "assetsProcessor"
        9 => "twigProcessor"
        10 => "pagesProcessor"
        11 => "debuggerAssetsProcessor"
        12 => "renderProcessor"
      ]
      -values: array:48 [
        "loader" => Composer\Autoload\ClassLoader {#1
          -prefixLengthsPsr4: array:7 [
            "W" => array:1 [ …1]
            "S" => array:7 [ …7]
            "R" => array:8 [ …8]
            "M" => array:3 [ …3]
            "L" => array:1 [ …1]
            "G" => array:1 [ …1]
            "D" => array:2 [ …2]
          ]

I'm expecting the next page object, not this collection. The collection that I have passed down to my twig templates looks like this:

content:
    items: 
        '@taxonomy.category': 'Food'
    order:
        by: date
        dir: desc
    limit: 30
    pagination: true

I actually have another section with a collection using taxonomy "News" that has the next and previous sibling functions correctly returning the next or previous page object, I am stumped.

PZ01 avatar Nov 20 '16 19:11 PZ01

Reading the code, if there is no adjacent sibling, the collection is returned. So you should first call page.isLast(). Is this the cause of the different behavior?

flaviocopes avatar Nov 25 '16 18:11 flaviocopes

Here's how I use the page :

<ul class="pagination-gold">
    {{ dump(page.isLast()) }} <!-- Returns FALSE -->
    {{ dump(page.adjacentSibling(-1)) }} <!-- Returns Collection -->
    {% if page.isLast() == false %}
    <li class="next">
        <a href="{{ page.adjacentSibling(-1).url }}"> NEXT Post <i class="fa fa-angle-double-right"></i></a>
    </li>
    {% endif %}
    {% if page.isFirst() == false %}
    <li class="previous">
        <a href="{{ page.adjacentSibling(1).url }}"> <i class="fa fa-angle-double-left"></i> PREVIOUS POST</a>
    </li>
    {% endif %}
</ul>

The same code is used for my other tag and I get the next sibling so the problem must be related to how I pass the collections. I believe I am already properly calling the isLast() method.

I do not know if this can help, but I attached my folder structure. structure I have all my pages in a 'News' folder, but for other categories I use separate folders. According to the docs, using @taxonomy will retrieve all the published pages in /page so it should be good. I will keep investigating.

PZ01 avatar Nov 27 '16 19:11 PZ01