Commit 27bbb504 authored by StryKaizer's avatar StryKaizer Committed by borisson_

Issue #2926335 by borisson_, sylus, tibezh, StryKaizer: Empty facet summary...

Issue #2926335 by borisson_, sylus, tibezh, StryKaizer: Empty facet summary precedence over 'show_text_when_empty'
parent 3c35919d
......@@ -28,18 +28,25 @@ class ShowTextWhenEmptyProcessor extends ProcessorPluginBase implements BuildPro
public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
$config = $this->getConfiguration();
if (isset($build['#items'])) {
return $build;
$results_count = array_sum(array_map(function ($it) {
/** @var \Drupal\facets\FacetInterface $it */
return count($it->getResults());
}, $facets));
// No items are found, so we should return the empty summary.
if (!isset($build['#items']) || $results_count === 0) {
return [
'#theme' => 'facets_summary_empty',
'#message' => [
'#type' => 'processed_text',
'#text' => $config['text']['value'],
'#format' => $config['text']['format'],
],
];
}
return [
'#theme' => 'facets_summary_empty',
'#message' => [
'#type' => 'processed_text',
'#text' => $config['text']['value'],
'#format' => $config['text']['format'],
],
];
// Return the actual items.
return $build;
}
/**
......@@ -67,7 +74,7 @@ class ShowTextWhenEmptyProcessor extends ProcessorPluginBase implements BuildPro
return [
'text' => [
'format' => 'plain_text',
'value' => $this->t('There is no current search in progress.'),
'value' => $this->t('No results found.'),
],
];
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\facets_summary\Unit\Plugin\Processor;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\facets_summary\Plugin\facets_summary\processor\ShowTextWhenEmptyProcessor;
......@@ -86,13 +87,37 @@ class ShowTextWhenEmptyProcessorTest extends UnitTestCase {
*
* @covers ::build
*/
public function testBuildWithItems() {
public function testBuildWithEmptyItems() {
$summary = new FacetsSummary([], 'facets_summary');
$summary->setFacetSourceId('foo');
$build = ['#items' => []];
$result = $this->processor->build($summary, $build, []);
$this->assertEquals($build, $result);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('#theme', $result);
$this->assertEquals('facets_summary_empty', $result['#theme']);
$this->assertArrayHasKey('#message', $result);
$this->assertArrayHasKey('#text', $result['#message']);
$this->assertEquals(new TranslatableMarkup('No results found.'), (string) $result['#message']['#text']);
$this->assertEquals('plain_text', $result['#message']['#format']);
}
/**
* Tests build with config changes.
*
* @covers ::build
*/
public function testBuildWithConfigChange() {
$summary = new FacetsSummary([], 'facets_summary');
$summary->setFacetSourceId('foo');
$build = ['#items' => []];
$this->processor->setConfiguration(['text' => ['value' => 'Owl', 'format' => 'llama']]);
$result = $this->processor->build($summary, $build, []);
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('#text', $result['#message']);
$this->assertEquals('Owl', (string) $result['#message']['#text']);
$this->assertEquals('llama', $result['#message']['#format']);
}
}
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