Skip to content

Commit 874c968

Browse files
authored
Merge pull request #546 from ExpressionEngine/feature/7.x/modifier-updates
variable modifier updates
2 parents c0d7a54 + 1caff04 commit 874c968

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

docs/development/addon-setup-php-file.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ As of 3.1.0 fieldtypes can specify their compatibility. When editing a Channel F
119119
| relationship | [Relationships](https://docs.expressionengine.com/latest/fieldtypes/relationships.html) |
120120
| text | [Email Address](fieldtypes/email-address.md), [Rich Text Editor](fieldtypes/rte.md), [Text Input](fieldtypes/text.md), [Textarea](fieldtypes/textarea.md),[URL](fieldtypes/url.md) |
121121

122+
### `modifiers`
123+
124+
'modifiers' => array(
125+
'modifier_name',
126+
'another_modifier_name'
127+
)
128+
129+
This property lists the [variable modifiers](development/modifiers.md) that the add-on provides.
130+
122131
### `services`
123132

124133
'services' => array(

docs/development/modifiers.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!--
2+
This source file is part of the open source project
3+
ExpressionEngine User Guide (https://github.com/ExpressionEngine/ExpressionEngine-User-Guide)
4+
5+
@link https://expressionengine.com/
6+
@copyright Copyright (c) 2003-2023, Packet Tide, LLC (https://packettide.com)
7+
@license https://expressionengine.com/license Licensed under Apache License, Version 2.0
8+
-->
9+
10+
# Developing Variable Modifiers
11+
12+
Add-ons can provide their own [variable modifiers](templates/variable-modifiers.md) for use in templates.
13+
14+
Each variable modifier needs be created as a separate file in the `Modifiers` directory within the add-on's root folder, and registered in `addon.setup.php`.
15+
16+
The file name, which is also the PHP class name, should be the modifier's name with the first letter capitalized.
17+
18+
All modifier files are required to implement `ExpressionEngine\Service\Template\Variables\ModifierInterface`.
19+
20+
Each modifier should have a `namespace` definition, which consists of the add-on's namespace as defined in `addon.setup.php` followed by `\Modifiers`.
21+
22+
Lastly, the modifier's name should be registered in `addon.setup.php`.
23+
24+
TIP: **Tip:** Modifiers provided by add-ons can be called by their name as well as by their name prefixed with add-on's name and underscore. For example, below we can use `{title:hacker}` and `{title:seeo_hacker}` to achieve the same result.
25+
26+
### Example
27+
28+
Let's create the `:hacker` modifier, which would make text look geeky by converting some of the letters to similar looking numbers. This example modifier is part of the "Seeo" add-on.
29+
30+
<?php
31+
32+
/**
33+
* namespace is required and must be add-on's namespace + 'Modifiers'
34+
*
35+
*/
36+
namespace EEHarbor\Seeo\Modifiers;
37+
38+
use ExpressionEngine\Service\Template\Variables\ModifierInterface;
39+
40+
class Hacker implements ModifierInterface
41+
{
42+
public function modify($data, $params = array(), $tagdata = false)
43+
{
44+
return str_replace(['e', 'o', 'l'], ['3', '0', '1'], (string) $data);
45+
}
46+
}
47+
48+
Next, we'll add the following to `addon.setup.php`
49+
50+
'modifiers' => array(
51+
'hacker'
52+
),
53+
54+
And now, let's call it in a template.
55+
56+
{exp:channel:entries entry_id="1"}
57+
<div class="title">
58+
<span>{title}</span> - Hello
59+
</div>
60+
<div class="hacker">
61+
<span>{title:hacker}</span> - H3110
62+
</div>
63+
<div class="seeo_hacker">
64+
<span>{title:seeo_hacker}</span> - H3110
65+
</div>
66+
{/exp:channel:entries}

docs/templates/variable-modifiers.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Most template variables can be modified for common formatting and output needs w
2222

2323
NOTE: **Note:** Some add-ons and components may have modifiers not listed here. For instance the [File Fieldtype](fieldtypes/file.md) has its own file information-related modifiers. The modifiers listed here are just those universally available.
2424

25+
## Modifiers syntax
26+
27+
The modifiers are being applied by adding the modifier name after the variable name, separated by a semicolon, e.g. `var_name:trim`.
28+
29+
It is possible to apply several modifiers at the same time by chaining those, e.g. `var_name:trim:url_encode`. The modifiers would be applied left to right, so in this case the variable's content will first be trimmed and then URL-encoded.
30+
31+
To avoid conflicts in parameter names when applying several modifiers, the parameters can be prefixed with the modifier name followed by semicolon. E.g. `{excerpt:limit characters='20'}` could be written as `{excerpt:limit limit:characters='20'}`. This allows writing constructions such as `{excerpt:limit:trim limit:characters='20' trim:characters='\n\r'}`. A prefixed parameter has a higher precedence than a non-prefixed parameter.
32+
2533
## Modifiers
2634

2735
[TOC=3]

0 commit comments

Comments
 (0)