diff --git a/src/Plugin/facets/widget/DropdownWidget.php b/src/Plugin/facets/widget/DropdownWidget.php index 18932aa..c03e0f8 100644 --- a/src/Plugin/facets/widget/DropdownWidget.php +++ b/src/Plugin/facets/widget/DropdownWidget.php @@ -34,17 +34,15 @@ class DropdownWidget extends WidgetPluginBase { public function build(FacetInterface $facet) { $results = $facet->getResults(); $options = $this->buildOptions($facet, $results); - $widget = $facet->getWidget(); - $defaultValue = ''; - $emptyOptionValue = $defaultValue; - $activeResult = $this->getActiveResult($results); - if ($activeResult) { - $emptyOptionValue = $activeResult->getUrl()->toString(); + $default_value = ''; + $active_result = $this->getActiveResult($results); + if ($active_result) { + $default_value = $active_result->getUrl()->toString(); } - $emptyOption = [ - $emptyOptionValue => $this->getConfiguration()['default_option_label'], + $empty_option = [ + '' => $this->getConfiguration()['default_option_label'], ]; $item = [ @@ -52,14 +50,13 @@ class DropdownWidget extends WidgetPluginBase { '#id' => $facet->id(), '#name' => $facet->id(), '#facet' => $facet, - '#options' => array_merge($emptyOption, $options), - '#value' => $defaultValue, + '#options' => array_merge($empty_option, $options), + '#value' => $default_value, '#attributes' => [ 'data-drupal-facet-id' => $facet->id(), 'data-drupal-facet-alias' => $facet->getUrlAlias(), - 'class' => ['js-facets-dropdown-links'], + 'class' => [$active_result ? 'facet-active' : 'facet-inactive', 'js-facets-dropdown-links'], ], - '#context' => ['list_style' => $widget['type']], '#attached' => [ 'library' => [ 'facets/drupal.facets.dropdown-widget', @@ -163,11 +160,7 @@ class DropdownWidget extends WidgetPluginBase { */ protected function appendOptions(array &$options, FacetInterface $facet, array $results) { foreach ($results as $result) { - $urlString = ''; - if (!$result->isActive()) { - $urlString = $result->getUrl() - ->toString(); - } + $urlString = $result->getUrl()->toString(); $options[$urlString] = $this->buildResultOptionText($result); $children = $result->getChildren(); diff --git a/tests/src/Unit/Plugin/widget/DropdownWidgetTest.php b/tests/src/Unit/Plugin/widget/DropdownWidgetTest.php index 2e61d41..1b81501 100644 --- a/tests/src/Unit/Plugin/widget/DropdownWidgetTest.php +++ b/tests/src/Unit/Plugin/widget/DropdownWidgetTest.php @@ -30,22 +30,28 @@ class DropdownWidgetTest extends WidgetTestBase { $output = $this->widget->build($facet); $this->assertSame('array', gettype($output)); - $this->assertCount(4, $output['#items']); + // The dropdown contains the 4 results, as well as the empty option, so 5 in + // total. + $this->assertCount(5, $output['#options']); $this->assertEquals(['facet-inactive', 'js-facets-dropdown-links'], $output['#attributes']['class']); + // Skip the first option, it is the empty option. + $first_option = array_shift($output['#options']); + $expected_links = [ $this->buildLinkAssertion('Llama', 'llama', $facet, 10), $this->buildLinkAssertion('Badger', 'badger', $facet, 20), $this->buildLinkAssertion('Duck', 'duck', $facet, 15), $this->buildLinkAssertion('Alpaca', 'alpaca', $facet, 9), ]; - foreach ($expected_links as $index => $value) { - $this->assertSame('array', gettype($output['#items'][$index])); - $this->assertEquals($value, $output['#items'][$index]['#title']); - $this->assertSame('array', gettype($output['#items'][$index]['#title'])); - $this->assertEquals('link', $output['#items'][$index]['#type']); - $this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']); + foreach ($expected_links as $value) { + $option = array_shift($output['#options']); + $this->assertSame('array', gettype($option)); + $this->assertEquals($value, $option['#title']); + $this->assertSame('array', gettype($option['#title'])); + $this->assertEquals('link', $option['#type']); + $this->assertEquals(['facet-item'], $option['#wrapper_attributes']['class']); } } diff --git a/tests/src/Unit/Plugin/widget/WidgetTestBase.php b/tests/src/Unit/Plugin/widget/WidgetTestBase.php index 76975b3..96782c9 100644 --- a/tests/src/Unit/Plugin/widget/WidgetTestBase.php +++ b/tests/src/Unit/Plugin/widget/WidgetTestBase.php @@ -61,14 +61,18 @@ abstract class WidgetTestBase extends UnitTestCase { new Result($facet, 'alpaca', 'Alpaca', 9), ]; + // Create a container, so we can access string translation. + $string_translation = $this->prophesize(TranslationInterface::class); + $url_generator = $this->prophesize(UrlGeneratorInterface::class); + foreach ($original_results as $original_result) { - $original_result->setUrl(new Url('test')); + $raw_value = $original_result->getRawValue(); + $original_result->setUrl(new Url($raw_value)); + $result_url = "http://example.com/$raw_value"; + $url_generator->generateFromRoute($raw_value, [], [], FALSE)->willReturn($result_url); } $this->originalResults = $original_results; - // Create a container, so we can access string translation. - $string_translation = $this->prophesize(TranslationInterface::class); - $url_generator = $this->prophesize(UrlGeneratorInterface::class); $widget_manager = $this->prophesize(WidgetPluginManager::class); $container = new ContainerBuilder();