Commit bd2a8d5f authored by Jur de Vries's avatar Jur de Vries
Browse files

Integration of different parts and cleanup of code.

parent 2e40ebf6
......@@ -62,7 +62,6 @@ function facetapi_search_api_query_alter(QueryInterface &$query) {
// Get the plugin manager.
/** @var \Drupal\facetapi\Adapter\AdapterPluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.facetapi.adapter');
// Get the searcher id.
// This is the id of the view.
$search_id = $query->getOption('search id');
......
......@@ -144,15 +144,4 @@ interface AdapterInterface {
*/
public function build($facet);
/**
* Sets results to a facet.
*
* @param \Drupal\facetapi\FacetInterface $facet
* The facet to add results.
*
* @return \Drupal\facetapi\FacetInterface
* The facet with the results.
*/
public function setResults(FacetInterface $facet);
}
......@@ -286,12 +286,13 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
* An array of enabled facets.
*/
public function getEnabledFacets() {
// Use the hook_info to discover facets.
/** @var Facet[] $facet_definitions */
$facet_definitions = $this->module_handler->invokeAll('facetapi_facet_info');
// Get the enabled facets.
// @Todo: inject the entitymanager in the adapter and use that.
/** @var Facet[] $facets */
$facets = facetapi_get_enabled_facets();;
// Maybe also add different discovery methods later,
// for instance in the adapter itself.
return $facet_definitions;
return $facets;
}
......@@ -345,18 +346,28 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
}
public function build($facet) {
// It might be that the facet received here,
// is not the same as the already loaded facets in the adapter.
// For that reason, get the facet from the already loaded facets
// in the adapter.
// If this is omitted, building will fail.
$facet = $this->facets[$facet->id()];
// Process the facets.
// @TODO: inject the searcher id on create of the adapter.
$this->searcher_id = $facet->getFacetSource();
// @TODO: Should we update facets each time we build a block?
//$this->processFacets();
$facet = $this->setResults($facet);
// Let the plugin render the facet.
// @TODO: functionality to alter the state of the facet should
// somewhere else. Now here for speed reasons for proof of concept.
// For clarity, process facets is called each build.
// The first facet therefor will trigger the processing.
// Note that processing is done only once, so repeatedly
// calling this method will not trigger the processing more than once.
// Furthermore: don't add any processing after this method call!
// All processing should be done in the processFacets method.
// After the processFacets is finished, all information for rendering
// is added to the facet.
$this->processFacets();
// Let the widget plugin render the facet.
/** @var \Drupal\facetapi\Widget\WidgetInterface $widget */
$widget = $this->widget_plugin_manager->createInstance($facet->getWidget());
......@@ -372,10 +383,7 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
/** @var UrlProcessorInterface $url_processor */
$url_processor_name = $facet->getUrlProcessorName();
$url_processor = $this->url_processor_plugin_manager->createInstance($url_processor_name);
// @TODO this value we're passing on into
// \Drupal\facetapi\UrlProcessor\UrlProcessorInterface::addUriToResults is
// should be corrected.
$url_processor->addUriToResults($facet, '');
$url_processor->addUriToResults($facet);
}
}
}
......@@ -257,7 +257,8 @@ class Facet extends ConfigEntityBase implements FacetInterface {
public function getUrlProcessorName() {
return $this->url_processor_name;
// @Todo: for now if the url processor is not set, defualt to query_string.
return isset($this->url_processor_name) ? $this->url_processor_name : 'query_string';
}
public function getName() {
......
......@@ -12,6 +12,7 @@ use Drupal\facetapi\UrlProcessor\UrlProcessorPluginManager;
use Drupal\facetapi\Widget\WidgetPluginManager;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Query\ResultsCacheInterface;
use Drupal\search_api\Query\ResultSetInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -81,41 +82,24 @@ class SearchApiViewsAdapter extends AdapterPluginBase {
// Store all information in $this->facets.
$results = $this->searchResultsCache->getResults($this->searcher_id);
$facet_results = $results->getExtraData('search_api_facets');
foreach ($this->facets as $facet) {
$configuration = array(
'query' => NULL,
'facet' => $facet,
'results' => $facet_results[$facet->getName()],
);
$query_type_plugin = $this->query_type_plugin_manager->createInstance($facet->getQueryType(),
$configuration
);
$query_type_plugin->build();
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.
}
}
/**
* {@inheritdoc}
*/
public function setResults(FacetInterface $facet) {
$results = $this->searchResultsCache->getResults($this->searcher_id);
$facet_results = $results->getExtraData('search_api_facets');
$configuration = array(
'query' => NULL,
'facet' => $facet,
'results' => $facet_results[$facet->getFieldIdentifier()],
);
$query_type_plugin = $this->query_type_plugin_manager->createInstance($facet->getQueryType(),
$configuration
);
return $query_type_plugin->build();
}
}
......@@ -35,9 +35,9 @@ class QueryTypeTerm extends QueryTypePluginBase {
if (! empty($query)) {
$options = &$query->getOptions();
$field_name = $this->facet->getName();
$options['search_api_facets'][$field_name] = array(
'field' => $field_name,
$field_identifier = $this->facet->getFieldIdentifier();
$options['search_api_facets'][$field_identifier] = array(
'field' => $field_identifier,
'limit' => 50,
'operator' => 'and',
'min_count' => 0,
......
......@@ -41,7 +41,7 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{
}
public function addUriToResults(FacetInterface $facet, $value) {
public function addUriToResults(FacetInterface $facet) {
// Create links for all the values.
// First get the current list of get paramaters.
$get_params = $this->request->query;
......
......@@ -42,7 +42,7 @@ class LinksWidget implements WidgetInterface {
$text = '(-) ' . $text;
}
$link_generator = \Drupal::linkGenerator();
$link = $text;//$link_generator->generate($text, $result->getUrl());
$link = $link_generator->generate($text, $result->getUrl());
$items[] = $link;
}
}
......
......@@ -21,16 +21,15 @@ use Drupal\facetapi\FacetInterface;
interface UrlProcessorInterface {
/**
* Get the uri for a facet for a value.
* Add the uri's to all results of the facets.
*
* The facet knows which values are active or not.
*
* @param FacetInterface $facet
* @param $value
*
* @return mixed
*/
public function addUriToResults(FacetInterface $facet, $value);
public function addUriToResults(FacetInterface $facet);
/**
* Returns the filter key.
......
......@@ -19,7 +19,7 @@ abstract class UrlProcessorPluginBase extends PluginBase implements UrlProcessor
/** @var Request */
protected $request;
abstract public function addUriToResults(FacetInterface $facet, $value);
abstract public function addUriToResults(FacetInterface $facet);
public function getFilterKey() {
return $this->filter_key;
......
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