Skip to content
Snippets Groups Projects

Expanded existing functional test to cover base module functionality.

Files
10
@@ -11,9 +11,6 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Template\Attribute;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\facets_summary\FacetsSummaryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -28,13 +25,6 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
use UncacheableDependencyTrait;
/**
* The Default Facet Manager.
*
* @var \Drupal\facets\FacetManager\DefaultFacetManager
*/
protected $facetsManager;
/**
* The Module Handler Interface.
*
@@ -72,8 +62,6 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\facets\FacetManager\DefaultFacetManager $facets_manager
* The Facets manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Block\BlockManagerInterface $plugin_manager_block
@@ -83,9 +71,8 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, DefaultFacetManager $facets_manager, ModuleHandlerInterface $module_handler, BlockManagerInterface $plugin_manager_block, AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, BlockManagerInterface $plugin_manager_block, AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->facetsManager = $facets_manager;
$this->moduleHandler = $module_handler;
$this->pluginManagerBlock = $plugin_manager_block;
$this->currentUser = $current_user;
@@ -100,7 +87,6 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
$configuration,
$plugin_id,
$plugin_definition,
$container->get('facets.manager'),
$container->get('module_handler'),
$container->get('plugin.manager.block'),
$container->get('current_user'),
@@ -120,20 +106,20 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
$form['block_settings']['show_title'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show Facet titles'),
'#default_value' => isset($this->configuration['show_title']) ? $this->configuration['show_title'] : TRUE,
'#default_value' => $this->configuration['show_title'] ?? TRUE,
];
$form['block_settings']['exclude_empty_facets'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclude empty facets'),
'#default_value' => isset($this->configuration['exclude_empty_facets']) ? $this->configuration['exclude_empty_facets'] : TRUE,
'#default_value' => $this->configuration['exclude_empty_facets'] ?? TRUE,
];
$form['block_settings']['hide_empty_block'] = [
'#type' => 'checkbox',
'#title' => $this->t('Hide empty block'),
'#description' => $this->t("Don't render the Facets Block if no facets are available (for instance when no search results are found)."),
'#default_value' => isset($this->configuration['hide_empty_block']) ? $this->configuration['hide_empty_block'] : FALSE,
'#default_value' => $this->configuration['hide_empty_block'] ?? FALSE,
];
$form['block_settings']['add_js_classes'] = [
@@ -145,7 +131,7 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
$form['block_settings']['facets_to_include'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Facets to include'),
'#default_value' => isset($this->configuration['facets_to_include']) ? $this->configuration['facets_to_include'] : [],
'#default_value' => $this->configuration['facets_to_include'] ?? [],
'#options' => $this->getAvailableFacets(),
];
@@ -155,28 +141,40 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Returns a list of available facets.
*
* @param bool $include_source_title
* Whether or not source of the facet or facet summary should be include.
*
* @return array
* An array of enabled facets.
*/
protected function getAvailableFacets() {
protected function getAvailableFacets(bool $include_source_title = TRUE) {
$available_facets = [];
/** @var \Drupal\facets\FacetListBuilder $list_builder */
$list_builder = $this->entityTypeManager->getHandler('facets_facet', 'list_builder');
$facet_groups = $list_builder->loadGroups();
foreach ($facet_groups['facet_source_groups'] as $facet_data) {
foreach ($facet_data['facets'] as $facet) {
if ($facet instanceof FacetInterface) {
$available_facets['facet_block:' . $facet->id()] = $this->t('@facet_source: @facet_name', [
'@facet_source' => $facet_data['facet_source']['label'],
'@facet_name' => $facet->getName(),
]);
foreach ($facet_data['facets'] as $entity) {
switch ($entity->getEntityTypeId()) {
case 'facets_facet':
$base_plugin_id = 'facet_block';
break;
case 'facets_summary':
$base_plugin_id = 'facets_summary_block';
break;
default:
$base_plugin_id = '';
}
if ($facet instanceof FacetsSummaryInterface) {
$available_facets['facets_summary_block:' . $facet->id()] = $this->t('@facet_source: @facet_name', [
'@facet_source' => $facet_data['facet_source']['label'],
'@facet_name' => $facet->getName(),
]);
if (!empty($base_plugin_id)) {
$available_facets[sprintf('%s:%s', $base_plugin_id, $entity->id())] = !$include_source_title
? $entity->getName()
: $this->t('@facet_source: @facet_name', [
'@facet_source' => $facet_data['facet_source']['label'],
'@facet_name' => $entity->getName(),
]);
}
}
}
@@ -224,7 +222,7 @@ class FacetsBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected function buildFacets(array $facets_to_include) {
$facets = [];
$available_facets = $this->getAvailableFacets();
$available_facets = $this->getAvailableFacets(FALSE);
foreach (array_intersect_key($available_facets, array_combine($facets_to_include, $facets_to_include)) as $plugin_id => $facet_title) {
$block_plugin = $this->pluginManagerBlock->createInstance($plugin_id, []);
Loading