Jump to content

Alphabetical pager nav


MarkE
 Share

Recommended Posts

Prompted by @ryan's recent enhancement of the page tree display, I wanted to display an alphabetical index of pages rather than a numerical one, so I built a hook after MarkupPagerNav::render. You can see the result here: https://ncorchards.co.uk/main/fruit-orchards/apples/

Basically the hook just replaces render() with a modified version. The key modifications are to select all the items without limit:

		$items = $event->arguments(0);  //PageArray
		$itemSelector = $items->getSelectors(true);  // true returns the string rather than the object
		$noLimit = preg_replace('/,\s*limit=-?\d+/i', '', $itemSelector);

and then to modify the pager:

		$pagerNav = new PagerNav($object->totalItems, $object->itemsPerPage, $object->pageNum);
		$pagerNav->setLabels($objectOptions['previousItemLabel'], $objectOptions['nextItemLabel']);
		$pagerNav->setNumPageLinks($object->numPageLinks);
		$pager = $pagerNav->getPager();
		$allItems = $this->find($noLimit)->explode('title');
		$itemsPerPage = $object->itemsPerPage;
		foreach($pager as $key => $item) {
			if(!ctype_digit("$item->label")) continue;
			$index = (int) $item->label;
			$itemTitle = $allItems[($index - 1) * $itemsPerPage];
			$item->label = substr($itemTitle, 0, 2);
		}

If anyone wants the full hook code, let me know, but you will probably want to modify it for your own use anyway and the above are the key bits.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

On 3/9/2025 at 9:32 PM, MarkE said:

If anyone wants the full hook code, let me know, but you will probably want to modify it for your own use anyway and the above are the key bits.

Publish it as a gist on Github or in case you are interested write a full recipe and publish it on processwire.recipes.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...