Skip to content

Commit 666743c

Browse files
authored
Merge pull request #551 from Promptless/promptless/pr-15855-token-formatting
docs: Document entity token formatting with getFormattedTokens() method
2 parents e9e6dda + e941c5c commit 666743c

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

docs/plugin_extensions/emails.rst

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,169 @@ This pattern lets you add new per-Email settings and surface them in a Symfony f
128128
129129
.. vale off
130130
131+
Database-sourced tokens with BuilderTokenHelper
132+
===============================================
133+
134+
.. vale on
135+
136+
When your tokens represent database entities like Landing Pages, Assets, or Forms, use the ``BuilderTokenHelper`` to query and format them. This helper handles permission checks, search filtering, and consistent label formatting.
137+
138+
Getting the helper
139+
------------------
140+
141+
Inject ``BuilderTokenHelperFactory`` and create a helper for your entity type:
142+
143+
.. code-block:: PHP
144+
145+
<?php
146+
147+
// plugins/HelloWorldBundle/EventListener/BuilderSubscriber.php
148+
149+
declare(strict_types=1);
150+
151+
namespace MauticPlugin\HelloWorldBundle\EventListener;
152+
153+
use Mautic\CoreBundle\Helper\BuilderTokenHelperFactory;
154+
use Mautic\EmailBundle\EmailEvents;
155+
use Mautic\EmailBundle\Event\EmailBuilderEvent;
156+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
157+
158+
final class BuilderSubscriber implements EventSubscriberInterface
159+
{
160+
public function __construct(
161+
private BuilderTokenHelperFactory $builderTokenHelperFactory,
162+
) {
163+
}
164+
165+
public static function getSubscribedEvents(): array
166+
{
167+
return [
168+
EmailEvents::EMAIL_ON_BUILD => ['onEmailBuild', 0],
169+
];
170+
}
171+
172+
public function onEmailBuild(EmailBuilderEvent $event): void
173+
{
174+
// Create a helper for the 'page' model
175+
$tokenHelper = $this->builderTokenHelperFactory->getBuilderTokenHelper('page');
176+
}
177+
}
178+
179+
.. vale off
180+
181+
Formatting tokens with getFormattedTokens()
182+
-------------------------------------------
183+
184+
.. vale on
185+
186+
Use ``getFormattedTokens()`` to retrieve tokens with properly formatted labels. This method accepts ``TokenFormatOptions`` to control the label format:
187+
188+
.. code-block:: PHP
189+
190+
<?php
191+
192+
use Mautic\CoreBundle\DTO\TokenFormatOptions;
193+
use Mautic\CoreBundle\Event\BuilderEvent;
194+
use Mautic\CoreBundle\Helper\BuilderTokenHelperFactory;
195+
196+
public function onBuilderBuild(BuilderEvent $event): void
197+
{
198+
$tokenHelper = $this->builderTokenHelperFactory->getBuilderTokenHelper('page');
199+
$tokenFilter = $event->getTokenFilter();
200+
201+
// Get tokens with formatted labels like "Page link: my-landing-page (123)"
202+
$tokens = $tokenHelper->getFormattedTokens(
203+
'pagelink=(\d+)', // Token regex pattern
204+
TokenFormatOptions::linkWithId('mautic.page.token.pagelink', 'pagelink=(\d+)'), // Format options
205+
'label' === $tokenFilter['target'] ? $tokenFilter['filter'] : '', // Search filter
206+
'title' // Label column
207+
);
208+
209+
if ($tokens) {
210+
$event->addTokens($tokens);
211+
}
212+
}
213+
214+
Token format options
215+
--------------------
216+
217+
The ``TokenFormatOptions`` class provides two factory methods for common label formats:
218+
219+
.. vale off
220+
221+
**Simple prefix** - Creates labels like ``Form: Contact Form``:
222+
223+
.. vale on
224+
225+
.. code-block:: PHP
226+
227+
TokenFormatOptions::simplePrefix('mautic.form.form')
228+
// Result: "Form: Contact Form"
229+
230+
.. vale off
231+
232+
**Link with ID** - Creates labels like ``Page link: my-landing-page (456)`` for linkable entities:
233+
234+
.. vale on
235+
236+
.. code-block:: PHP
237+
238+
TokenFormatOptions::linkWithId('mautic.page.token.pagelink', 'pagelink=(\d+)')
239+
// Result: "a:Page link: my-landing-page (456)"
240+
241+
The ``a:`` prefix indicates a link token. The second parameter is a regular expression pattern to extract the entity ID from the token.
242+
243+
Token sorting
244+
-------------
245+
246+
.. vale off
247+
248+
Mautic automatically groups tokens in the builder dropdown by type, such as Contact, Company, Owner, Page link, Dynamic Content, Focus Item, Asset, This page, Email. Custom or unrecognized tokens fall into an 'Other' group.
249+
250+
.. vale on
251+
252+
Within the Contact group, First Name, Last Name, and Title appear first. Tokens then sort alphabetically by label within each group. The ``TokenSorter`` class constants in Mautic core define the authoritative order.
253+
254+
Deprecated methods
255+
------------------
256+
257+
.. deprecated:: 7.2
258+
259+
Mautic deprecates the following ``BuilderEvent`` methods and removes them in a future version:
260+
261+
- ``addTokensFromHelper()`` - Use ``BuilderTokenHelper::getFormattedTokens()`` and ``$event->addTokens()`` instead.
262+
- ``getTokensFromHelper()`` - Use ``BuilderTokenHelper::getFormattedTokens()`` instead.
263+
264+
**Migration example:**
265+
266+
.. code-block:: PHP
267+
268+
// Before (deprecated):
269+
public function onEmailBuild(EmailBuilderEvent $event): void
270+
{
271+
$event->addTokensFromHelper($tokenHelper, $tokenRegex, 'label', 'alias');
272+
}
273+
274+
// After:
275+
public function onEmailBuild(EmailBuilderEvent $event): void
276+
{
277+
$tokenHelper = $this->builderTokenHelperFactory->getBuilderTokenHelper('page');
278+
$tokenFilter = $event->getTokenFilter();
279+
$tokens = $tokenHelper->getFormattedTokens(
280+
$tokenRegex,
281+
TokenFormatOptions::simplePrefix('mautic.page.page'),
282+
'label' === $tokenFilter['target'] ? $tokenFilter['filter'] : '',
283+
'label', // Label column
284+
'alias' // Value column
285+
);
286+
287+
if ($tokens) {
288+
$event->addTokens($tokens);
289+
}
290+
}
291+
292+
.. vale off
293+
131294
.. _Email A/B testing:
132295

133296
Email A/B testing

0 commit comments

Comments
 (0)