Twig plugin: Provide a twig rewrite plugin

Created on 13 June 2025, about 1 month ago

Problem/Motivation

The Rewrite plugin is sufficient for very simple cases, however sometimes it'd be nice to manipulate the results using if else conditions, similar to how Views rewrites work.

Proposed resolution

Provide a plugin for twig support.

Remaining tasks

Provide issue fork/patch.

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @codebymikey
  • 🇳🇱Netherlands megachriz

    Sounds like a good idea! Do you need any pointers to start with writing such a plugin?

  • No thanks, I'll push an issue fork/MR for it in due course.

  • 🇳🇱Netherlands megachriz

    If you could add tests for it too, that would be great! (A unit test and a functional test are required in order for the code to be merged.)

  • @codebymikey opened merge request.
  • 🇳🇱Netherlands megachriz

    Thanks for providing code and tests! Looks good at first glance.

    I think that the tests fail because of the following warning:

    1 test triggered 1 PHP warning:
    1) /builds/issue/tamper-3530101/vendor/twig/twig/src/Template.php:345
    Array to string conversion
    Triggered by:
    * Drupal\Tests\tamper\Unit\Plugin\Tamper\TwigTest::testTwig
    /builds/issue/tamper-3530101/tests/src/Unit/Plugin/Tamper/TwigTest.php:93

  • 🇳🇱Netherlands megachriz

    Very well made! I merged it.

    Possible improvement for a follow-up: allow arrays as replacement patterns. The Rewrite plugin also is limited in this regard.

    Let's say you have two sources called "name" and "extension" and you have the following lines in the CSV:

    name,extension
    foo|bar,jpg|png
    baz,gif
    

    You might want that to result into:

    I assume that is what is 🐛 The rewrite plugin renames multiple values with the same (first) row Active about.

  • One thing I forgot to address with this was to ensure the template in final YAML config doesn't have \r\n characters since that causes the template to be exported as a single line rather than a more human-friendly format. This should probably be addressed in a new issue since it probably affects the rewrite plugin too.

    $this->setConfiguration([
      static::SETTING_TEMPLATE => str_replace(["\r\n", "\r"], "\n", $form_state->getValue(static::SETTING_TEMPLATE)),
    ]);
    
    Possible improvement for a follow-up: allow arrays as replacement patterns.

    And that's an interesting scenario, this is untested, but that's the beauty of now having Twig available, that scenario should be something users can potentially solve by composing multiple tamper plugins together to achieve the desired effect:
    1. Have something similar to the Migrate Plus Transpose plugin to explode then map "name" and "extension" into a single array, then work with it that way (Optional) - On a side note, I think most of the process plugins from the Migrate module are useful inspirations for the Tamper module.
    2. Twig plugin: create a multiline string of the items.

    {# This assumes names and extensions are always the same length #}
    {% set names = name|split('|') %}
    {% set extensions = extension|split('|') %}
    {% for i, name in names %}
    {{ name }}.{{ extensions[i] }}
    {% endfor %}
    

    3. Explode plugin: explode items by new line into multiple values per row.

    If it's not possible at the moment, then we'd just need to introduce a new plugin that makes it possible to achieve that.

Production build 0.71.5 2024