Skip to content

Commit 0283be6

Browse files
committed
variable modifier updates
1 parent 46ca40b commit 0283be6

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ As of 3.1.0 fieldtypes can specify their compatibility. When editing a Channel F
114114
| relationship | [Relationships](https://docs.expressionengine.com/latest/fieldtypes/relationships.html) |
115115
| 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) |
116116

117+
### `modifiers`
118+
119+
'modifiers' => array(
120+
'modifier_name',
121+
'another_modifier_name'
122+
)
123+
124+
This property lists the [variable modifiers](development/modifiers.md) that the add-on provides
125+
117126
### `services`
118127

119128
'services' => array(

docs/development/modifiers.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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-2021, 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+
The add-ons can provide their own [variable modifiers](templates/variable-modifiers.md)
13+
14+
Each variable modifier needs be created as a separate file in `Modifiers` directory within add-on's own folder and registered in `addon.setup.php`.
15+
16+
The file name (which will also be PHP class name) should be the modifier's name with first letter capitalized.
17+
18+
All modifier files are required to implement `ExpressionEngine\Service\Template\Variables\ModifierInterface`.
19+
20+
Each widget should have `namespace` definition, which should consist 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-on 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:seo_hacker}` to achieve same result
25+
26+
### Example
27+
28+
Let's create `:hacker` modifier which would make text look geeky by converting some letters to numbers that look similarly. The modifier would be part of "Seeo" add-on.
29+
30+
<?php
31+
32+
/**
33+
* namespace is required and must be add-on's namespace + 'Modifiers'
34+
*
35+
*/
36+
<?php
37+
38+
namespace EEHarbor\Seeo\Modifiers;
39+
40+
use ExpressionEngine\Service\Template\Variables\ModifierInterface;
41+
42+
class Hacker implements ModifierInterface
43+
{
44+
public function modify($data, $params = array(), $tagdata = false)
45+
{
46+
return str_replace(['e', 'o', 'l'], ['3', '0', '1'], (string) $data);
47+
}
48+
}
49+
50+
Next, we'll add the following to `addon.setup.php`
51+
52+
'modifiers' => array(
53+
'hacker'
54+
),
55+
56+
And now, let's call it in template.
57+
58+
{exp:channel:entries entry_id="1"}
59+
<div class="title">
60+
<span>{title}</span> - Hello
61+
</div>
62+
<div class="hacker">
63+
<span>{title:hacker}</span> - H3110
64+
</div>
65+
<div class="seeo_hacker">
66+
<span>{title:seeo_hacker}</span> - H3110
67+
</div>
68+
{/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 modifier name after variable name, separated by semicolon, e.g. `var_name:trim`.
28+
29+
It is possible to apply several modifiers at the same time by chaing 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+
When applying several modifiers, there could be conflict in parameter names. To avoid that, the parameters could be prefixed with 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'}`. Prefixed parameter has higher precedence over non-prefixed one.
32+
2533
## Modifiers
2634

2735
[TOC=3]

0 commit comments

Comments
 (0)