Commit a4a836e9 authored by claudiu.cristea's avatar claudiu.cristea Committed by borisson_

Issue #2760769 by claudiu.cristea: Allow theming of result item links

parent b006b77f
......@@ -66,7 +66,7 @@ class IntegrationTest extends WebTestBase {
// Verify that the label is correct for a clicked link.
$this->clickLinkPartialName('page');
$this->assertRaw('<span class="facet-deactivate">(-)</span> page <span class="facet-count">(19)</span>');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> page <span class="facet-count">(19)</span>');
// To make sure we have an empty result, we truncate the search_index table
// because, for the moment, we don't have the possibility to clear the index
......
......@@ -29,6 +29,19 @@ function facets_help($route_name, RouteMatchInterface $route_match) {
}
}
/**
* Implements hook_theme().
*/
function facets_theme($existing, $type, $theme, $path) {
$result_item_variables = [
'variables' => ['value' => '', 'show_count' => FALSE, 'count' => NULL],
];
return [
'facets_result_item' => $result_item_variables,
'facets_result_item_active' => $result_item_variables,
];
}
/**
* Implementation of hook_search_api_query_alter.
*
......
......@@ -43,7 +43,7 @@
if (active) {
checkbox.attr('checked', true);
label.find('.facet-deactivate').remove();
label.find('.js-facet-deactivate').remove();
}
$link.before(checkbox).before(label).hide();
......
......@@ -41,7 +41,7 @@
$default_option.attr('value', $link.attr('href'));
$option.attr('selected', 'selected');
$link.find('.facet-deactivate').remove();
$link.find('.js-facet-deactivate').remove();
}
$option.html($link.html());
$dropdown.append($option);
......
......@@ -22,9 +22,9 @@ class Result implements ResultInterface {
/**
* The facet count.
*
* @var int|FALSE
* @var int|null
*/
protected $count = FALSE;
protected $count = NULL;
/**
* The Url object.
......@@ -50,8 +50,8 @@ class Result implements ResultInterface {
* The raw value.
* @param mixed $display_value
* The formatted value.
* @param int $count
* The amount of items.
* @param int|null $count
* The amount of items or NULL.
*/
public function __construct($raw_value, $display_value, $count) {
$this->rawValue = $raw_value;
......
......@@ -29,8 +29,8 @@ interface ResultInterface {
/**
* Returns the count for the result.
*
* @return mixed
* The amount of items for the result.
* @return int|null
* The amount of items for the result or NULL.
*/
public function getCount();
......
......@@ -341,7 +341,7 @@ class IntegrationTest extends WebTestBase {
$this->assertLink('article');
$this->clickLink('item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> item');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> item');
$this->assertNoLink('article');
$this->drupalGet($facet_edit_page);
......@@ -351,7 +351,7 @@ class IntegrationTest extends WebTestBase {
$this->assertLink('article');
$this->clickLink('item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> item');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> item');
$this->assertLink('article');
// Verify the number of results for OR functionality.
......@@ -444,7 +444,7 @@ class IntegrationTest extends WebTestBase {
$this->assertLink('item');
$this->clickLink('item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> item');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> item');
$this->assertText('foo baz');
$this->assertText('bar baz');
$this->assertNoText('foo bar baz');
......@@ -460,7 +460,7 @@ class IntegrationTest extends WebTestBase {
$this->assertLink('item');
$this->clickLink('item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> item');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> item');
$this->assertText('foo bar baz');
$this->assertText('foo test');
$this->assertText('bar');
......@@ -488,13 +488,13 @@ class IntegrationTest extends WebTestBase {
$this->clickLink('grape');
$this->assertText('Displaying 3 search results');
$this->assertRaw('<span class="facet-deactivate">(-)</span> grape');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> grape');
$this->assertLink('orange');
$this->clickLink('orange');
$this->assertText('Displaying 3 search results');
$this->assertLink('grape');
$this->assertRaw('<span class="facet-deactivate">(-)</span> orange');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> orange');
}
/**
......
......@@ -373,7 +373,7 @@ class ProcessorIntegrationTest extends WebTestBase {
$this->drupalGet('search-api-test-fulltext');
$this->clickLink('strawberry');
$this->assertStringPosition('<span class="facet-deactivate">(-)</span> strawberry', 'grape');
$this->assertStringPosition('<span class="js-facet-deactivate">(-)</span> strawberry', 'grape');
$form = [
'facet_sorting[active_widget_order][status]' => TRUE,
......@@ -383,7 +383,7 @@ class ProcessorIntegrationTest extends WebTestBase {
$this->drupalGet('search-api-test-fulltext');
$this->clickLink('strawberry');
$this->assertStringPosition('grape', '<span class="facet-deactivate">(-)</span> strawberry');
$this->assertStringPosition('grape', '<span class="js-facet-deactivate">(-)</span> strawberry');
$form = [
'facet_sorting[active_widget_order][status]' => FALSE,
......
......@@ -141,7 +141,7 @@ class UrlIntegrationTest extends WebTestBase {
// Make sure 'test:colon' is active.
$url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'water_bear:test:colon']]);
$this->assertUrl($url);
$this->assertRaw('<span class="facet-deactivate">(-)</span> test:colon');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> test:colon');
$this->assertLink('orange');
$this->assertLink('banana');
}
......@@ -161,7 +161,7 @@ class UrlIntegrationTest extends WebTestBase {
$this->clickLink('item');
$this->assertResponse(200);
$this->assertRaw('<span class="facet-deactivate">(-)</span> item');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> item');
$this->assertLink('article');
$this->assertUrl($url);
}
......
......@@ -65,7 +65,7 @@ class WidgetIntegrationTest extends WebTestBase {
$this->assertLink('article');
$this->clickLink('item');
$this->assertRaw('<span class="facet-deactivate">(-)</span> item');
$this->assertRaw('<span class="js-facet-deactivate">(-)</span> item');
}
/**
......
......@@ -52,7 +52,7 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
$items = array_map(function (Result $result) {
if (empty($result->getUrl())) {
return ['#markup' => $this->extractText($result)];
return $this->buildResultItem($result);
}
else {
return $this->buildListItems($result);
......@@ -138,7 +138,7 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
$children_markup = [];
foreach ($children as $child) {
$children_markup[] = $this->buildChildren($child);
$children_markup[] = $this->buildChild($child);
}
$classes[] = 'expanded';
......@@ -169,63 +169,50 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
* A result item.
*
* @return array
* The item, as a renderable array.
* The item as a render array.
*/
protected function prepareLink(ResultInterface $result) {
$text = $this->extractText($result);
if (is_null($result->getUrl())) {
$link = ['#markup' => $text];
}
else {
$link = (new Link($text, $result->getUrl()))->toRenderable();
$item = $this->buildResultItem($result);
if (!is_null($result->getUrl())) {
$item = (new Link($item, $result->getUrl()))->toRenderable();
}
return $link;
return $item;
}
/**
* Builds a renderable array of a result.
* Builds a result item as a render array.
*
* @param \Drupal\facets\Result\ResultInterface $child
* A result item.
*
* @return array
* A renderable array of the result.
* The result item as render array.
*/
protected function buildChildren(ResultInterface $child) {
$text = $this->extractText($child);
if (!is_null($child->getUrl())) {
$link = new Link($text, $child->getUrl());
$item = $link->toRenderable();
}
else {
$item = ['#markup' => $text];
}
protected function buildChild(ResultInterface $child) {
$item = $this->prepareLink($child);
$item['#wrapper_attributes'] = ['class' => ['leaf']];
return $item;
}
/**
* Extracts the text for a result to display in the UI.
* Builds a facet result item.
*
* @param \Drupal\facets\Result\ResultInterface $result
* The result to extract the text for.
* The result item.
*
* @return string
* The text to display.
* @return array
* The facet result item as a render array.
*/
protected function extractText(ResultInterface $result) {
$template = '@text';
$arguments = ['@text' => $result->getDisplayValue()];
if ($this->getConfiguration()['show_numbers'] && $result->getCount() !== FALSE) {
$template .= ' <span class="facet-count">(@count)</span>';
$arguments += ['@count' => $result->getCount()];
}
if ($result->isActive()) {
$template = '<span class="facet-deactivate">(-)</span> ' . $template;
}
return new FormattableMarkup($template, $arguments);
protected function buildResultItem(ResultInterface $result) {
$count = $result->getCount();
return [
'#theme' => $result->isActive() ? 'facets_result_item_active' : 'facets_result_item',
'#value' => $result->getDisplayValue(),
'#show_count' => $this->getConfiguration()['show_numbers'] && ($count !== NULL),
'#count' => $count,
];
}
}
{#
/**
* @file
* Default theme implementation of an active facet result item.
*
* Available variables:
* - value: The item value.
* - show_count: If this facet provides count.
* - count: The amount of results.
*
* @ingroup themeable
*/
#}
<span class="js-facet-deactivate">(-)</span> {{ value }} {% if show_count %}<span class="facet-count">({{ count }})</span>{% endif %}
{#
/**
* @file
* Default theme implementation of a facet result item.
*
* Available variables:
* - value: The item value.
* - show_count: If this facet provides count.
* - count: The amount of results.
*
* @ingroup themeable
*/
#}
{{ value }} {% if show_count %}<span class="facet-count">({{ count }})</span>{% endif %}
......@@ -75,7 +75,7 @@ class CheckboxWidgetTest extends UnitTestCase {
foreach ($expected_links as $index => $value) {
$this->assertInternalType('array', $output['#items'][$index]);
$this->assertEquals($value, $output['#items'][$index]['#title']);
$this->assertInstanceOf(FormattableMarkup::class, $output['#items'][$index]['#title']);
$this->assertInternalType('array', $output['#items'][$index]['#title']);
$this->assertEquals('link', $output['#items'][$index]['#type']);
$this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']);
}
......@@ -101,18 +101,16 @@ class CheckboxWidgetTest extends UnitTestCase {
* @param bool $show_numbers
* Numbers are displayed.
*
* @return \Drupal\Component\Render\FormattableMarkup
* Formattable markup object for link.
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
$text = new FormattableMarkup('@text', ['@text' => $text, '@count' => $count]);
if ($show_numbers !== FALSE) {
$text->string .= ' <span class="facet-count">(@count)</span>';
}
if ($active) {
$text->string = '<span class="facet-deactivate">(-)</span> ' . $text->string;
}
return $text;
return [
'#theme' => $active ? 'facets_result_item_active' : 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,
];
}
}
......@@ -83,7 +83,7 @@ class DropdownWidgetTest extends UnitTestCase {
foreach ($expected_links as $index => $value) {
$this->assertInternalType('array', $output['#items'][$index]);
$this->assertEquals($value, $output['#items'][$index]['#title']);
$this->assertInstanceOf(FormattableMarkup::class, $output['#items'][$index]['#title']);
$this->assertInternalType('array', $output['#items'][$index]['#title']);
$this->assertEquals('link', $output['#items'][$index]['#type']);
$this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']);
}
......@@ -101,18 +101,16 @@ class DropdownWidgetTest extends UnitTestCase {
* @param bool $show_numbers
* Numbers are displayed.
*
* @return \Drupal\Component\Render\FormattableMarkup
* Formattable markup object for link.
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
$text = new FormattableMarkup('@text', ['@text' => $text, '@count' => $count]);
if ($show_numbers !== FALSE) {
$text->string .= ' <span class="facet-count">(@count)</span>';
}
if ($active) {
$text->string = '<span class="facet-deactivate">(-)</span> ' . $text->string;
}
return $text;
return [
'#theme' => $active ? 'facets_result_item_active' : 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,
];
}
}
......@@ -74,7 +74,7 @@ class LinksWidgetTest extends UnitTestCase {
foreach ($expected_links as $index => $value) {
$this->assertInternalType('array', $output['#items'][$index]);
$this->assertEquals($value, $output['#items'][$index]['#title']);
$this->assertInstanceOf(FormattableMarkup::class, $output['#items'][$index]['#title']);
$this->assertInternalType('array', $output['#items'][$index]['#title']);
$this->assertEquals('link', $output['#items'][$index]['#type']);
$this->assertEquals(['facet-item'], $output['#items'][$index]['#wrapper_attributes']['class']);
}
......@@ -232,20 +232,16 @@ class LinksWidgetTest extends UnitTestCase {
* @param bool $show_numbers
* Numbers are displayed.
*
* @return \Drupal\Component\Render\FormattableMarkup
* Formattable markup object for link.
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
$template = '@text';
$arguments = ['@text' => $text];
if ($show_numbers) {
$template .= ' <span class="facet-count">(@count)</span>';
$arguments += ['@count' => $count];
}
if ($active) {
$template = '<span class="facet-deactivate">(-)</span> ' . $template;
}
return new FormattableMarkup($template, $arguments);
return [
'#theme' => $active ? 'facets_result_item_active' : 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment