Field methods
Field methods are registered with the fieldMethods
extension.
Default field methods
For a full list of default field methods, please check out the Reference.
Be aware that you cannot override these default field methods with any custom field method.
Getting started
This example shows the basic architecture of creating a new field method. You define the method name with the array key for the fieldsMethod
array. The callback function receives the $field
object as first argument.
How to use your new field method
The $field
object
The object passed to the callback function gives you access to three important attributes.
Attribute | Description |
---|---|
$field->key() |
Name of the field |
$field->value |
Raw content of the field |
$field->model() |
Parent object: $page , $site , $file or $user |
Return options
There are three common scenarios, what field methods can do:
1. Modifying the field for further chaining
If you want to make it possible that the field value can be further modified by other field methods, you must modify the field value by overwriting/modifying $field->value
and returning the $field
object.
Example
2. Directly returning a modified value
When you directly return the modified value, further chaining is not possible.
Example
3. Returning info about the field
Field methods can also be used to make if clauses easier or return info about a value, such as the length or the readingtime.
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: