Rewrite uses the key/value pairs from the source for string translations. This may not be correct if the source has previously been exploded; the value is still the unexploded value.

I'm creating a patch that uses the passed $data instead of the value, if the key matches the source. I'm not at all sure if this is the correct approach, but it's making sense for the project I work on. Open to other ideas.

CommentFileSizeAuthor
#2 rewrite_after_explode-3065458-2.patch573 bytesdkosbob

Comments

dkosbob created an issue. See original summary.

dkosbob’s picture

StatusFileSize
new573 bytes
chegor’s picture

Just checked that patch works correctly with the latest version of the module

git apply -v rewrite_after_explode-3065458-2.patch
Checking patch src/Plugin/Tamper/Rewrite.php...
Applied patch src/Plugin/Tamper/Rewrite.php cleanly.

jamesdixon’s picture

Thanks.

We should write a test to make sure the rewrite plugin works after the explode plugin as expected.

Maybe something like:

0) Make sure [enemy] token is set to string "ghost"
1) Source is: "Pacman, yellow ghost, red, ghost, blue ghost"
2) Explode into ["Pacman", "yellow [enemy]", "red [enemy]", "blue [enemy]"]
3) Use rewrite to update values to:
4) ["Pacman", "yellow ghost", "red ghost", "blue ghost"]

jamesdixon’s picture

Sorry should be:

0) Make sure [enemy] token is set to string "ghost"
1) Source is: "Pacman, yellow [enemy], red [enemy], blue [enemy]"
2) Explode into ["Pacman", "yellow [enemy]", "red [enemy]", "blue [enemy]"]
3) Use rewrite to update values to:
4) ["Pacman", "yellow ghost", "red ghost", "blue ghost"]
tostinni’s picture

@jamesdixon do you have any advice on how to link plugins in tests ?
I tried to write this test but it's not working :

  /**
   * Tests if rewrite is working after explode.
   */
  public function testRewriteAfterExplode() {
    $source = "Pacman, yellow [enemy], red [enemy], blue [enemy]";
    $expected = ["Pacman", "yellow ghost", "red ghost", "blue ghost"];

    $explode_config = [
      Explode::SETTING_SEPARATOR => ', ',
    ];
    $explode_plugin = new Explode($explode_config, 'explode', [], $this->getMockSourceDefinition());
    $explode_result = $explode_plugin->tamper($source);

    $config = [
      Rewrite::SETTING_TEXT => '[source]',
    ];
    $plugin = new Rewrite($config, 'rewrite', [], $this->getMockSourceDefinition());
    $this->assertEquals($expected, $plugin->tamper($explode_result));

  }
There was 1 failure:

1) Drupal\Tests\tamper\Unit\Plugin\Tamper\RewriteTest::testRewriteAfterExplode
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     0 => 'Pacman'
-    1 => 'yellow ghost'
-    2 => 'red ghost'
-    3 => 'blue ghost'
+    1 => 'yellow [enemy]'
+    2 => 'red [enemy]'
+    3 => 'blue [enemy]'
 )
megachriz’s picture

Status: Active » Closed (duplicate)

A fix for this issue is now available in a combination of two other issues:

Closing this as a duplicate now.