Rewrite plugin: iterate through values to rewrite

Created on 22 July 2021, about 4 years ago
Updated 17 July 2025, 17 days ago

Problem/Motivation

When a source field to rewrite is multivalued, the same value is written for every item in the source field.

Steps to reproduce

For example, take the following XML data:

<root>
  <item>
    <Images>
      <Image>
        <filename>foo.jpg</filename>
        <width>500</width>
        <height>268</height>
        <filesize>52470</filesize>
        <type>2</type>
        <view>1</view>
        <orientation>1</orientation>
        <created_at>2015-12-03 08:57:58</created_at>
      </Image>
      <Image>
        <filename>bar.jpg</filename>
        <width>500</width>
        <height>239</height>
        <filesize>24434</filesize>
        <type>2</type>
        <view>2</view>
        <orientation>1</orientation>
        <created_at>2016-02-09 13:56:17</created_at>
      </Image>
    </Images>
  </item>
</root>

Imagine that for a certain source field the filename is selected with the following xpath query Images/Image/filename.

The replacement pattern for Rewrite is then configured as follows:
https://www.example.com[images_image_filename]

Output before applying the Rewrite plugin:

  • foo.jpg
  • bar.jpg

Output after applying the Rewrite plugin:

This is because the Rewrite plugin picks the first element of the replacement source field if that one is an array:

$trans['[' . $key . ']'] = is_array($value) ? reset($value) : $value;

Proposed resolution

A fix that would hopefully fix most use cases (but not all), is to allow using the passed in data as replacement. The Tamper plugin now doesn't use $data at all. This only fixes the issue where the source field to rewrite itself is used as replacement pattern, not when data from other source fields are needed. But this would be at least a good start, I hope.

Remaining tasks

  • Figure out how to properly rewrite multivalued sources when using array data from other source fields

User interface changes

API changes

Data model changes

Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

🇳🇱Netherlands megachriz

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇱Netherlands megachriz

    Array replacing is now supported.

    If the source data is:

    [
      'name' => [
        'foo',
        'bar',
        'qux',
      ],
      'ext' => [
        'jpg',
        'png',
        'gif',
      ],
    ]
    

    And you apply a the Rewrite plugin to the source 'name' using the text [name].[ext]

    You will get:

    [
      'name' => [
        'foo.jpg',
        'bar.png',
        'qux.gif',
      ],
      'ext' => [
        'jpg',
        'png',
        'gif',
      ],
    ]
    

    I've also been working on supporting nested data, if you use the text [name].[ext.1] instead, you should get:

    [
      'name' => [
        'foo.png',
        'bar.png',
        'qux.png',
      ],
      'ext' => [
        'jpg',
        'png',
        'gif',
      ],
    ]
    

    For this I've not added test coverage yet. Marking as "needs work" for additional test coverage.

Production build 0.71.5 2024