Modifiers
On this page

Modifiers

This page is for the PHP developer who needs a change to a value that the store can put on any mapping row, the way it puts truncate:150 or strip_tags. A modifier is one such step. Once registered, it is listed in the menu of the Modifiers column, behind the + of every row, and it works everywhere a modifier does:

  • as a chip on a mapping row, after the row's source and dictionary;
  • in a template placeholder, after a pipe: {name|words:12};
  • in a field catalog's default_modifiers; see Field catalogs.

The module registers trim, lower, upper, ucfirst, strip_tags, truncate, replace, default, prefix, suffix, number, date, currency and join. For join, see Join a list. See Modifiers for the others. An item under one of those names replaces the module's modifier.

date preserves blank, unparseable and impossible date values. Calendar rollover is rejected without changing the original value. Valid compact Ymd dates, Unix timestamps and date/time strings are formatted normally. A following default replaces null, false, an empty string or an empty list. Required also treats whitespace-only text as blank and does not validate dates. This modifier is shared with Notification templates.

Join a list

join turns a list into one text, using a comma when you omit the separator. Use join:";" for semicolons or join:"|" for pipes. It flattens nested lists, converts scalar members to text and drops nulls and members whose converted text is empty, keeping 0. An empty list becomes an empty string. A scalar passes through unchanged; a list containing an object or another value it cannot convert passes through unchanged as a whole.

Pair it with all_images for several image URLs in one column. See Main and additional images. For the store settings, see European marketplaces.

Write a modifier

The following modifier keeps the first words of a text, words:12 keeping twelve:

<?php
declare(strict_types=1);

namespace Vendor\Module\Model\Modifier;

use Arlanix\Core\Model\Template\Modifier\Description;
use Arlanix\Core\Model\Template\Modifier\StringModifier;

class Words extends StringModifier
{
    protected function transform(string $value, array $arguments): string
    {
        $limit = max(1, (int)($arguments[0] ?? 20));
        $words = preg_split('/\s+/u', trim($value), -1, PREG_SPLIT_NO_EMPTY) ?: [];

        return implode(' ', array_slice($words, 0, $limit));
    }

    public function describe(): Description
    {
        return new Description('words:20', __('Keep the first words'), 'words:12');
    }
}

Arlanix\Core\Model\Template\Modifier\StringModifier is the base of the module's own text modifiers. It passes each scalar to transform() as a string, including numbers and booleans. It applies the same rule recursively to lists, such as the labels of a multiselect attribute. Other values, including null, pass through untouched.

A modifier of something other than text implements Arlanix\Core\Model\Template\ModifierInterface itself:

Method Does
apply(mixed $value, array $arguments = []): mixed Returns the changed value. $value is whatever the previous step gave: text, a number, a list or null. Pass on untouched what the modifier has no meaning for, so that one step never breaks a row.

An exception from a modifier stops the whole run, so return the value unchanged rather than throw.

How arguments arrive

$arguments holds the arguments as they are written after the name, as strings, in order: words:12 gives ['12'], replace:a:b gives ['a', 'b']. A missing argument is absent from the array, so give each one a default. An argument holding a : or a |, or starting or ending with a space, is written in quotes, suffix:' | ', and arrives without them.

The parser checks the syntax and the pool checks the modifier's name when the feed is saved. Your modifier interprets the argument values when it runs. Read them leniently, as max(1, (int)…) does in the example.

Describe it

describe(): Description, from Arlanix\Core\Model\Template\Modifier\DescribesItselfInterface, which StringModifier implements, gives the modifier menu its line:

Description argument Shown
signature How it is written, arguments included: words:20. The menu lists modifiers by it.
summary One line, in the imperative: Keep the first words. The menu shows it beside the signature, and a chip shows it when pointed at.
example A step that works as it is, words:12, which the menu inserts when it is chosen.

A modifier that does not describe itself is listed by its name alone, and choosing it inserts the bare name.

Register it

In your module's etc/di.xml, add the modifier to ModifierPool under its name:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Arlanix\Core\Model\Template\ModifierPool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="words" xsi:type="object">Vendor\Module\Model\Modifier\Words</item>
            </argument>
        </arguments>
    </type>
</config>

The name is what a mapping row stores. Use lower-case letters, digits and underscores: a name ends at the first unescaped :, |, { or }.

Then run bin/magento setup:upgrade and bin/magento cache:clean config. Open a feed, press + in the Modifiers column of a row, choose words:20, save, and check the value with Preview. A mapping that names a modifier no installed module registers is refused when the feed is saved, naming the row.

After changing an existing modifier's implementation, save the feeds that use it. This advances their mapping version so cached Merchant API rows are rebuilt on the next scheduled delivery. Cleaning the configuration cache alone does not rebuild those rows.