Issue #2570895 by alexpott: FieldPluginBase can duplicate a suffix

8.0.x
webchick 2015-10-04 23:55:12 -07:00
parent 9bd9390755
commit aced68bf3f
2 changed files with 48 additions and 11 deletions

View File

@ -1260,7 +1260,7 @@ abstract class FieldPluginBase extends HandlerBase implements FieldHandlerInterf
$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 @@ abstract class FieldPluginBase extends HandlerBase implements FieldHandlerInterf
// @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 @@ abstract class FieldPluginBase extends HandlerBase implements FieldHandlerInterf
$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 @@ abstract class FieldPluginBase extends HandlerBase implements FieldHandlerInterf
$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;
}
}

View File

@ -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 @@ class FieldPluginBaseTest extends UnitTestCase {
$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 @@ class FieldPluginBaseTestField extends FieldPluginBase {
}
}
}
// @todo Remove as part of https://www.drupal.org/node/2529170.
namespace {
if (!function_exists('base_path')) {
function base_path() {
return '/';
}
}
}