Commit 9e162f1d authored by jsbalsera's avatar jsbalsera Committed by borisson_

Issue #2862982 by jsbalsera, borisson_: Selecting the wrong widget in a Facet...

Issue #2862982 by jsbalsera, borisson_: Selecting the wrong widget in a Facet for a Rest export causes an exception and a white page
parent 5dc5052a
......@@ -213,4 +213,26 @@ class RestIntegrationTest extends FacetsTestBase {
}
/**
* Tests that the system raises an error when selecting the wrong widget.
*/
public function testWidgetSelection() {
$name = 'Type';
$id = 'type';
// Add a new facet to filter by content type.
$this->createFacet($name, $id, 'type', 'rest_export_1', 'views_rest__search_api_rest_test_view');
// Use the array widget.
$facet_edit_page = '/admin/config/search/facets/' . $id . '/edit';
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->drupalPostForm(NULL, ['widget' => 'checkbox'], $this->t('Configure widget'));
$this->assertText('The Facet source is a Rest export. Please select a raw widget.');
$this->drupalPostForm(NULL, ['widget' => 'array'], $this->t('Configure widget'));
$this->assertNoText('The Facet source is a Rest export. Please select a raw widget.');
}
}
......@@ -5,7 +5,7 @@ namespace Drupal\facets\FacetSource;
/**
* A facet source that uses Search API as a base.
*/
interface SearchApiFacetSourceInterface {
interface SearchApiFacetSourceInterface extends FacetSourcePluginInterface {
/**
* Returns the search_api index.
......@@ -15,4 +15,12 @@ interface SearchApiFacetSourceInterface {
*/
public function getIndex();
/**
* Retrieves the Search API display plugin associated with this facet source.
*
* @return \Drupal\search_api\Display\DisplayInterface
* The Search API display plugin associated with this facet source.
*/
public function getDisplay();
}
......@@ -575,6 +575,15 @@ class FacetForm extends EntityForm {
}
}
// Only widgets that return an array can work with rest facet sources, so if
// the user has selected another widget, we should point them to their
// misconfiguration.
if ($facet->getFacetSource()->getDisplay() instanceof \Drupal\search_api\Plugin\search_api\display\ViewsRestDisplay) {
if (strpos($values['widget'], 'array') === FALSE) {
$form_state->setErrorByName('widget', $this->t('The Facet source is a Rest export. Please select a raw widget.'));
}
}
// Validate url alias.
$url_alias = $form_state->getValue(['facet_settings', 'url_alias']);
if ($url_alias == 'page') {
......
......@@ -8,6 +8,7 @@ use Drupal\Core\Url;
use Drupal\facets\Exception\InvalidQueryTypeException;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginBase;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets\QueryType\QueryTypePluginManager;
use Drupal\search_api\Backend\BackendInterface;
use Drupal\search_api\Display\DisplayPluginManager;
......@@ -26,7 +27,7 @@ use Symfony\Component\HttpFoundation\Request;
* deriver = "Drupal\facets\Plugin\facets\facet_source\SearchApiDisplayDeriver"
* )
*/
class SearchApiDisplay extends FacetSourcePluginBase {
class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSourceInterface {
/**
* The search index the query should is executed on.
......@@ -311,12 +312,9 @@ class SearchApiDisplay extends FacetSourcePluginBase {
}
/**
* Retrieves the Search API display plugin associated with this facet source.
*
* @return \Drupal\search_api\Display\DisplayInterface
* The Search API display plugin associated with this facet source.
* {@inheritdoc}
*/
protected function getDisplay() {
public function getDisplay() {
return $this->displayPluginManager
->createInstance($this->pluginDefinition['display_id']);
}
......
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