Commit 13e09a28 authored by Jur de Vries's avatar Jur de Vries
Browse files

First work on the url processor.

parent d4144d5d
......@@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Query\QueryInterface;
use Drupal\facetapi\Entity\Facet;
use Drupal\facetapi\FacetInterface;
use Drupal\Component\Plugin\PluginBase;
/**
......@@ -44,12 +45,12 @@ function facetapi_theme()
/**
* Get all facet definitions.
*
* @return mixed
* @return FacetInterface[]
*/
function facetapi_get_enabled_facets() {
$module_handler = \Drupal::service('module_handler');
$facet_definitions = $module_handler->invokeAll('facetapi_facet_info');
return $facet_definitions;
$facets = $module_handler->invokeAll('facetapi_facet_info');
return $facets;
}
/**
......@@ -117,9 +118,6 @@ function facetapi_facetapi_facet_info() {
// @TODO: This should be a function in search api. Look it up.
$searcher_name = 'search_api_views:' . $view_id . ':' . $display['id'];
foreach ($fields as $field_name => $field) {
if ( empty($facet_info[$searcher_name])) {
$facet_info[$searcher_name] = array();
}
// Determine the query type.
$term_types = array(
'string',
......@@ -139,13 +137,14 @@ function facetapi_facetapi_facet_info() {
'field_identifier' => $field->getFieldIdentifier(),
'query_type_name' => 'search_api_term',
'searcher_name' => $searcher_name,
'url_processor_name' => 'query_string',
);
$facet = Facet::create($properties);
$facet_info[$searcher_name][$field_name] = $facet;
$facet_info[$searcher_name . '::' . $field_name] = $facet;
}
}
......
......@@ -10,6 +10,7 @@ namespace Drupal\facetapi\Adapter;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\QueryType\QueryTypePluginManager;
use Drupal\facetapi\UrlProcessor\UrlProcessorInterface;
use Drupal\facetapi\UrlProcessor\UrlProcessorPluginManager;
......@@ -55,9 +56,9 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
protected $keys;
/**
* An array of FacetapiFacet objects for facets being rendered.
* An array of FacetInterface objects for facets being rendered.
*
* @var array
* @var FacetInterface[]
*
* @see FacetapiFacet
*/
......@@ -212,6 +213,11 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
$this->module_handler = $module_handler;
$this->query_type_plugin_manager = $query_type_plugin_manager;
$this->url_processor_plugin_manager = $url_processor_plugin_manager;
// Immediately initialize the facets.
// This can be done directly because the only thing needed is
// the url.
$this->initFacets();
}
/**
......@@ -300,11 +306,15 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
$facets = $this->getEnabledFacets();
// Get the searcher name from the query.
$search_id = $this->searcher_id;
foreach ($facets[$search_id] as $facet) {
// Create the query type plugin.
$query_type_plugin = $this->query_type_plugin_manager->createInstance($facet->getQueryType(), array('query' => $query, 'facet' => $facet));
// Let the query type alter the query.
$query_type_plugin->execute();
foreach ($facets as $facet) {
// Only if the facet is for this query, alter the query.
if ($facet->getSearcherName() == $search_id) {
// Create the query type plugin.
$query_type_plugin = $this->query_type_plugin_manager->createInstance($facet->getQueryType(),
array('query' => $query, 'facet' => $facet));
// Let the query type alter the query.
$query_type_plugin->execute();
}
}
}
......@@ -453,6 +463,25 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
}
/**
* Initialize enabled facets.
*
* In this method the url processor is used
* to check for each facet what the active items are.
*/
protected function initFacets() {
if (empty($this->facets)) {
$this->facets = $this->getEnabledFacets();
foreach ($this->facets as $facet) {
/** @var UrlProcessorInterface $url_processor */
$url_processor_name = $facet->getUrlProcessorName();
$url_processor = $this->url_processor_plugin_manager->createInstance($url_processor_name);
$url_processor->processFacet($facet);
}
}
}
public function build($facet) {
// Process the facets.
// @TODO: inject the searcher id on create of the adapter.
......
......@@ -87,7 +87,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*
* @var string
*/
protected $field_indentifier;
protected $field_identifier;
/**
* The query type name.
......@@ -103,6 +103,15 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $searcher_name;
/**
* The plugin name of the url processor.
*
* @var string
*/
protected $url_processor_name;
protected $active_values = array();
/**
* {@inheritdoc}
*/
......@@ -130,7 +139,9 @@ class Facet extends ConfigEntityBase implements FacetInterface {
* @return mixed
*/
public function getFieldAlias() {
return NULL;
// For now, create the field alias based on the field identifier.
$field_alias = preg_replace('/[:\/]+/', '_', $this->field_identifier);
return $field_alias;
}
/**
......@@ -139,7 +150,9 @@ class Facet extends ConfigEntityBase implements FacetInterface {
* @param $value
*/
public function setActiveItem($value) {
return NULL;
if (!in_array($value, $this->active_values)) {
$this->active_values[] = $value;
}
}
/**
......@@ -182,7 +195,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
}
public function getFieldIdentifier() {
return $this->field_indentifier;
return $this->field_identifier;
}
public function getQueryType() {
......@@ -193,6 +206,10 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->searcher_name;
}
public function getUrlProcessorName() {
return $this->url_processor_name;
}
public function getName() {
return $this->name;
}
......
......@@ -62,6 +62,13 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getSearcherName();
/**
* Get the plugin name for the url processor.
*
* @return mixed
*/
public function getUrlProcessorName();
/**
* Retrieves an option.
*
......
......@@ -7,8 +7,9 @@
namespace Drupal\facetapi\Plugin\facetapi\url_processor;
use Drupal\facetapi\Facet\FacetInterface;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\HttpFoundation\Request;
/**
* @FacetApiUrlProcessor(
......@@ -22,11 +23,57 @@ use Drupal\facetapi\UrlProcessor\UrlProcessorPluginBase;
*/
class UrlProcessorQueryString extends UrlProcessorPluginBase{
const SEPARATOR = ':';
protected $active_filters = array();
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Request $request
) {
parent::__construct($configuration, $plugin_id, $plugin_definition,
$request);
$this->initializeActiveFilters();
}
public function getUri(FacetInterface $facet, $value) {
// TODO: Implement getUri() method.
}
public function processFacet(FacetInterface $facet) {
// TODO: Implement processFacet() method.
// Get the filterkey of the facet.
$filter_key = $facet->getFieldAlias();
if (isset($this->active_filters[$filter_key])) {
foreach ($this->active_filters[$filter_key] as $value) {
$facet->setActiveItem($value);
}
}
}
/**
* Initialize the active filters.
*/
protected function initializeActiveFilters() {
$url_parameters = $this->request->query;
// Get the active facet parameters.
$active_params = $url_parameters->get($this->filter_key, array(), TRUE);
// Explode the active params on the separator.
foreach ($active_params as $param) {
list($key, $value) = explode(self::SEPARATOR, $param);
if (!isset($this->active_filters[$key])) {
$this->active_filters[$key] = array(
$value
);
}
else {
$this->active_filters[$key][] = $value;
}
}
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@
namespace Drupal\facetapi\UrlProcessor;
use Drupal\facetapi\Facet\FacetInterface;
use Drupal\facetapi\FacetInterface;
/**
* Interface UrlProcessorInterface
......
......@@ -6,12 +6,19 @@
namespace Drupal\facetapi\UrlProcessor;
use Drupal\facetapi\Facet\FacetInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facetapi\FacetInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
abstract class UrlProcessorPluginBase implements UrlProcessorInterface {
abstract class UrlProcessorPluginBase extends PluginBase implements UrlProcessorInterface, ContainerFactoryPluginInterface {
protected $filter_key = 'f';
/** @var Request */
protected $request;
abstract public function getUri(FacetInterface $facet, $value);
public function getFilterKey() {
......@@ -19,4 +26,28 @@ abstract class UrlProcessorPluginBase implements UrlProcessorInterface {
}
abstract public function processFacet(FacetInterface $facet);
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Request $request
) {
parent::__construct($configuration, $plugin_id,
$plugin_definition);
$this->request = $request;
}
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
// Add the request.
/** @var Request $request */
$request = $container->get('request_stack')->getCurrentRequest();
$plugin = new static($configuration, $plugin_id, $plugin_definition, $request);
return $plugin;
}
}
\ No newline at end of file
......@@ -13,6 +13,6 @@ use Drupal\Core\Plugin\DefaultPluginManager;
class UrlProcessorPluginManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/facetapi/UrlProcessor', $namespaces, $module_handler, 'Drupal\facetapi\UrlProcessor\UrlProcessorInterface', '\Drupal\facetapi\Annotation\FacetApiUrlProcessor');
parent::__construct('Plugin/facetapi/url_processor', $namespaces, $module_handler, 'Drupal\facetapi\UrlProcessor\UrlProcessorInterface', '\Drupal\facetapi\Annotation\FacetApiUrlProcessor');
}
}
\ No newline at end of file
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