diff --git a/src/Plugin/facets/widget/LinksWidget.php b/src/Plugin/facets/widget/LinksWidget.php index 71232e9..a2ef401 100644 --- a/src/Plugin/facets/widget/LinksWidget.php +++ b/src/Plugin/facets/widget/LinksWidget.php @@ -44,7 +44,7 @@ class LinksWidget implements WidgetInterface { } if (is_null($result->getUrl())) { - $items[] = $text; + $items[] = ['#markup' => $text]; } else { $items[] = $this->buildListItems($result, $show_numbers); @@ -72,38 +72,33 @@ class LinksWidget implements WidgetInterface { * @param bool $show_numbers * A boolean that's true when the numbers should be shown. * - * @return array|Link|string - * A renderable array of the result or a link when the result has no - * children. + * @return array + * A renderable array of the result. */ protected function buildListItems(ResultInterface $result, $show_numbers) { + + $classes = ['facet_item']; + if ($result->isActive()) { + $classes[] = 'facet_item--active'; + } + if ($children = $result->getChildren()) { - $link = $this->prepareLink($result, $show_numbers); + $items = $this->prepareLink($result, $show_numbers); $children_markup = []; foreach ($children as $child) { $children_markup[] = $this->buildChildren($child, $show_numbers); } - if ($link instanceof Link) { - $items = $link->toRenderable(); - $items['#wrapper_attibutes'] = ['class' => ['expanded']]; - $items['children'] = [$children_markup]; - } - else { - $items = [ - '#markup' => $link, - '#wrapper_attributes' => [ - 'class' => ['expanded'], - ], - 'children' => [$children_markup], - ]; - } + $classes[] = 'expanded'; + $items['children'] = [$children_markup]; } else { $items = $this->prepareLink($result, $show_numbers); } + $items['#wrapper_attributes'] = ['class' => $classes]; + return $items; } @@ -115,8 +110,8 @@ class LinksWidget implements WidgetInterface { * @param bool $show_numbers * A boolean that's true when the numbers should be shown. * - * @return Link|string - * The item, can be a link or just the text. + * @return array + * The item, as a renderable array. */ protected function prepareLink(ResultInterface $result, $show_numbers) { $text = $result->getDisplayValue(); @@ -129,10 +124,11 @@ class LinksWidget implements WidgetInterface { } if (is_null($result->getUrl())) { - $link = $text; + $link = ['#markup' => $text]; } else { $link = new Link($text, $result->getUrl()); + $link = $link->toRenderable(); } return $link; @@ -146,7 +142,7 @@ class LinksWidget implements WidgetInterface { * @param bool $show_numbers * A boolean that's true when the numbers should be shown. * - * @return array|Link|string + * @return array * A renderable array of the result. */ protected function buildChildren(ResultInterface $child, $show_numbers) { @@ -160,19 +156,15 @@ class LinksWidget implements WidgetInterface { if (!is_null($child->getUrl())) { $link = new Link($text, $child->getUrl()); - $link = $link->toRenderable(); - $link['#wrapper_attributes'] = ['class' => ['leaf']]; + $item = $link->toRenderable(); } else { - $link = [ - '#markup' => $text, - '#wrapper_attributes' => [ - 'class' => ['leaf'], - ], - ]; + $item = ['#markup' => $text]; } - return $link; + $item['#wrapper_attributes'] = ['class' => ['leaf']]; + + return $item; } /** diff --git a/tests/src/Unit/Plugin/widget/LinksWidgetTest.php b/tests/src/Unit/Plugin/widget/LinksWidgetTest.php index f7491f4..0ca4542 100644 --- a/tests/src/Unit/Plugin/widget/LinksWidgetTest.php +++ b/tests/src/Unit/Plugin/widget/LinksWidgetTest.php @@ -66,8 +66,10 @@ class LinksWidgetTest extends UnitTestCase { $expected_links = ['Llama (10)', 'Badger (20)', 'Duck (15)', 'Alpaca (9)']; foreach ($expected_links as $index => $value) { - $this->assertInstanceOf('\Drupal\Core\Link', $output['#items'][$index]); - $this->assertEquals($value, $output['#items'][$index]->getText()); + $this->assertInternalType('array', $output['#items'][$index]); + $this->assertEquals($value, $output['#items'][$index]['#title']); + $this->assertEquals('link', $output['#items'][$index]['#type']); + $this->assertEquals(['facet_item'], $output['#items'][$index]['#wrapper_attributes']['class']); } } @@ -95,8 +97,15 @@ class LinksWidgetTest extends UnitTestCase { '(-) Alpaca (9)', ]; foreach ($expected_links as $index => $value) { - $this->assertInstanceOf('\Drupal\Core\Link', $output['#items'][$index]); - $this->assertEquals($value, $output['#items'][$index]->getText()); + $this->assertInternalType('array', $output['#items'][$index]); + $this->assertEquals($value, $output['#items'][$index]['#title']); + $this->assertEquals('link', $output['#items'][$index]['#type']); + if ($index == 0 || $index == 3) { + $this->assertEquals(['facet_item', 'facet_item--active'], $output['#items'][$index]['#wrapper_attributes']['class']); + } + else { + $this->assertEquals(['facet_item'], $output['#items'][$index]['#wrapper_attributes']['class']); + } } } @@ -118,8 +127,15 @@ class LinksWidgetTest extends UnitTestCase { $expected_links = ['Llama', '(-) Badger', 'Duck', 'Alpaca']; foreach ($expected_links as $index => $value) { - $this->assertInstanceOf('\Drupal\Core\Link', $output['#items'][$index]); - $this->assertEquals($value, $output['#items'][$index]->getText()); + $this->assertInternalType('array', $output['#items'][$index]); + $this->assertEquals($value, $output['#items'][$index]['#title']); + $this->assertEquals('link', $output['#items'][$index]['#type']); + if ($index === 1) { + $this->assertEquals(['facet_item', 'facet_item--active'], $output['#items'][$index]['#wrapper_attributes']['class']); + } + else { + $this->assertEquals(['facet_item'], $output['#items'][$index]['#wrapper_attributes']['class']); + } } // Enable the 'show_numbers' setting again to make sure that the switch @@ -138,8 +154,15 @@ class LinksWidgetTest extends UnitTestCase { 'Alpaca (9)', ]; foreach ($expected_links as $index => $value) { - $this->assertInstanceOf('\Drupal\Core\Link', $output['#items'][$index]); - $this->assertEquals($value, $output['#items'][$index]->getText()); + $this->assertInternalType('array', $output['#items'][$index]); + $this->assertEquals($value, $output['#items'][$index]['#title']); + $this->assertEquals('link', $output['#items'][$index]['#type']); + if ($index === 1) { + $this->assertEquals(['facet_item', 'facet_item--active'], $output['#items'][$index]['#wrapper_attributes']['class']); + } + else { + $this->assertEquals(['facet_item'], $output['#items'][$index]['#wrapper_attributes']['class']); + } } }