In this tutorial we’re going to look into extending ProcessWire’s admin using custom modules. With three example modules I will give you small a taste of the power ProcessWire modules have for adding new functionality to the admin.
Create Custom Admin Pages
Creating pages for your website in ProcessWire (PW) couldn’t be easier, but what about making custom pages in the CMS? Say you want to make a custom settings page, FAQs or video page?
Read on, we’ve got your back.
The module ProcessSimpleAdminPage shows simply to create a module that extends the Process class to add a new admin Process, and then create a page for the new Process. Process is the base class for the PW’s admin so using it we can create a page that shows our content.
Tip: Your new Process extending module can also display forms and other functionality as well as return content.
We’re going to create a new Process that returns some content, then create a new page in the admin navigation to reach that content (in this example I’ve named the page “CMS FAQ”). To build our module we start by creating a file called “ProcessSimpleAdminPage.module” and saving our module details to it.
At this point, if we installed our module we would have everything we need to create our new Process ProcessSimpleAdminPage and return the content. However, there is currently no page to execute our code. We have several options to do this:
Manually create a new page in the PW pagetree under Admin, give it the template admin and in the field Process: ProcessSimpleAdminPage. Moving the new page in the pagetree list under Settings, for example, will see the page added to the top navigation under Settings.
Add a page attribute to the module’s getModuleInfo() array. If the module extends Process (which ours does), then adding a page attribute array with at least a name and parent on install will automatically create a new admin page under your specified parent with the module as its Process. Here is an example from the hello module.
Create install() and uninstall() functions and add the new page. This is detailed below to show you how you might add a page:
Go forth and create admin pages for your website!
Add Custom Text Formatter
By using Textformatters you can manipulate the input of text fields with the PW admin. For example, say you want to create snippets of code that are added instead of someone typing their own snippets, it’s perfectly possible (in a similar way to the hana code plugin for Wordpress).
This time I’ve created a module that extends another base class Textformatter to create our new module TextformatterFindReplace and created a separate config file:
Now in the module settings I can specify a string to match, and a string to replace it with (in our example above the default is find: ---- and replace with <b>REPLACED TEXT</b>).
All I then need to do is add our new TextFormatter to a text field within the field’s details tab. Now every time I type ---- I will get the replaced text string.
Add New Functionality to Admin Pages
As the PW admin is itself created using PW’s API-like library, PW modules have pretty much access to hook into anywhere you need to.
For my last example on extending PW’s admin, I’ve created a module that adds buttons to the edit page and pagetree, creates two new fields and adds them globally to every page. It requires another module, LazyCron, to operate.
The module PageDeferredPublish, on clicking one of the Publish Later buttons, sets LazyCron to check that page’s countdown every minute and publishes the page when its countdown reaches 0. This means I can publish a page approximately 24 hours in advance (obviously the checking interval and delay time can be changed to your requirements).
I did this by:
Creating two fields within my install() function: a checkbox field to set to true when a button is checked to indicate the page should countdown, and a countdown field to store the count in seconds for that specific page.
Adding hooks to both ProcessPageEdit::buildForm and ProcessPageListActions::getExtraActions enabling me to add the two buttons.
Checking to see if one of the buttons was clicked with my ready() function then setting the corresponding page’s checkbox to true.
Using a LazyCron hook function to check all pages that are unpublished for true checkboxes and then comparing the seconds field to see if the page needs publishing. If not then deduct the elapsed time in seconds.
The result is a module that has settings (the time interval to check and publish at a later time), hooks into the admin using buttons and fields, and enables us to use other modules installed in PW (i.e. LazyCron).
Wrapping up
Often, a module you need has already been created and can be downloaded from PW’s module page. However, if you find yourself in need it’s nice to know it is relatively easy to jump in, extend a core class and create new functionality that solves your problem or alters PW’s default solution.
I hope you’ve found this small taster of the power of extending PW useful, and please check out the other ProcessWire tutorials on Envato Tuts+ for more tips and tricks.
Useful Resources
A Beginner’s Introduction to Writing Modules in ProcessWire
Module docs
Module custom config
Module dependencies
Implementable hooks