Search code examples
phphtmlnavigationmodxmodx-revolution

Display 'prev' and 'next' pagination, even when inactive, in Modx Revolution's Articles?


I have a blog set up in modx revolution using the articles addon, but I'm having some trouble configuring certain parts of the pagination. My current call for the pagination looks like this:

[[!+page.nav:notempty=`<nav>[[!+page.nav]]</nav>`]]

I've added the following listing parameters to the getPage call, to removes the [[+first]] and [[+last]] template from the pagination, and insert a static "back to top" link:

&pageNavOuterTpl=`[[+prev]]<a href="#header">Back to top</a>[[+next]]`

However, one thing still doesn't work as I intend. By default the previous and next links disappear if there is no previous or next page. However I'd rather display them and make them inactive in such a case. That way the pagination always looks the same, but inactive parts can be grayed out.

It seems that include.getpage.php contains the following lines (which prevent the prev and next links to be shown when there are no pages to navigate to):

// lines 16 - 18

if (!empty($pagePrevTpl) && ($page - 1) >= 1) {
    $nav['prev'] = getpage_makeUrl($modx, $properties, $page - 1, $pagePrevTpl);
}

// and lines 29 - 31

if (!empty($pageNextTpl) && ($page + 1) <= $pageCount) {
    $nav['next'] = getpage_makeUrl($modx, $properties, $page + 1, $pageNextTpl);
}

Which makes sense, but prohibits me from displaying placeholders as I want to. So what I'm trying to achieve is something like this:

// example for lines 16 - 18

if (!empty($pagePrevTpl)) {
    if (($page - 1) >= 1) {
        $nav['prev'] = getpage_makeUrl($modx, $properties, $page - 1, $pagePrevTpl);
    } else {
        $nav['prev'] = x // where x = default value for prev (set with :default)
    }
}

A simple else or elseif like the above would do the trick, but that would be gone after an upgrade. Anyone have any ideas on the best way to approach this? Is there a way to achieve what I want to do without the changes getting overwritten on an upgrade? With a property set or something? I'm hoping someone knows how to do this.


Solution

  • For clarity: this answer was posted by the OP. I did this because none of the other answers were up to par (the posters never even responded to my comments/suggestions), and I don't want to give a 150pt. bounty for an answer that isn't good. Since SO automatically awards a bounty to given answers (without even offering the choice of not awarding it to the OP), I've decided to do it this way. I'd rather not award the bounty, than give it to someone who doesn't put in the effort for it.

    So, since this is a bug in the Articles include.getpage.php snippet, I decided to solve it with php as well. So I used a snippet called nav.placeholder to insert a placeholder when necessary. It's called from the articles template like so:

    <!-- Secondary Navigation -->
    <!-- Insert the nav tag only when there is pagination -->
    [[!+page.nav:notempty=`<nav>`]] 
      <!-- Call nav.placeholder snippet and test if placeholder is needed -->
      [[!nav.placeholder? &nav=`[[!+page.nav]]` &before=`1`]]
      <!-- Display regular pagination, without surrounding tags -->
      [[!+page.nav:notempty=`[[!+page.nav]]`]]
      <!-- Call nav.placeholder snippet and test if placeholder is needed -->      
      [[!nav.placeholder? &nav=`[[!+page.nav]]` &before=`0`]]
    <!-- Insert the /nav tag only when there is pagination -->
    [[!+page.nav:notempty=`</nav>`]]
    

    This call passes the content of page.nav (with the nav variable) to the snippet and tells it in which position of the menu it is with the before variable (so it won't insert the placeholder at the wrong place).

    The snippet itself looks like this:

    if (strstr($nav, "Older") != false && $before == 1) {
      return "insert 'newer' placeholder content here";
    } elseif (strstr($nav, "Newer") != false && $before == 0) {
      return "insert 'older' placeholder here";
    }
    

    I know this is all pretty hacky, and not very dynamic, but it works, won't be overwritten on upgrade and is all done in the cms without ugly js or css hacking.