Skip to content

Commit 0c69b4b

Browse files
committed
Issue #2693727 by mikelutz, sanduhrs, CalebD, ajlib, Lendude, tstoeckler, catch: Limiting options for exposed Language filters causes errors and doesn't work for special languages
(cherry picked from commit 0b6c398)
1 parent 2b2b206 commit 0c69b4b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

modules/views/src/Plugin/views/filter/InOperator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\views\Plugin\views\filter;
44

5+
use Drupal\Component\Render\MarkupInterface;
56
use Drupal\Component\Utility\Unicode;
67
use Drupal\Core\Form\FormStateInterface;
78
use Drupal\views\Plugin\views\display\DisplayPluginBase;
@@ -263,15 +264,15 @@ public function reduceValueOptions($input = NULL) {
263264
}
264265

265266
// Because options may be an array of strings, or an array of mixed arrays
266-
// and strings (optgroups) or an array of objects, we have to
267-
// step through and handle each one individually.
267+
// and strings (optgroups), or an array of objects, or a form of Markup, we
268+
// have to step through and handle each one individually.
268269
$options = [];
269270
foreach ($input as $id => $option) {
270271
if (is_array($option)) {
271272
$options[$id] = $this->reduceValueOptions($option);
272273
continue;
273274
}
274-
elseif (is_object($option)) {
275+
elseif (is_object($option) && !$option instanceof MarkupInterface) {
275276
$keys = array_keys($option->option);
276277
$key = array_shift($keys);
277278
if (isset($this->options['value'][$key])) {

modules/views/tests/src/Kernel/Handler/FilterInOperatorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Drupal\Tests\views\Kernel\Handler;
44

5+
use Drupal\Core\StringTranslation\StringTranslationTrait;
6+
use Drupal\Core\StringTranslation\TranslatableMarkup;
57
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
8+
use Drupal\views\Plugin\views\display\DisplayPluginBase;
9+
use Drupal\views\ViewExecutable;
610
use Drupal\views\Views;
711

812
/**
@@ -11,6 +15,7 @@
1115
* @group views
1216
*/
1317
class FilterInOperatorTest extends ViewsKernelTestBase {
18+
use StringTranslationTrait;
1419

1520
public static $modules = ['system'];
1621

@@ -195,4 +200,34 @@ protected function getGroupedExposedFilters() {
195200
return $filters;
196201
}
197202

203+
/**
204+
* Tests that the InOperator filter can handle TranslateableMarkup.
205+
*/
206+
public function testFilterOptionAsMarkup() {
207+
$view = $this->prophesize(ViewExecutable::class);
208+
$display = $this->prophesize(DisplayPluginBase::class);
209+
$display->getOption('relationships')->willReturn(FALSE);
210+
$view->display_handler = $display->reveal();
211+
212+
/** @var \Drupal\views\Plugin\ViewsHandlerManager $manager */
213+
$manager = $this->container->get('plugin.manager.views.filter');
214+
/** @var \Drupal\views\Plugin\views\filter\InOperator $operator */
215+
$operator = $manager->createInstance('in_operator');
216+
$options = ['value' => ['foo' => [], 'baz' => []]];
217+
$operator->init($view->reveal(), $display->reveal(), $options);
218+
219+
$input_options = [
220+
'foo' => 'bar',
221+
'baz' => $this->t('qux'),
222+
'quux' => (object) ['option' => ['quux' => 'corge']],
223+
];
224+
$reduced_values = $operator->reduceValueOptions($input_options);
225+
226+
$this->assertSame(['foo', 'baz'], array_keys($reduced_values));
227+
$this->assertInstanceOf(TranslatableMarkup::class, $reduced_values['baz']);
228+
$this->assertSame('qux', (string) $reduced_values['baz']);
229+
$this->assertSame('bar', $reduced_values['foo']);
230+
231+
}
232+
198233
}

0 commit comments

Comments
 (0)