Commit edb04f5c authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2877691 by borisson_: Only display Facet Widget if certain conditions are met?

parent af8df94d
......@@ -129,6 +129,14 @@ class FacetForm extends EntityForm {
foreach ($this->widgetPluginManager->getDefinitions() as $widget_id => $definition) {
$widget_options[$widget_id] = !empty($definition['label']) ? $definition['label'] : $widget_id;
}
// Filters all the available widgets to make sure that only those that
// this facet applies for are enabled.
$widget_options = array_filter($widget_options, function ($widget_id) use ($facet) {
$widget = $this->widgetPluginManager->createInstance($widget_id);
return $widget->supportsFacet($facet);
}, ARRAY_FILTER_USE_KEY);
$form['widget'] = [
'#type' => 'radios',
'#title' => $this->t('Widget'),
......@@ -178,10 +186,20 @@ class FacetForm extends EntityForm {
}
$enabled_processors = $facet->getProcessors(TRUE);
// Filters all the available processors to make sure that only those that
// this facet applies for are enabled.
$all_processors = array_filter($all_processors, function ($id) use ($facet) {
$processor = $this->processorPluginManager->createInstance($id, ['facet' => $facet]);
return $processor->supportsFacet($facet);
}, ARRAY_FILTER_USE_KEY);
$stages = $this->processorPluginManager->getProcessingStages();
$processors_by_stage = [];
foreach ($stages as $stage => $definition) {
$processors_by_stage[$stage] = $facet->getProcessorsByStage($stage, FALSE);
$processors_by_stage[$stage] = array_filter($facet->getProcessorsByStage($stage, FALSE), function ($id) use ($facet) {
$processor = $this->processorPluginManager->createInstance($id, ['facet' => $facet]);
return $processor->supportsFacet($facet);
}, ARRAY_FILTER_USE_KEY);
}
$form['#tree'] = TRUE;
......
......@@ -111,4 +111,19 @@ interface ProcessorInterface extends ConfigurablePluginInterface, PluginInspecti
*/
public function getDescription();
/**
* Checks if the facet is supported by this widget.
*
* Reasons why this would be unsupported can be chosen by the widget.
*
* @param \Drupal\facets\FacetInterface $facet
* The facet to check for.
*
* @return bool
* Returns true when allowed, false otherwise.
*
* @see \Drupal\facets\Widget\WidgetPluginInterface::supportsFacet
*/
public function supportsFacet(FacetInterface $facet);
}
......@@ -107,4 +107,11 @@ class ProcessorPluginBase extends PluginBase implements ProcessorInterface {
return $this->dependencies;
}
/**
* {@inheritdoc}
*/
public function supportsFacet(FacetInterface $facet) {
return TRUE;
}
}
......@@ -245,4 +245,11 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
return FALSE;
}
/**
* {@inheritdoc}
*/
public function supportsFacet(FacetInterface $facet) {
return TRUE;
}
}
......@@ -51,6 +51,21 @@ interface WidgetPluginInterface extends ConfigurablePluginInterface {
*/
public function isPropertyRequired($name, $type);
/**
* Checks if the facet is supported by this processor.
*
* Reasons why this would be unsupported can be chosen by the processor.
*
* @param \Drupal\facets\FacetInterface $facet
* The facet to check for.
*
* @return bool
* Returns true when allowed, false otherwise.
*
* @see \Drupal\facets\Processor\ProcessorInterface::supportsFacet
*/
public function supportsFacet(FacetInterface $facet);
/**
* Provides a configuration form for this widget.
*
......
......@@ -39,4 +39,11 @@ class TestPreQuery extends ProcessorPluginBase implements PreQueryProcessorInter
drupal_set_message($this->getConfiguration()['test_value']);
}
/**
* {@inheritdoc}
*/
public function supportsFacet(FacetInterface $facet) {
return \Drupal::state()->get('facets_test_supports_facet', TRUE);
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\facets_custom_widget\Plugin\facets\widget;
use Drupal\facets\FacetInterface;
use Drupal\facets\Widget\WidgetPluginBase;
/**
......@@ -29,4 +30,11 @@ class CustomWidget extends WidgetPluginBase {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function supportsFacet(FacetInterface $facet) {
return \Drupal::state()->get('facets_test_supports_facet', TRUE);
}
}
......@@ -646,4 +646,25 @@ class ProcessorIntegrationTest extends FacetsTestBase {
$this->assertSession()->pageTextContains('Llama');
}
/**
* Tests the facet support for a widget.
*/
public function testSupportsFacet() {
$id = 'masked_owl';
$this->createFacet('Australian masked owl', $id);
// Go the the facet edit page and check to see if the custom processor shows
// up.
$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
$this->assertSession()->pageTextContains('test pre query');
// Make the ::supportsFacet method on the custom processor return false.
\Drupal::state()->set('facets_test_supports_facet', FALSE);
// Go to the facet edit page and check to see if the custom processor is
// now hidden.
$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
$this->assertSession()->pageTextNotContains('test pre query');
}
}
......@@ -152,4 +152,25 @@ class WidgetIntegrationTest extends FacetsTestBase {
$this->assertSession()->checkboxChecked('edit-facet-settings-show-only-one-result');
}
/**
* Tests the facet support for a widget.
*/
public function testSupportsFacet() {
$id = 'masked_owl';
$this->createFacet('Australian masked owl', $id);
// Go the the facet edit page and check to see if the custom widget shows
// up.
$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
$this->assertSession()->pageTextContains('Custom widget');
// Make the ::supportsFacet method on the custom widget return false.
\Drupal::state()->set('facets_test_supports_facet', FALSE);
// Go to the facet edit page and check to see if the custom widget is now
// hidden.
$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
$this->assertSession()->pageTextNotContains('Custom widget');
}
}
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