Commit aa8cbf65 authored by borisson_'s avatar borisson_ Committed by borisson_
Browse files

Issue #2611198 by borisson_, mollux, StryKaizer: Give widgets the ability to require settings

parent eddd2d00
......@@ -138,6 +138,7 @@ class FacetForm extends EntityForm {
/** @var \Drupal\facets\FacetInterface $facet */
$facet = $this->entity;
$widget = $facet->getWidgetInstance();
$widget_options = [];
foreach ($this->getWidgetPluginManager()->getDefinitions() as $widget_id => $definition) {
......@@ -213,13 +214,16 @@ class FacetForm extends EntityForm {
),
),
);
foreach ($all_processors as $processor_id => $processor) {
if (!($processor instanceof SortProcessorInterface) && !($processor instanceof UrlProcessorInterface)) {
$default_value = $processor->isLocked() || $widget->isPropertyRequired($processor_id, 'processors') || !empty($enabled_processors[$processor_id]);
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['facet_settings'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($enabled_processors[$processor_id]),
'#default_value' => $default_value,
'#description' => $processor->getDescription(),
'#attributes' => array(
'class' => array(
......@@ -227,7 +231,7 @@ class FacetForm extends EntityForm {
),
'data-id' => $clean_css_id,
),
'#disabled' => $processor->isLocked(),
'#disabled' => $processor->isLocked() || $widget->isPropertyRequired($processor_id, 'processors'),
'#access' => !$processor->isHidden(),
);
......@@ -269,11 +273,12 @@ class FacetForm extends EntityForm {
);
foreach ($all_processors as $processor_id => $processor) {
if ($processor instanceof SortProcessorInterface) {
$default_value = $processor->isLocked() || $widget->isPropertyRequired($processor_id, 'processors') || !empty($enabled_processors[$processor_id]);
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['facet_sorting'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($enabled_processors[$processor_id]),
'#default_value' => $default_value,
'#description' => $processor->getDescription(),
'#attributes' => array(
'class' => array(
......@@ -314,14 +319,16 @@ class FacetForm extends EntityForm {
'#type' => 'checkbox',
'#title' => $this->t('Hide facet when facet source is not rendered'),
'#description' => $this->t('When checked, this facet will only be rendered when the facet source is rendered. If you want to show facets on other pages too, you need to uncheck this setting.'),
'#default_value' => $facet->getOnlyVisibleWhenFacetSourceIsVisible(),
'#default_value' => $widget->isPropertyRequired('only_visible_when_facet_source_is_visible', 'settings') ?: $facet->getOnlyVisibleWhenFacetSourceIsVisible(),
'#disabled' => $widget->isPropertyRequired('only_visible_when_facet_source_is_visible', 'settings') ?: 0,
];
$form['facet_settings']['show_only_one_result'] = [
'#type' => 'checkbox',
'#title' => $this->t('Make sure only one result can be shown.'),
'#description' => $this->t('When checked, this will make sure that only one result can be selected for this facet at one time.'),
'#default_value' => $facet->getShowOnlyOneResult(),
'#default_value' => $widget->isPropertyRequired('show_only_one_result', 'settings') ?: $facet->getShowOnlyOneResult(),
'#disabled' => $widget->isPropertyRequired('show_only_one_result', 'settings') ?: 0,
];
$form['facet_settings']['url_alias'] = [
......
......@@ -215,4 +215,11 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
];
}
/**
* {@inheritdoc}
*/
public function isPropertyRequired($name, $type) {
return FALSE;
}
}
......@@ -33,6 +33,24 @@ interface WidgetPluginInterface extends ConfigurablePluginInterface {
*/
public function getQueryType(array $query_types);
/**
* Checks is a specific property is required for this widget.
*
* This works for base properties (show_only_one_result,
* only_visible_when_facet_source_is_visible) or one the processors.
*
* @param string $name
* The name of the property. Something like
* 'hide_non_narrowing_result_processor' or 'show_only_one_result'
* @param string $type
* The type of the property. Either 'processors' or 'settings'. Another
* value will not be picked up by the widgets.
*
* @return bool
* True when the property is required, false by default.
*/
public function isPropertyRequired($name, $type);
/**
* Provides a configuration form for this widget.
*
......
name: 'Facets custom widget'
type: module
description: 'Facets custom widget'
package: 'Search'
core: 8.x
hidden: true
<?php
namespace Drupal\facets_custom_widget\Plugin\facets\widget;
use Drupal\facets\Widget\WidgetPluginBase;
/**
* A simple widget class that returns a simple array of the facet results.
*
* @FacetsWidget(
* id = "custom_widget",
* label = @Translation("Custom widget"),
* description = @Translation("Custom widget"),
* )
*/
class CustomWidget extends WidgetPluginBase {
/**
* {@inheritdoc}
*/
public function isPropertyRequired($name, $type) {
if ($type == 'processors' && $name == 'hide_non_narrowing_result_processor') {
return TRUE;
}
if ($type == 'settings' && $name == 'show_only_one_result') {
return TRUE;
}
return FALSE;
}
}
......@@ -20,6 +20,7 @@ class WidgetIntegrationTest extends FacetsTestBase {
'block',
'facets_search_api_dependency',
'facets_query_processor',
'facets_custom_widget',
];
/**
......@@ -132,4 +133,28 @@ class WidgetIntegrationTest extends FacetsTestBase {
$this->assertFacetLabel('article');
}
/**
* Tests custom widget.
*
* ::requiredFacetProperties in the custom widget requires the
* hide_non_narrowing_result_processor processor, so check that it's enabled
* after the custom widget is selected.
*/
public function testCustomWidget() {
$id = 'custom_widget';
$name = 'Custom widget.';
$this->createFacet($name, $id);
$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
$this->assertNoFieldChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
$this->assertNoFieldChecked('edit-facet-settings-show-only-one-result');
$this->drupalPostForm(NULL, ['widget' => 'custom_widget'], $this->t('Configure widget'));
$this->drupalPostForm(NULL, ['widget' => 'custom_widget'], $this->t('Save'));
$this->assertFieldChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
$this->assertFieldChecked('edit-facet-settings-show-only-one-result');
}
}
......@@ -50,4 +50,12 @@ class CheckboxWidgetTest extends WidgetTestBase {
}
}
/**
* Tests default configuration.
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$this->assertEquals(['show_numbers' => FALSE, 'soft_limit' => 0], $default_config);
}
}
......@@ -89,6 +89,13 @@ abstract class WidgetTestBase extends UnitTestCase {
$this->assertEquals('string', $result);
}
/**
* Tests default for required properties.
*/
public function testIsPropertyRequired() {
$this->assertFalse($this->widget->isPropertyRequired('llama', 'owl'));
}
/**
* Build a formattable markup object to use as assertion.
*
......
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