Page methods
Page methods are registered with the pageMethods
extension.
Default page methods
For a full list of default page methods, please check out the Reference.
Be aware that - contrary to page models - custom page methods cannot override default page methods.
Custom page methods vs. page models
Page models are a great way to create an extended version of the default page object with additional methods and functionalities. But a page model is tied to a specific template, while custom page methods apply to all page objects. If your use case is based on one specific template, a page model might be the better solution. If you have methods that should be available for all page objects in all templates, a custom page method is the way to go.
Getting started
You can extend the set of defined page methods very easily in a plugin file.
This example shows the basic architecture of a page method. You define the method name with the key for the pageMethods
extension array. $this
in the callback function is the $page
object.
Return options
There are two common scenarios, what page methods can do:
1. Modifying the page
If you want to make it possible to chain page methods and that the page can be further modified by other page methods, you must modify the page and then return the page object ($this
).
Example
2. Returning info about the page
Page methods can also be used to make if clauses easier.
Example
Working with method arguments
In some cases it might be helpful to be able to pass arguments to the method:
The definition for such a method with arguments is very simple: