Commit 3cdc87e9 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2860393 by borisson_, acbramley: API break: Add getViewsDisplay method...

Issue #2860393 by borisson_, acbramley: API break: Add getViewsDisplay method to the Search API Facet Source
parent 3773e7a5
......@@ -9,6 +9,7 @@ use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetSource\FacetSourcePluginInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets_summary\Processor\ProcessorPluginManager;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\views\Views;
......@@ -249,25 +250,13 @@ class FacetsSummarySettingsForm extends EntityForm {
}
// Ensure that the caching of the view display is disabled, so the search
// correctly returns the facets. First, get the plugin definition of the
// Search API display.
// correctly returns the facets.
$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'];
$view = Views::getView($view_id);
$view->setDisplay($view_display);
if (isset($facet_source) && $facet_source instanceof SearchApiFacetSourceInterface) {
$view = $facet_source->getViewsDisplay();
if ($view !== NULL) {
$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()]));
}
}
......
......@@ -23,4 +23,16 @@ interface SearchApiFacetSourceInterface extends FacetSourcePluginInterface {
*/
public function getDisplay();
/**
* Retrieves the Views entity with the correct display set.
*
* This returns NULL when the facet source is not based on views. If it is, it
* returns a ViewsExecutable plugin with the correct display already set.
*
* @return \Drupal\views\ViewExecutable|null
* NULL when the view can't be found or loaded, the view with preset display
* otherwise.
*/
public function getViewsDisplay();
}
......@@ -8,10 +8,9 @@ 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\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets\Processor\ProcessorPluginManager;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -120,6 +119,10 @@ class FacetSettingsForm extends EntityForm {
/**
* Builds the form for editing and creating a facet.
*
* @param array $form
* The form array for the complete form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param \Drupal\facets\FacetInterface $facet
* The facets facet entity that is being created or edited.
*/
......@@ -317,24 +320,12 @@ class FacetSettingsForm extends EntityForm {
}
// Ensure that the caching of the view display is disabled, so the search
// 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);
// correctly returns the facets.
if (isset($facet_source) && $facet_source instanceof SearchApiFacetSourceInterface) {
$view = $facet_source->getViewsDisplay();
if ($view !== NULL) {
$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\facets\Plugin\facets\facet_source;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\Exception\InvalidQueryTypeException;
use Drupal\facets\FacetInterface;
......@@ -56,6 +57,13 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
*/
protected $request;
/**
* The Drupal module handler.
*
* @var \Drupal\Core\Extension\ModuleHandler
*/
protected $moduleHandler;
/**
* Constructs a SearchApiBaseFacetSource object.
*
......@@ -73,12 +81,15 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
* The display plugin manager.
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object for the current request.
* @param \Drupal\Core\Extension\ModuleHandler $moduleHandler
* Core's module handler class.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryTypePluginManager $query_type_plugin_manager, QueryHelper $search_results_cache, DisplayPluginManager $display_plugin_manager, Request $request) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryTypePluginManager $query_type_plugin_manager, QueryHelper $search_results_cache, DisplayPluginManager $display_plugin_manager, Request $request, ModuleHandler $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager);
$this->searchApiQueryHelper = $search_results_cache;
$this->displayPluginManager = $display_plugin_manager;
$this->moduleHandler = $moduleHandler;
$this->request = clone $request;
}
......@@ -100,7 +111,8 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
$container->get('plugin.manager.facets.query_type'),
$container->get('search_api.query_helper'),
$container->get('plugin.manager.search_api.display'),
$container->get('request_stack')->getMasterRequest()
$container->get('request_stack')->getMasterRequest(),
$container->get('module_handler')
);
}
......@@ -326,4 +338,25 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
->createInstance($this->pluginDefinition['display_id']);
}
/**
* {@inheritdoc}
*/
public function getViewsDisplay() {
if (!$this->moduleHandler->moduleExists('views')) {
return NULL;
}
$search_api_display_definition = $this->getDisplay()->getPluginDefinition();
if (empty($search_api_display_definition['view_id'])) {
return NULL;
}
$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);
return $view;
}
}
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