Commit 199477d3 authored by marcvangend's avatar marcvangend Committed by borisson_

Issue #2859139 by borisson_, marcvangend: Automatically disable caching of...

Issue #2859139 by borisson_, marcvangend: Automatically disable caching of views facet sources (regression) and add tests
parent 9ecdf88d
......@@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetSource\FacetSourcePluginInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets_summary\Processor\ProcessorPluginManager;
use Drupal\Core\Routing\UrlGeneratorInterface;
......@@ -206,18 +207,27 @@ class FacetsSummarySettingsForm extends EntityForm {
$facets_summary->save();
// Ensure that the caching of the view display is disabled, so the search
// correctly returns the facets. Only apply this when the facet source is
// actually a view by exploding on :.
// correctly returns the facets. First, get the plugin definition of the
// Search API display.
$facet_source_id = $form_state->getValue('facet_source_id');
list($type,) = explode(':', $facet_source_id);
$facet_source = $this->facetSourcePluginManager->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
if (isset($facet_source) && $facet_source instanceof FacetSourcePluginInterface) {
$facet_source_display_id = $facet_source->getPluginDefinition()['display_id'];
$search_api_display = \Drupal::service('plugin.manager.search_api.display')
->createInstance($facet_source_display_id);
$search_api_display_definition = $search_api_display->getPluginDefinition();
// Get the view of the Search API display and disable caching.
if (!empty($search_api_display_definition['view_id'])) {
$view_id = $search_api_display_definition['view_id'];
$view_display = $search_api_display_definition['view_display'];
if ($type === 'search_api_views') {
list(, $view_id, $display) = explode(':', $facet_source_id);
if (isset($view_id)) {
$view = Views::getView($view_id);
$view->setDisplay($display);
$view->setDisplay($view_display);
$view->display_handler->overrideOption('cache', ['type' => 'none']);
$view->save();
drupal_set_message($this->t('Caching of view %view has been disabled.', ['%view' => $view->storage->label()]));
}
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\facets_summary\Functional;
use Drupal\Tests\facets\Functional\FacetsTestBase;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -170,4 +171,37 @@ class IntegrationTest extends FacetsTestBase {
->findAll('css', 'li');
$this->assertCount(2, $list_items);
}
/**
* Check that the disabling of the cache works.
*/
public function testViewsCacheDisable() {
// Load the view, verify cache settings.
$view = Views::getView('search_api_test_view');
$view->setDisplay('page_1');
$current_cache = $view->display_handler->getOption('cache');
$this->assertEquals('none', $current_cache['type']);
$view->display_handler->setOption('cache', ['type' => 'tag']);
$view->save();
$current_cache = $view->display_handler->getOption('cache');
$this->assertEquals('tag', $current_cache['type']);
// Create a facet and check for the cache disabled message.
$id = "western_screech_owl";
$name = "Western screech owl";
$values = [
'name' => $name,
'id' => $id,
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
$this->assertSession()->pageTextContains('Caching of view Search API Test Fulltext search view has been disabled.');
// Check the view's cache settings again to see if they've been updated.
$view = Views::getView('search_api_test_view');
$view->setDisplay('page_1');
$current_cache = $view->display_handler->getOption('cache');
$this->assertEquals('none', $current_cache['type']);
}
}
......@@ -9,6 +9,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\Processor\ProcessorPluginManager;
use Drupal\views\Views;
......@@ -308,21 +309,28 @@ class FacetSettingsForm extends EntityForm {
$facet->save();
// Ensure that the caching of the view display is disabled, so the search
// correctly returns the facets. Only apply this when the facet source is
// actually a view by exploding on :.
list($type,) = explode(':', $facet_source_id);
if ($type === 'search_api_views') {
list(, $view_id, $display) = explode(':', $facet_source_id);
}
if (isset($view_id)) {
$view = Views::getView($view_id);
$view->setDisplay($display);
$view->display_handler->overrideOption('cache', ['type' => 'none']);
$view->save();
$display_plugin = $view->getDisplay()->getPluginId();
// correctly returns the facets. First, get the plugin definition of the
// Search API display.
if (isset($facet_source) && $facet_source instanceof FacetSourcePluginInterface) {
$facet_source_display_id = $facet_source->getPluginDefinition()['display_id'];
$search_api_display = \Drupal::service('plugin.manager.search_api.display')
->createInstance($facet_source_display_id);
$search_api_display_definition = $search_api_display->getPluginDefinition();
// Get the view of the Search API display and disable caching.
if (!empty($search_api_display_definition['view_id'])) {
$view_id = $search_api_display_definition['view_id'];
$view_display = $search_api_display_definition['view_display'];
$view = Views::getView($view_id);
$view->setDisplay($view_display);
$view->display_handler->overrideOption('cache', ['type' => 'none']);
$view->save();
drupal_set_message($this->t('Caching of view %view has been disabled.', ['%view' => $view->storage->label()]));
$display_plugin = $view->getDisplay()->getPluginId();
}
}
if ($is_new) {
......@@ -332,7 +340,7 @@ class FacetSettingsForm extends EntityForm {
$form_state->setRedirect('entity.facets_facet.edit_form', ['facets_facet' => $facet->id()]);
}
if (isset($view_id) && $display_plugin === 'block') {
if (isset($view_id, $display_plugin) && $display_plugin === 'block') {
$facet->setOnlyVisibleWhenFacetSourceIsVisible(FALSE);
}
}
......
......@@ -5,6 +5,7 @@ namespace Drupal\Tests\facets\Functional;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\views\Entity\View;
use Drupal\views\Views;
/**
* Tests the overall functionality of the Facets admin UI.
......@@ -769,6 +770,34 @@ class IntegrationTest extends FacetsTestBase {
$this->assertText('Eastern screech owl');
}
/**
* Check that the disabling of the cache works.
*/
public function testViewsCacheDisable() {
// Load the view, verify cache settings.
$view = Views::getView('search_api_test_view');
$view->setDisplay('page_1');
$current_cache = $view->display_handler->getOption('cache');
$this->assertEquals('none', $current_cache['type']);
$view->display_handler->setOption('cache', ['type' => 'tag']);
$view->save();
$current_cache = $view->display_handler->getOption('cache');
$this->assertEquals('tag', $current_cache['type']);
// Create a facet and check for the cache disabled message.
$id = "western_screech_owl";
$name = "Western screech owl";
$this->createFacet($name, $id);
$this->drupalPostForm('admin/config/search/facets/' . $id . '/settings', [], 'Save');
$this->assertSession()->pageTextContains('Caching of view Search API Test Fulltext search view has been disabled.');
// Check the view's cache settings again to see if they've been updated.
$view = Views::getView('search_api_test_view');
$view->setDisplay('page_1');
$current_cache = $view->display_handler->getOption('cache');
$this->assertEquals('none', $current_cache['type']);
}
/**
* Configures empty behavior option to show a text on empty results.
*
......
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