diff --git a/core/modules/views/src/Plugin/views/filter/InOperator.php b/core/modules/views/src/Plugin/views/filter/InOperator.php index e0add0fcb1..8a884898f5 100644 --- a/core/modules/views/src/Plugin/views/filter/InOperator.php +++ b/core/modules/views/src/Plugin/views/filter/InOperator.php @@ -2,6 +2,7 @@ namespace Drupal\views\Plugin\views\filter; +use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Unicode; use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -263,15 +264,15 @@ public function reduceValueOptions($input = NULL) { } // Because options may be an array of strings, or an array of mixed arrays - // and strings (optgroups) or an array of objects, we have to - // step through and handle each one individually. + // and strings (optgroups), or an array of objects, or a form of Markup, we + // have to step through and handle each one individually. $options = []; foreach ($input as $id => $option) { if (is_array($option)) { $options[$id] = $this->reduceValueOptions($option); continue; } - elseif (is_object($option)) { + elseif (is_object($option) && !$option instanceof MarkupInterface) { $keys = array_keys($option->option); $key = array_shift($keys); if (isset($this->options['value'][$key])) { diff --git a/core/modules/views/tests/src/Kernel/Handler/FilterInOperatorTest.php b/core/modules/views/tests/src/Kernel/Handler/FilterInOperatorTest.php index e02a8430c7..b7b76cef97 100644 --- a/core/modules/views/tests/src/Kernel/Handler/FilterInOperatorTest.php +++ b/core/modules/views/tests/src/Kernel/Handler/FilterInOperatorTest.php @@ -2,7 +2,11 @@ namespace Drupal\Tests\views\Kernel\Handler; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; +use Drupal\views\Plugin\views\display\DisplayPluginBase; +use Drupal\views\ViewExecutable; use Drupal\views\Views; /** @@ -11,6 +15,7 @@ * @group views */ class FilterInOperatorTest extends ViewsKernelTestBase { + use StringTranslationTrait; public static $modules = ['system']; @@ -195,4 +200,34 @@ protected function getGroupedExposedFilters() { return $filters; } + /** + * Tests that the InOperator filter can handle TranslateableMarkup. + */ + public function testFilterOptionAsMarkup() { + $view = $this->prophesize(ViewExecutable::class); + $display = $this->prophesize(DisplayPluginBase::class); + $display->getOption('relationships')->willReturn(FALSE); + $view->display_handler = $display->reveal(); + + /** @var \Drupal\views\Plugin\ViewsHandlerManager $manager */ + $manager = $this->container->get('plugin.manager.views.filter'); + /** @var \Drupal\views\Plugin\views\filter\InOperator $operator */ + $operator = $manager->createInstance('in_operator'); + $options = ['value' => ['foo' => [], 'baz' => []]]; + $operator->init($view->reveal(), $display->reveal(), $options); + + $input_options = [ + 'foo' => 'bar', + 'baz' => $this->t('qux'), + 'quux' => (object) ['option' => ['quux' => 'corge']], + ]; + $reduced_values = $operator->reduceValueOptions($input_options); + + $this->assertSame(['foo', 'baz'], array_keys($reduced_values)); + $this->assertInstanceOf(TranslatableMarkup::class, $reduced_values['baz']); + $this->assertSame('qux', (string) $reduced_values['baz']); + $this->assertSame('bar', $reduced_values['foo']); + + } + }