Commit 96e0be97 authored by Jur de Vries's avatar Jur de Vries
Browse files

Issue #2588791: Execution of the view should be moved to the facet source from the adapter

parent 2e00f303
......@@ -59,7 +59,7 @@ function facetapi_get_enabled_facets() {
* @param \Drupal\search_api\Query\QueryInterface $query
*/
function facetapi_search_api_query_alter(QueryInterface &$query) {
// Get the facet_manager derivative.
// Get the adapter derivative.
// Get the plugin manager.
/** @var \Drupal\facetapi\FacetManager\FacetManagerPluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.facetapi.manager');
......@@ -67,7 +67,7 @@ function facetapi_search_api_query_alter(QueryInterface &$query) {
// This is the id of the view.
$search_id = $query->getOption('search id');
$plugin_id = 'search_api_views';
$plugin_id = 'facetapi_default';
$manager = $plugin_manager->getMyOwnChangeLaterInstance($plugin_id, $search_id);
......
......@@ -126,6 +126,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $facet_source;
/**
* The path all the links should point to.
*
* @var string
*/
protected $path;
/**
* The results.
*
......@@ -317,7 +324,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
* @return string
*/
public function getManagerPluginId() {
return 'search_api_views';
return 'facetapi_default';
}
/**
......@@ -364,4 +371,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return array_intersect_key($this->facetSourcePlugins, array_flip($this->facetSourcePlugins));
}
public function setPath($path) {
$this->path = $path;
}
public function getPath() {
return $this->path;
}
}
......@@ -183,4 +183,18 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getFacetSources($only_enabled = TRUE);
/**
* Get the path to which the facet should link.
*
* @param string $path
*/
public function setPath($path);
/**
* Get the path to which the facet should link.
*
* @return NULL|string
*/
public function getPath();
}
......@@ -11,6 +11,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetSource\FacetSourcePluginManager;
use Drupal\facetapi\QueryType\QueryTypePluginManager;
use Drupal\facetapi\Result\Result;
use Drupal\facetapi\UrlProcessor\UrlProcessorInterface;
......@@ -45,6 +46,13 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager
*/
protected $url_processor_plugin_manager;
/**
* The facet source plugin manager.
*
* @var FacetSourcePluginManager
*/
protected $facet_source_manager;
/**
* @var ModuleHandlerInterface
*/
......@@ -137,7 +145,10 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager
/** @var \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager */
$widget_plugin_manager = $container->get('plugin.manager.facetapi.widget');
$plugin = new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $url_processor_plugin_manager, $widget_plugin_manager);
/** @var \Drupal\facetapi\FacetManager\FacetSourcePluginManager $facet_source_plugin_manager */
$facet_source_plugin_manager = $container->get('plugin.manager.facetapi.facet_source');
$plugin = new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $url_processor_plugin_manager, $widget_plugin_manager, $facet_source_plugin_manager);
return $plugin;
}
......@@ -165,7 +176,8 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager
ModuleHandlerInterface $module_handler,
QueryTypePluginManager $query_type_plugin_manager,
UrlProcessorPluginManager $url_processor_plugin_manager,
WidgetPluginManager $widget_plugin_manager
WidgetPluginManager $widget_plugin_manager,
FacetSourcePluginManager $facet_source_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
......@@ -173,6 +185,7 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager
$this->query_type_plugin_manager = $query_type_plugin_manager;
$this->url_processor_plugin_manager = $url_processor_plugin_manager;
$this->widget_plugin_manager = $widget_plugin_manager;
$this->facet_source_manager = $facet_source_manager;
// Immediately initialize the facets.
// This can be done directly because the only thing needed is
......
......@@ -47,4 +47,12 @@ interface FacetSourceInterface {
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet, FacetSourceInterface $facet_source);
/**
* Add the results to the facets.
*
* @param FacetInterface[] $facets
* @return mixed
*/
public function addResults($facets);
}
......@@ -8,6 +8,8 @@
namespace Drupal\facetapi\FacetSource;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a base class from which other facet sources may extend.
......@@ -23,7 +25,12 @@ use Drupal\Component\Plugin\PluginBase;
* @see \Drupal\facetapi\FacetSource\FacetSourceInterface
* @see plugin_api
*/
abstract class FacetSourcePluginBase extends PluginBase implements FacetSourceInterface {
abstract class FacetSourcePluginBase extends PluginBase implements FacetSourceInterface, ContainerFactoryPluginInterface {
/**
* The plugin manager.
*/
protected $query_type_plugin_manager;
public function getAllowedQueryTypes() {
return [];
......@@ -32,4 +39,29 @@ abstract class FacetSourcePluginBase extends PluginBase implements FacetSourceIn
public function getFields() {
return [];
}
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
$query_type_plugin_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->query_type_plugin_manager = $query_type_plugin_manager;
}
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
// Insert the plugin manager for query types.
/** @var \Drupal\facetapi\QueryType\QueryTypePluginManager $query_type_plugin_manager */
$query_type_plugin_manager = $container->get('plugin.manager.facetapi.query_type');
return new static($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager);
}
}
......@@ -84,7 +84,7 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface {
}
// This should be changeable when we support more than just search API.
$plugin_id = 'search_api_views';
$plugin_id = 'facetapi_default';
/** @var \Drupal\facetapi\FacetManager\FacetManagerInterface $manager */
$manager = $this->pluginManager->getMyOwnChangeLaterInstance(
......
......@@ -7,6 +7,8 @@ namespace Drupal\facetapi\Plugin\facetapi\facet_manager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\facetapi\FacetManager\FacetManagerPluginBase;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetSource\FacetSourceInterface;
use Drupal\facetapi\FacetSource\FacetSourcePluginManager;
use Drupal\facetapi\QueryType\QueryTypePluginManager;
use Drupal\facetapi\UrlProcessor\UrlProcessorPluginManager;
use Drupal\facetapi\Widget\WidgetPluginManager;
......@@ -17,12 +19,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @FacetApiFacetManager(
* id = "search_api_views",
* label = @Translation("Search api"),
* id = "facetapi_default",
* label = @Translation("Dafault manager"),
* description = @Translation("Search api facet api facet_manager"),
* )
*/
class SearchApiViewsFacetManager extends FacetManagerPluginBase {
class DefaultFacetManager extends FacetManagerPluginBase {
/**
* @var \Drupal\search_api\Query\QueryInterface
......@@ -54,8 +56,11 @@ class SearchApiViewsFacetManager extends FacetManagerPluginBase {
/** @var \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager */
$widget_plugin_manager = $container->get('plugin.manager.facetapi.widget');
/** @var FacetSourcePluginManager $facet_plugin_manager */
$facet_plugin_manager = $container->get('plugin.manager.facetapi.facet_source');
return new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $results_cache, $url_processor_plugin_manager, $widget_plugin_manager);
return new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $results_cache, $url_processor_plugin_manager, $widget_plugin_manager, $facet_plugin_manager);
}
public function __construct(
......@@ -65,10 +70,11 @@ class SearchApiViewsFacetManager extends FacetManagerPluginBase {
QueryTypePluginManager $query_type_plugin_manager,
ResultsCacheInterface $results_cache,
UrlProcessorPluginManager $url_processor_plugin_manager,
WidgetPluginManager $widget_plugin_manager
WidgetPluginManager $widget_plugin_manager,
FacetSourcePluginManager $facet_source_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $url_processor_plugin_manager, $widget_plugin_manager);
parent::__construct($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $url_processor_plugin_manager, $widget_plugin_manager, $facet_source_manager);
$this->searchResultsCache = $results_cache;
}
......@@ -77,28 +83,11 @@ class SearchApiViewsFacetManager extends FacetManagerPluginBase {
* method should disappear later when facetapi does it.
*/
public function updateResults() {
// Get the facet values from the query that has been done.
// Store all information in $this->facets.
$results = $this->searchResultsCache->getResults($this->searcher_id);
if ($results instanceof ResultSetInterface) {
$facet_results = $results->getExtraData('search_api_facets');
foreach ($this->facets as $facet) {
$configuration = array(
'query' => NULL,
'facet' => $facet,
'results' => $facet_results[$facet->getFieldIdentifier()],
);
$query_type_plugin = $this->query_type_plugin_manager->createInstance($facet->getQueryType(),
$configuration
);
$query_type_plugin->build();
}
}
else {
// @Todo: perform the query so there are results.
}
// Get an instance of the facet source.
/** @var FacetSourceInterface $facet_source_plugin */
$facet_source_plugin = $this->facet_source_manager->createInstance($this->searcher_id);
$facet_source_plugin->addResults($this->facets);
}
}
......@@ -14,7 +14,9 @@ use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetSource\FacetSourceInterface;
use Drupal\facetapi\FacetSource\FacetSourcePluginBase;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -52,17 +54,41 @@ class SearchApiViews extends FacetSourcePluginBase {
*/
protected $configFactory;
/**
* The search result cache.
*
* @var \Drupal\search_api\Query\ResultsCacheInterface
*/
protected $searchResultsCache;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
public function __construct(array $configuration, $plugin_id, array $plugin_definition, $query_type_plugin_manager, $search_results_cache) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager);
// Since defaultConfiguration() depends on the plugin definition, we need to
// override the constructor and set the definition property before calling
// that method.
$this->pluginDefinition = $plugin_definition;
$this->pluginId = $plugin_id;
$this->configuration = $configuration + $this->defaultConfiguration();
$this->searchResultsCache = $search_results_cache;
}
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
// Insert the plugin manager for query types.
/** @var \Drupal\facetapi\QueryType\QueryTypePluginManager $query_type_plugin_manager */
$query_type_plugin_manager = $container->get('plugin.manager.facetapi.query_type');
// Get the ResultsCache from the container.
/** @var \Drupal\search_api\Query\ResultsCacheInterface $results_cache */
$search_results_cache = $container->get('search_api.results_static_cache');
return new static($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager, $search_results_cache);
}
/**
......@@ -113,4 +139,47 @@ class SearchApiViews extends FacetSourcePluginBase {
return $form;
}
public function addResults($facets) {
// Get the facet values from the query that has been done.
// Store all information in $this->facets.
$results = $this->searchResultsCache->getResults($this->pluginId);
if (! $results instanceof ResultSetInterface) {
// If there are no results, execute the view. and check for results again!
$view = Views::getView($this->pluginDefinition['view_id']);
$view->setDisplay($this->pluginDefinition['view_display']);
$view->execute();
// Set the path of all facets.
$path = $view->getDisplay()->getOption('path');
if ($path) {
foreach ($facets as $facet) {
$facet->setPath($path);
}
}
$results = $this->searchResultsCache->getResults($this->pluginId);
}
if ($results instanceof ResultSetInterface) {
$facet_results = $results->getExtraData('search_api_facets');
foreach ($facets as $facet) {
$configuration = array(
'query' => NULL,
'facet' => $facet,
'results' => $facet_results[$facet->getFieldIdentifier()],
);
$query_type_plugin = $this->query_type_plugin_manager->createInstance($facet->getQueryType(),
$configuration
);
// @TODO: This should be done somewhere else.
$query_type_plugin->build();
}
}
else {
// @Todo: perform the query so there are results.
}
}
}
......@@ -7,7 +7,6 @@
namespace Drupal\facetapi\Plugin\facetapi\query_type;
use Drupal\facetapi\FacetManager\FacetManagerInterface;
use \Drupal\facetapi\QueryType\QueryTypePluginBase;
use Drupal\facetapi\Result\Result;
use Drupal\search_api\Query\Query;
......
......@@ -43,7 +43,7 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{
public function addUriToResults(FacetInterface $facet) {
// Create links for all the values.
// First get the current list of get paramaters.
// First get the current list of get parameters.
$get_params = $this->request->query;
$results = $facet->getResults();
......@@ -73,7 +73,11 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{
}
$result_get_params->set($this->filter_key, $filter_params);
$url = Url::createFromRequest($this->request);
$request = $this->request;
if ($facet->getPath()) {
$request = Request::create('/' . $facet->getPath());
}
$url = Url::createFromRequest($request);
$url->setOption('query', $result_get_params->all());
$result->setUrl($url);
}
......
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