Jump to content

How to make a custom admin page with predefined ProcessPageLister configuration


TomPich
 Share

Recommended Posts

I received so much help from kind people on this forum; it’s now my turn to contribute. ?
I recently had a need for a customer. They have a long list of articles, and they wanted to find any article corresponding to some criteria. They could use the Pages > find tool, which use pageLister, but then they would have to each time select some filters, which can be quite intimidating when you have many fields.
So my idea was to create an admin page with the right filters already set. The customer only needs to use some of them. As I found out, this is much simpler than I first thought. So my tutorial is not rocket science, but my hope is that it helps newbies like me to achieve this... ?

So first, you need to create a simple custom admin page. Bernhard already made a nice tutorial about this, and it’s quite straightforward (like everything in PW ❤️).

Here is the code to create this custom admin page in our situation (thanks to Bernhard) :

namespace ProcessWire;

class ProcessArticlesList extends Process
{

	// Infos about your module and some settings
	public static function getModuleinfo()
	{
		return [
			'title' => 'Articles Admin Page',
			'summary' => 'No need to be afraid of building custom admin pages. ;-)',
			'author' => 'Your name goes here',
			'version' => 1,

			// you can set permissions here
			'permission' => 'meeting-view',
			'permissions' => [
                'meeting-view' => 'View Meetings page',
            ],

			// page that you want created to execute this module
			'page' => [
				// your page will be online at /processwire/articles/
				'name' => 'articles',
				// page title for this admin-page
				'title' => 'Articles',
			],
		];
	}

	public function ___execute()
	{
		// the logic goes here
	}

}

So, with this code, you get a new module that will produce a blank page. You have now to create a ProcessArticlesList folder inside that file in the site/modules folder and put your new file inside.
Then, activate your module.

The ___execute() method will run on page load. It’s supposed to return a string which will be your page content. Now things get quite... simple! ?
The idea is to run an instance of ProcessPageLister, with custom parameters. You can find all these parameters in /wire/modules/Process/ProcessPageLister/ProcessPageLister.module (there are comments that really help you) and you can also have a look at the API doc about ProcessPageLister.
Here is an example that covered my needs :

public function ___execute()
{
	// this is to avoid error detection in your IDE
	/** @var \ProcessPageLister $lister */

	// get the module, so that you can use it the way you want.
	$lister = $this->modules->get("ProcessPageLister");

	// from here, the comments in the source file were self explanatory, so I found how to cover my needs

	// filter all pages with the "meeting" template. This filter is by default, won’t show
	// in the filter list and cannot be changed.
	$lister->set('initSelector', "template=meeting");

	// Then I set 3 filters, left blank, so that the user has just to fill them – or leave them blank
	$lister->set('defaultSelector', "title%=, themes.title%=, text%=");

	// Disallow bookmark creation. In my use case, there was no sense for that.
	$lister->set('allowBookmarks', false);

	// let the table be full width
	$lister->set('responsiveTable', false);

	// use the custom order defined by the user by moving the pages in the page tree
	$lister->set('defaultSort', 'sort');

	// and just show the configured filter on the page.
	return $lister->execute(); 
}

As you can see, there are 7 lines of specific code to achieve that. How elegant! ?

Hope that it is helpful to someone somehow.

Cheers
Thomas

  • Like 7
  • Thanks 3
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...