diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index 54e89ea..46fb11e 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -10,6 +10,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Utility\SafeStringInterface; use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\Xss; @@ -1173,10 +1174,10 @@ public function advancedRender(ResultRow $values) { } // This happens here so that renderAsLink can get the unaltered value of // this field as a token rather than the altered value. - $this->last_render = (string) $value; + $this->last_render = $value; } - if (empty($this->last_render)) { + if (empty((string) $this->last_render)) { if ($this->isValueEmpty($this->last_render, $this->options['empty_zero'], FALSE)) { $alter = $this->options['alter']; $alter['alter_text'] = 1; @@ -1185,9 +1186,6 @@ public function advancedRender(ResultRow $values) { $this->last_render = $this->renderText($alter); } } - // @todo Fix this in https://www.drupal.org/node/2280961. - $this->last_render = SafeMarkup::set($this->last_render); - return $this->last_render; } @@ -1196,6 +1194,10 @@ public function advancedRender(ResultRow $values) { * {@inheritdoc} */ public function isValueEmpty($value, $empty_zero, $no_skip_empty = TRUE) { + // Convert SafeStringInterface to a string for checking. + if ($value instanceof SafeStringInterface) { + $value = (string) $value; + } if (!isset($value)) { $empty = TRUE; } @@ -1231,7 +1233,7 @@ public function renderText($alter) { // Check whether the value is empty and return nothing, so the field isn't rendered. // First check whether the field should be hidden if the value(hide_alter_empty = TRUE) /the rewrite is empty (hide_alter_empty = FALSE). // For numeric values you can specify whether "0"/0 should be empty. - if ((($this->options['hide_empty'] && empty($value)) + if ((($this->options['hide_empty'] && empty((string) $value)) || ($alter['phase'] != static::RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty)) && $this->isValueEmpty($value, $this->options['empty_zero'], FALSE)) { return ''; diff --git a/core/modules/views/src/Tests/Handler/FieldUnitTest.php b/core/modules/views/src/Tests/Handler/FieldUnitTest.php index 85fabe3..a42110b 100644 --- a/core/modules/views/src/Tests/Handler/FieldUnitTest.php +++ b/core/modules/views/src/Tests/Handler/FieldUnitTest.php @@ -349,7 +349,7 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'By default, a string should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'By default, a string should not be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; @@ -363,14 +363,14 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, '0', 'By default, 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, '0', 'By default, 0 should not be treated as empty.'); // Test zero as a string. $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'By default, "0" should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'By default, "0" should not be treated as empty.'); // Test when results are not rewritten and non-zero empty values are hidden. $view->field['name']->options['hide_alter_empty'] = TRUE; @@ -382,7 +382,7 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'If hide_empty is checked, a string should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'If hide_empty is checked, a string should not be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; @@ -396,14 +396,14 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, '0', 'If hide_empty is checked, but not empty_zero, 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, '0', 'If hide_empty is checked, but not empty_zero, 0 should not be treated as empty.'); // Test zero as a string. $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If hide_empty is checked, but not empty_zero, "0" should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'If hide_empty is checked, but not empty_zero, "0" should not be treated as empty.'); // Test when results are not rewritten and all empty values are hidden. $view->field['name']->options['hide_alter_empty'] = TRUE; @@ -437,28 +437,28 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, it should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'If the rewritten string is not empty, it should not be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "" should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'If the rewritten string is not empty, "" should not be treated as empty.'); // Test zero as an integer. $view->result[0]->{$column_map_reversed['name']} = 0; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'If the rewritten string is not empty, 0 should not be treated as empty.'); // Test zero as a string. $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'If the rewritten string is not empty, "0" should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'If the rewritten string is not empty, "0" should not be treated as empty.'); // Test when results are rewritten to an empty string and non-zero empty results are hidden. $view->field['name']->options['hide_alter_empty'] = TRUE; @@ -472,7 +472,7 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_name, 'If the rewritten string is empty, it should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_name, 'If the rewritten string is empty, it should not be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; @@ -486,14 +486,14 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, '0', 'If the rewritten string is empty, 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, '0', 'If the rewritten string is empty, 0 should not be treated as empty.'); // Test zero as a string. $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If the rewritten string is empty, "0" should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'If the rewritten string is empty, "0" should not be treated as empty.'); // Test when results are rewritten to zero as a string and non-zero empty // results are hidden. @@ -508,28 +508,28 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, the string rewritten as 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'If the rewritten string is zero and empty_zero is not checked, the string rewritten as 0 should not be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, "" rewritten as 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'If the rewritten string is zero and empty_zero is not checked, "" rewritten as 0 should not be treated as empty.'); // Test zero as an integer. $view->result[0]->{$column_map_reversed['name']} = 0; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'If the rewritten string is zero and empty_zero is not checked, 0 should not be treated as empty.'); // Test zero as a string. $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If the rewritten string is zero and empty_zero is not checked, "0" should not be treated as empty.'); + $this->assertIdentical((string) $render, "0", 'If the rewritten string is zero and empty_zero is not checked, "0" should not be treated as empty.'); // Test when results are rewritten to a valid string and non-zero empty // results are hidden. @@ -544,7 +544,7 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, it should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_value, 'If the original and rewritten strings are valid, it should not be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; @@ -558,14 +558,14 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, 0 should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_value, 'If the original and rewritten strings are valid, 0 should not be treated as empty.'); // Test zero as a string. $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $random_value, 'If the original and rewritten strings are valid, "0" should not be treated as empty.'); + $this->assertIdentical((string) $render, $random_value, 'If the original and rewritten strings are valid, "0" should not be treated as empty.'); // Test when results are rewritten to zero as a string and all empty // original values and results are hidden. @@ -580,7 +580,7 @@ function _testHideIfEmpty() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "", 'If the rewritten string is zero, it should be treated as empty.'); + $this->assertIdentical((string) $render, "", 'If the rewritten string is zero, it should be treated as empty.'); // Test an empty string. $view->result[0]->{$column_map_reversed['name']} = ""; @@ -623,20 +623,20 @@ function _testEmptyText() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $empty_text, 'If a field is empty, the empty text should be used for the output.'); + $this->assertIdentical((string) $render, $empty_text, 'If a field is empty, the empty text should be used for the output.'); $view->result[0]->{$column_map_reversed['name']} = "0"; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, "0", 'If a field is 0 and empty_zero is not checked, the empty text should not be used for the output.'); + $this->assertIdentical((string) $render, "0", 'If a field is 0 and empty_zero is not checked, the empty text should not be used for the output.'); $view->result[0]->{$column_map_reversed['name']} = "0"; $view->field['name']->options['empty_zero'] = TRUE; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.'); + $this->assertIdentical((string) $render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.'); $view->result[0]->{$column_map_reversed['name']} = ""; $view->field['name']->options['alter']['alter_text'] = TRUE; @@ -645,13 +645,13 @@ function _testEmptyText() { $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $alter_text, 'If a field is empty, some rewrite text exists, but hide_alter_empty is not checked, render the rewrite text.'); + $this->assertIdentical((string) $render, $alter_text, 'If a field is empty, some rewrite text exists, but hide_alter_empty is not checked, render the rewrite text.'); $view->field['name']->options['hide_alter_empty'] = TRUE; $render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { return $view->field['name']->advancedRender($view->result[0]); }); - $this->assertIdentical($render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.'); + $this->assertIdentical((string) $render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.'); } /**