1

I've a structure like this in the backend of october cms (static pages plugin)

Page
-subpage 1
-subpage 2
-subpage 3

I want to be able to link between the child pages to go to the next and previous one (if such exists).

Could not find anything about this.

OK. This is what I've got - not the most elegant solution but it works In the code part of the subpage! (the're sould be a check i the page has a parent but in my situation I only use links to subpages)

function onStart(){
  // current page url
  $parent = $this->page['apiBag']['staticPage']->getParent();
  $url = $this->page['apiBag']['staticPage']['viewBag']['url'];

  $currentPage = null;
  $children = $parent->getChildren();

  foreach( $children as $key => $page){
    if($page['viewBag']['url'] == $url) $currentPage = $key;
  }

  // previous page
  if ( array_key_exists($currentPage - 1, $children) ) {
    $this['prev_url'] = $children[$currentPage - 1]['viewBag']['url'];
    $this['prev_title'] = $children[$currentPage -1 ]['viewBag']['title'];
  }

  if ( array_key_exists($currentPage + 1, $children) ) {
    $this['next_url'] = $children[$currentPage + 1]['viewBag']['url'];
    $this['next_title'] = $children[$currentPage + 1]['viewBag']['title'];
  }

}
adam
  • 618
  • 2
  • 11
  • 31
  • Well .. then why don't you create your own blog kind of plugin .. in which you will add your records and check by making query for next and previous links .. – Mittul At TechnoBrave Jul 07 '17 at 10:50

1 Answers1

0

Parent page was added. Also now it works for second level pages.

function onStart()
{
    $this['search_query'] = get('q', $default = null);
    // current page url
    $parent = $this->page['apiBag']['staticPage']->getParent();
    $url = $this->page['apiBag']['staticPage']['viewBag']['url'];

    $currentPage = null;
    if($parent) {
        $children = $parent->getChildren();
        foreach( $children as $key => $page){
            if($page['viewBag']['url'] == $url) $currentPage = $key;
        }

        // previous page
        if ( array_key_exists($currentPage - 1, $children) ) {
            $this['prev_url'] = $children[$currentPage - 1]['viewBag']['url'];
            $this['prev_title'] = $children[$currentPage -1 ]['viewBag']['title'];
        }

        if ( array_key_exists($currentPage + 1, $children) ) {
            $this['next_url'] = $children[$currentPage + 1]['viewBag']['url'];
            $this['next_title'] = $children[$currentPage + 1]['viewBag']['title'];
        }
    // parent page
    $this['parent_title'] = $parent['viewBag']['title'];
    $this['parent_url'] = $parent['viewBag']['url'];
    }
}

Twig:

{% if prev_title|length > 0 %}
<a href="{{ prev_url }}" class="previous">{{ prev_title }}</a>
{% endif%}
{% if parent_title|length > 0 %}
<a href="{{ parent_url }}" class="up">{{ parent_title }}</a>
{% endif%}
{% if next_title|length > 0 %}
<a href="{{ next_url }}" class="next">{{ next_title }}</a>
{% endif%}
zooks
  • 525
  • 1
  • 4
  • 12