Commit 84149c29 authored by Jur de Vries's avatar Jur de Vries
Browse files

Working url processor and link generation.

parent 75703caa
......@@ -524,8 +524,12 @@ abstract class AdapterPluginBase extends PluginBase implements AdapterInterface,
if ($result->getCount()) {
// Get the link.
$text = $result->getValue() . ' (' . $result->getCount() . ')';
// $link = $this->link_generator->generate($text, );
$items[] = $text;
if ($result->isActive()) {
$text = '(-) ' . $text;
}
$link_generator = \Drupal::linkGenerator();
$link = $link_generator->generate($text, $result->getUrl());
$items[] = $link;
}
}
$build = array(
......@@ -542,7 +546,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);
$url_processor->processFacet($facet);
$url_processor->addUriToResults($facet);
}
}
}
......@@ -247,6 +247,15 @@ class Facet extends ConfigEntityBase implements FacetInterface {
public function setResults(array $results) {
$this->results = $results;
// If there are active values,
// set the results which are active to active.
if (count($this->active_values)) {
foreach ($this->results as $result) {
if (in_array($result->getValue(), $this->active_values)) {
$result->setActiveState(TRUE);
}
}
}
}
/**
......@@ -257,4 +266,15 @@ class Facet extends ConfigEntityBase implements FacetInterface {
public function getAdapterPluginId() {
return 'search_api_views';
}
/**
* @inheritdoc
*/
public function isActiveValue($value) {
$is_active = FALSE;
if (in_array($value, $this->active_values)) {
$is_active = TRUE;
}
return $is_active;
}
}
......@@ -48,10 +48,18 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getActiveItems();
/**
* Check if a value is active.
*
* @param string $value
* @return bool
*/
public function isActiveValue($value);
/**
* Get the result for the facet.
*
* @return mixed
* @return ResultInterface[] $results
*/
public function getResults();
......
......@@ -25,6 +25,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* "facet" = @ContextDefinition("entity:facetapi_facet", label=@Translation("Facet"))
* }
* )
*
*/
class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface {
......@@ -81,9 +82,11 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface {
// The plugin manager should be injected.
/** @var Facet $facet */
$facet = $this->getContextValue('facet');
// $facet = $this->getContextValue('facet');
$plugin_id = 'search_api_views';
$facets = facetapi_get_enabled_facets();
$facet = $facets[$this->configuration['facet_identifier']];
/** @var \Drupal\facetapi\Adapter\AdapterInterface $adapter */
$adapter = $this->pluginManager->getMyOwnChangeLaterInstance(
......
......@@ -7,6 +7,7 @@
namespace Drupal\facetapi\Plugin\facetapi\url_processor;
use Drupal\Core\Url;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\UrlProcessor\UrlProcessorPluginBase;
use Symfony\Component\HttpFoundation\Request;
......@@ -40,15 +41,44 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{
}
public function getUri(FacetInterface $facet, $value) {
// TODO: Implement getUri() method.
public function addUriToResults(FacetInterface $facet, $value) {
// Create links for all the values.
// First get the current list of get paramaters.
$get_params = $this->request->query;
$results = $facet->getResults();
foreach ($results as $result) {
$filter_string = $facet->getFieldAlias() . ':' . $result->getValue();
$result_get_params = clone $get_params;
$filter_params = $result_get_params->get($this->filter_key, [], TRUE);
// If the value is active, remove the filter string from the
// parameters.
if ($result->isActive()) {
foreach ($filter_params as $key => $filter_param) {
if ($filter_param == $filter_string) {
unset($filter_params[$key]);
}
}
}
// If the value is not active, add the filter string.
else {
$filter_params[] = $filter_string;
}
$result_get_params->set($this->filter_key, $filter_params);
$url = Url::createFromRequest($this->request);
$url->setOption('query', $result_get_params->all());
$result->setUrl($url);
}
}
public function processFacet(FacetInterface $facet) {
// 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) {
if (isset($this->active_filters[$facet->getFieldAlias()])) {
foreach ($this->active_filters[$facet->getFieldAlias()] as $value) {
$facet->setActiveItem($value);
}
}
......@@ -56,6 +86,12 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{
/**
* Initialize the active filters.
*
* Get all the filters that are active.
* This method only get's all the filters,
* but doesn't assign them to facets.
* In the processFacet method the active values
* for a specific facet are added to the facet.
*/
protected function initializeActiveFilters() {
$url_parameters = $this->request->query;
......
......@@ -31,6 +31,13 @@ class Result implements ResultInterface{
*/
protected $url;
/**
* Wether the value is active (selected) or not.
*
* @var bool
*/
protected $active = FALSE;
function __construct($value, $count) {
$this->value = $value;
$this->count = $count;
......@@ -71,4 +78,21 @@ class Result implements ResultInterface{
public function setUrl(Url $url) {
$this->url = $url;
}
/**
* @inheritdoc
*/
public function setActiveState($active) {
$this->active = $active;
}
/**
* @inheritdoc
*/
public function isActive() {
return $this->active;
}
}
\ No newline at end of file
......@@ -39,4 +39,18 @@ interface ResultInterface {
*/
public function setUrl(Url $url);
/**
* Indicate that the value is active (selected).
*
* @param bool $active
*/
public function setActiveState($active);
/**
* Returns true if the value is active (selected).
*
* @return bool $active
*/
public function isActive();
}
\ No newline at end of file
......@@ -30,7 +30,7 @@ interface UrlProcessorInterface {
*
* @return mixed
*/
public function getUri(FacetInterface $facet, $value);
public function addUriToResults(FacetInterface $facet, $value);
/**
* Returns the filter key.
......
......@@ -19,7 +19,7 @@ abstract class UrlProcessorPluginBase extends PluginBase implements UrlProcessor
/** @var Request */
protected $request;
abstract public function getUri(FacetInterface $facet, $value);
abstract public function addUriToResults(FacetInterface $facet, $value);
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