From aced68bf3f0f5c9755d8411e8c862624972815c5 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Sun, 4 Oct 2015 23:55:12 -0700
Subject: [PATCH] Issue #2570895 by alexpott: FieldPluginBase can duplicate a
 suffix

---
 .../Plugin/views/field/FieldPluginBase.php    | 16 +++----
 .../Unit/Plugin/field/FieldPluginBaseTest.php | 43 ++++++++++++++++++-
 2 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
index 7cb05a2aa654..7275bc44cdd8 100644
--- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -1260,7 +1260,7 @@ public function renderText($alter) {
       $value = strip_tags($value, $alter['preserve_tags']);
     }
 
-    $suffix = '';
+    $more_link = '';
     if (!empty($alter['trim']) && !empty($alter['max_length'])) {
       $length = strlen($value);
       $value = $this->renderTrimText($alter, $value);
@@ -1281,9 +1281,7 @@ public function renderText($alter) {
 
         // @todo Views should expect and store a leading /. See
         //   https://www.drupal.org/node/2423913.
-        $more_link = \Drupal::l($more_link_text, CoreUrl::fromUserInput('/' . $more_link_path, array('attributes' => array('class' => array('views-more-link')))));
-
-        $suffix .= " " . $more_link;
+        $more_link = ' ' . $this->linkGenerator()->generate($more_link_text, CoreUrl::fromUserInput('/' . $more_link_path, array('attributes' => array('class' => array('views-more-link')))));
       }
     }
 
@@ -1291,10 +1289,8 @@ public function renderText($alter) {
       $value = nl2br($value);
     }
 
-    // Preserve whether or not the string is safe. Since $suffix comes from
-    // \Drupal::l(), it is safe to append.
     if ($value_is_safe) {
-      $value = ViewsRenderPipelineMarkup::create($value . $suffix);
+      $value = ViewsRenderPipelineMarkup::create($value);
     }
     $this->last_render_text = $value;
 
@@ -1305,16 +1301,16 @@ public function renderText($alter) {
       $value = $this->renderAsLink($alter, $value, $tokens);
     }
 
-    // Preserve whether or not the string is safe. Since $suffix comes from
+    // Preserve whether or not the string is safe. Since $more_link comes from
     // \Drupal::l(), it is safe to append. Use SafeMarkup::isSafe() here because
     // renderAsLink() can return both safe and unsafe values.
     if (SafeMarkup::isSafe($value)) {
-      return ViewsRenderPipelineMarkup::create($value . $suffix);
+      return ViewsRenderPipelineMarkup::create($value . $more_link);
     }
     else {
       // If the string is not already marked safe, it is still OK to return it
       // because it will be sanitized by Twig.
-      return $value . $suffix;
+      return $value . $more_link;
     }
   }
 
diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php
index 015c414a9de9..d285021ae57f 100644
--- a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php
+++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php
@@ -5,7 +5,7 @@
  * Contains \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest.
  */
 
-namespace Drupal\Tests\views\Unit\Plugin\field;
+namespace Drupal\Tests\views\Unit\Plugin\field {
 
 use Drupal\Core\GeneratedUrl;
 use Drupal\Core\Language\Language;
@@ -227,6 +227,38 @@ public function testRenderAsLinkWithoutPath() {
     $this->assertEquals($expected_result, $result);
   }
 
+  /**
+   * Test rendering as a link without a path.
+   *
+   * @covers ::renderText
+   */
+  public function testRenderTrimmedWithMoreLink() {
+    $alter = [
+      'trim' => TRUE,
+      'max_length' => 7,
+      'more_link' => TRUE,
+      // Don't invoke translation.
+      'ellipsis' => FALSE,
+      'more_link_text' => 'more link',
+    ];
+
+    $this->display->expects($this->any())
+      ->method('getHandlers')
+      ->willReturnMap([
+        ['argument', []],
+        ['field', []],
+      ]);
+
+    $this->setUpUrlIntegrationServices();
+    $field = $this->setupTestField(['alter' => $alter]);
+    $field->field_alias = 'key';
+    $row = new ResultRow(['key' => 'a long value']);
+
+    $expected_result = 'a long <a href="/%3Cfront%3E" class="views-more-link">more link</a>';
+    $result = $field->advancedRender($row);
+    $this->assertEquals($expected_result, $result);
+  }
+
   /**
    * Test rendering of a link with a path and options.
    *
@@ -586,3 +618,12 @@ public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
   }
 
 }
+}
+// @todo Remove as part of https://www.drupal.org/node/2529170.
+namespace {
+  if (!function_exists('base_path')) {
+    function base_path() {
+      return '/';
+    }
+  }
+}
-- 
GitLab