We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1841
    • 141 Posts
    I'm hoping someone can direct me the right way with creating a snippet that will allow a working previous / next navigation system for my site. Any helper will receive my eternal gratitute, huge levels of Karma and a nice warm glow :}

    Back in the distant past when Evo was all the rage I had a great snippet called "navigator" which was modified perfectly to fit the purpose however I've never been able to replicate the same functionality in Revo. I've tried all the previous / next snippets including SiblingNav, BasicNav, Selflink and MXSimplePrevNext to no avail.

    I've also tried to use "get resources" and even write my own snippet but I can't quite get something working. The scenario is:

    The website has the file structure:

    Genre Folder >>
    Authors A-B >>
    Author A >>
    Review of Book by Author A
    Review of Book by Author A
    Review of Book by Author A
    Author B >>
    Review of Book by Author B
    Series by Author B >>
    Review of Book in Series X by Author B
    Authors C-D >>
    Author C >>
    Review of Book by Author C
    Review of Book by Author C

    .....


    The pages "review of book..." all use the same template and are the only pages that are set not to "hide from the menu" (I liked the idea of using that variable to help display and sort the reviews).

    What I am trying to do is have a previous / next navigation for the review pages which goes to the previous and next reviews based on position within the menu tree, going to the next review of the next author folder when no more reviews are in the current folder and sorted by "menuindex". As you can probably gather this doesn't seem as straight-forward as it was in Evo and I've hit a brick wall each time I've attempted.

    Other ways I've thought of doing this was to built a unique ID based on the menuindex of the "breadcrumb" (ie menuindex of genre folder+authors A-B+Author A+Review of book by author a) and use that unique ID to sort by - but couldn't figure out how this could be applied or stored in a placehiolder / TV so it could be applied to something like getresources..

    The other thing to note is that there are 3 "genre folders" and a few thousand resources all together (around 2 thousand+ published and at least that many again unpublished).

    As mentioned any help would be most appreciated.
      • 38705
      • 101 Posts
      How about the selfLink extra? Created by Oost Design

      Extra: http://modx.com/extras/package/selflink
      RTFM: http://rtfm.modx.com/extras/revo/selflink
        Addict since 2012....
        • 1841
        • 141 Posts
        Hi mintnl,

        Thanks for the reply, yes I did try Selflink - although it was prior to his latest update at the end of June but I did ask Bert himself if Selflink could be made to work and he said no.

        I have tried all the extras that are available for modx (along with a few that aren't) and have yet to get a workable system.
          • 39404
          • 175 Posts
          stalemate resolution associate Reply #4, 13 years, 1 month ago
          What about using a combination of getResources to get the list of those items with a certain template ordered by some key, and then write something to derive next and previous based on the indexof the current resource within the array?

          Since you know how to determine which pages you want in the getResources, you just call it without specifying the tpl parts.

          The indexof with the current resource would tell you where the resource fits within the context of all resources.
            • 1841
            • 141 Posts
            Hi stalemate resolution associate, thanks for the reply. Not sure how viable this would be, as mentioned there are a few thousand resources and I'm still not sure how we could get them to list in the right order and wouldn't know where to start with "indexof"
              • 39404
              • 175 Posts
              stalemate resolution associate Reply #6, 13 years, 1 month ago
              Hi antsplace,

              Sorry for the confusion: in the back of my mind as I was replying, I was thinking that getResources was normally used within a snippet (rather than being called on its own) and that the results were a normal array.

              Thinking more about it, I realize neither is the case.

              I would suggest calling the getResources within a snippet. Specify a tpl chunk which formats the results into a comma delimited string, explode the string and then get previous and next based on this.

              In terms of rough pseudocode, here's how I see it:

              // get your resources in a comma delimited string (myRowTpl has to be exist)
              $listString = $modx->runSnippet('getResources', array(&where=`{"template:=":8}` &tpl=`myRowTpl`));
              $list = explode(',',$listString);
              $positionOfCurrent = array_search($currentResourceId, $list);
              $nextResource = $list($positionOfCurrent + 1);
              $previousResource = $list($positionOfCurrent - 1);
              


              Of course you'd need to add something in there to check that the current resource wasn't the first or last (and violate the array restrictions), but I think you get the jist of it.
                • 1841
                • 141 Posts
                Hi stalemate resolution associate,

                forgive my ignorance but I'm still not sure how I could get this to work, especially given the volume of documents that would be in such an array
                  • 35150 ☆ A M B ☆
                  • 191 Posts
                  You might look through the source code for one of my extras. It will get parents, first and last children, next and previous siblings and you can see how it does it.
                  Using an approach like this you wouldn't have to map out the entire resource tree every time in the snippet, just enough of it to get the next/previous sibling, or the first/last child of the next/previous sibling of the parent.
                    Extras :: pThumb • Resizer • imageSlim • setPlaceholders
                    • 1841
                    • 141 Posts
                    Hi Jgrant,

                    that looks like some very clever and versatile bit of code! I'm still learning php but I will have a go, thanks - much appreciated!
                      • 35150 ☆ A M B ☆
                      • 191 Posts
                      Yeah, you should be able to write something using those various pieces. Actually you could almost use that extra without any extra coding (I think). I haven't tested this, but seems like the following might work for a 2-level tree.

                      [[setPlaceholders? &ph=`
                          prev == prev.uri !! parent.prev.child-1.uri ||
                          next == next.uri !! parent.next.child.uri
                      `]]
                      
                      [[+prev:!empty=`<a href="[[+prev]]">Previous</a>`]]
                      [[+next:!empty=`<a href="[[+next]]">Next</a>`]]
                      


                      That would either get a link to the previous resource, or to the last child of the parent's previous sibling, or nothing. Same for next: either the next resource or the first child of the parent's next sibling, which lets you move between reviews by the same author and also between authors in the same alphabetical section (Author A and Author B). The only problem is you've got 3 levels, so it wouldn't jump between Authors A–B and Authors C–D. For that you'd need to be able to specify a 2nd backup parameter, which setPlaceholders doesn't currently support.
                      It wouldn't be hard to add; maybe I could do it later this week if you're interested.
                      Then you could do something like:

                      [[setPlaceholders? &ph=`
                          prev == prev.uri !! parent.prev.child-1.uri !! parent2.prev.child-1.child-1.uri ||
                          next == next.uri !! parent.next.child.uri !! parent2.next.child.child.uri
                      `]]
                      


                      It's convoluted, but I think it'd work and be reasonably fast too, faster than using getResources on the whole document tree for sure.
                        Extras :: pThumb • Resizer • imageSlim • setPlaceholders