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
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
Comment #3
megachrizA possible fix is added to the issue branch "3224959-multivalue-rewrite".
Comment #4
megachrizArray replacing is now supported.
If the source data is:
And you apply a the Rewrite plugin to the source 'name' using the text
[name].[ext]You will get:
I've also been working on supporting nested data, if you use the text
[name].[ext.1]instead, you should get:For this I've not added test coverage yet. Marking as "needs work" for additional test coverage.
Comment #5
megachrizComment #8
megachriz@codebymikey
Thanks for your review! I did not notice it earlier, since the issue did not get updated.
I merged the code!