ShowTextWhenEmptyProcessor.php in Facets 8
File
modules/facets_summary/src/Plugin/facets_summary/processor/ShowTextWhenEmptyProcessor.php
View source
<?php
namespace Drupal\facets_summary\Plugin\facets_summary\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets_summary\FacetsSummaryInterface;
use Drupal\facets_summary\Processor\BuildProcessorInterface;
use Drupal\facets_summary\Processor\ProcessorPluginBase;
class ShowTextWhenEmptyProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
$config = $this
->getConfiguration();
$results_count = array_sum(array_map(function ($it) {
return count($it
->getResults());
}, $facets));
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 $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetsSummaryInterface $facets_summary) {
$config = $this
->getConfiguration();
$build['text'] = [
'#type' => 'text_format',
'#title' => $this
->t('Empty text'),
'#format' => $config['text']['format'],
'#editor' => TRUE,
'#default_value' => $config['text']['value'],
];
return $build;
}
public function defaultConfiguration() {
return [
'text' => [
'format' => 'plain_text',
'value' => $this
->t('No results found.'),
],
];
}
}