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

Issue fork tamper-3224959

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

MegaChriz created an issue. See original summary.

megachriz’s picture

Status: Active » Needs review
Issue tags: +Needs tests

A possible fix is added to the issue branch "3224959-multivalue-rewrite".

megachriz’s picture

Status: Needs review » Needs work

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.

megachriz’s picture

Status: Needs work » Needs review

  • megachriz committed 61d8d5e5 on 8.x-1.x
    Issue #3224959 by megachriz, codebymikey: Rewrite plugin: iterate...

megachriz’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

@codebymikey
Thanks for your review! I did not notice it earlier, since the issue did not get updated.

I merged the code!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.