Skip to content
name: 'Facet API'
name: 'Facets'
type: module
description: 'Faceted search interfaces that can be used on Search API searchers.'
core: 8.x
package: Search
configure: facets.overview
test_dependencies:
- search_api:search_api
- drupal:views
drupal.facets.index-active-formatters:
version: VERSION
js:
js/index-active-formatters.js: {}
dependencies:
- core/jquery
- core/drupal
- core/jquery.once
drupal.facets.admin_css:
version: VERSION
css:
......
facets.overview:
title: Facet API
title: Facets
description: 'Configure facets for Search API searchers.'
route_name: facets.overview
weight: 30
......
......@@ -7,12 +7,10 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\search_api\Query\QueryInterface;
use Drupal\facets\FacetInterface;
/**
* Implements hook_help().
*/
function facets_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the facets module.
......
'administer facets':
title: 'Administer Facet Api'
title: 'Administer Facets'
description: 'Create and configure Facets for your Search pages.'
facets_processor:
label: Facets processor
plugin_manager_service_id: plugin.manager.facets.processor
plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator
facets_url_processor:
label: Facets URL processor
plugin_manager_service_id: plugin.manager.facets.url_processor
plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator
facets_facet_source:
label: Facets source
plugin_manager_service_id: plugin.manager.facets.facet_source
plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator
facets_widget:
label: Facets widget
plugin_manager_service_id: plugin.manager.facets.widget
plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator
facets_query_type:
label: Facets query type
plugin_manager_service_id: plugin.manager.facets.query_type
plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator
facets.overview:
path: '/admin/config/search/facet-api'
path: '/admin/config/search/facets'
defaults:
_title: 'Facet API'
_title: 'Facets'
_entity_list: 'facets_facet'
requirements:
_entity_create_access: 'facets_facet'
entity.facets_facet.add_form:
path: '/admin/config/search/facet-api/add-facet'
path: '/admin/config/search/facets/add-facet'
defaults:
_entity_form: 'facets_facet.default'
requirements:
_entity_create_access: 'facets_facet'
entity.facets_facet.canonical:
path: '/admin/config/search/facet-api/{facets_facet}'
path: '/admin/config/search/facets/{facets_facet}'
defaults:
_controller: '\Drupal\facets\Controller\FacetController::page'
_title_callback: '\Drupal\facets\Controller\FacetController::pageTitle'
......@@ -22,22 +22,30 @@ entity.facets_facet.canonical:
_entity_access: 'facets_facet.view'
entity.facets_facet.edit_form:
path: '/admin/config/search/facet-api/{facets_facet}/edit'
path: '/admin/config/search/facets/{facets_facet}/edit'
defaults:
_entity_form: 'facets_facet.edit'
requirements:
_entity_access: 'facets_facet.edit'
entity.facets_facet.delete_form:
path: '/admin/config/search/facet-api/{facets_facet}/delete'
path: '/admin/config/search/facets/{facets_facet}/delete'
defaults:
_entity_form: 'facets_facet.delete'
requirements:
_entity_access: 'facets_facet.delete'
entity.facets_facet.display_form:
path: '/admin/config/search/facet-api/{facets_facet}/display'
path: '/admin/config/search/facets/{facets_facet}/display'
defaults:
_entity_form: 'facets_facet.display'
requirements:
_entity_access: 'facets_facet.edit'
entity.facets_facet_source.edit_form:
path: '/admin/config/search/facets/facet-sources/{source_id}/edit'
defaults:
_controller: '\Drupal\facets\Controller\FacetSourceController::facetSourceConfigForm'
_title: 'Edit facet source configuration'
requirements:
_entity_create_access: 'facets_facet'
......@@ -11,6 +11,9 @@ services:
plugin.manager.facets.processor:
class: Drupal\facets\Processor\ProcessorPluginManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@string_translation']
plugin.manager.facets.url_processor:
class: Drupal\facets\UrlProcessor\UrlProcessorPluginManager
parent: default_plugin_manager
facets.manager:
class: Drupal\facets\FacetManager\DefaultFacetManager
arguments:
......@@ -19,9 +22,3 @@ services:
- '@plugin.manager.facets.facet_source'
- '@plugin.manager.facets.processor'
- '@entity_type.manager'
facets.facet_context:
class: Drupal\facets\ContextProvider\FacetContextProvider
arguments: ['@entity_type.manager']
tags:
- { name: context_provider }
/**
* @file
* Attaches show/hide functionality to checkboxes in the "Processor" tab.
*/
(function ($) {
"use strict";
Drupal.behaviors.facetsIndexFormatter = {
attach: function (context, settings) {
$('.search-api-status-wrapper input.form-checkbox', context).each(function () {
var $checkbox = $(this);
var processor_id = $checkbox.data('id');
var $rows = $('.search-api-processor-weight--' + processor_id, context);
var tab = $('.search-api-processor-settings-' + processor_id, context).data('verticalTab');
// Bind a click handler to this checkbox to conditionally show and hide
// the processor's table row and vertical tab pane.
$checkbox.on('click.searchApiUpdate', function () {
if ($checkbox.is(':checked')) {
$rows.show();
if (tab) {
tab.tabShow().updateSummary();
}
}
else {
$rows.hide();
if (tab) {
tab.tabHide().updateSummary();
}
}
});
// Attach summary for configurable items (only for screen-readers).
if (tab) {
tab.details.drupalSetSummary(function () {
return $checkbox.is(':checked') ? Drupal.t('Enabled') : Drupal.t('Disabled');
});
}
// Trigger our bound click handler to update elements to initial state.
$checkbox.triggerHandler('click.searchApiUpdate');
});
}
};
})(jQuery);
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\facets\Annotation\FacetsFacet.
* Contains \Drupal\facets\Annotation\FacetsFacetSource.
*/
namespace Drupal\facets\Annotation;
......@@ -10,10 +10,10 @@ namespace Drupal\facets\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API facet source annotation.
* Defines a Facets facet source annotation.
*
* @see \Drupal\facets\FacetSource\FacetSourcePluginManager
* @see \Drupal\facets\FacetSource\FacetSourceInterface
* @see \Drupal\facets\FacetSource\FacetSourcePluginInterface
* @see \Drupal\facets\FacetSource\FacetSourcePluginBase
* @see plugin_api
*
......@@ -29,7 +29,7 @@ class FacetsFacetSource extends Plugin {
public $id;
/**
* The human-readable name of the facet soruce plugin.
* The human-readable name of the facet source plugin.
*
* @ingroup plugin_translatable
*
......
......@@ -9,7 +9,7 @@ namespace Drupal\facets\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API Processor annotation.
* Defines a Facets Processor annotation.
*
* @see \Drupal\facets\Processor\ProcessorPluginManager
* @see plugin_api
......
......@@ -10,7 +10,7 @@ namespace Drupal\facets\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API query type annotation.
* Defines a Facets query type annotation.
*
* @see \Drupal\facets\QueryType\QueryTypePluginManager
* @see plugin_api
......
<?php
/**
* @file
* Contains \Drupal\facets\Annotation\FacetsUrlProcessor.
*/
namespace Drupal\facets\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facets URL Processor annotation.
*
* @see \Drupal\facets\Processor\ProcessorPluginManager
* @see plugin_api
*
* @ingroup plugin_api
*
* @Annotation
*/
class FacetsUrlProcessor extends Plugin {
/**
* The URL processor plugin id.
*
* @var string
*/
public $id;
/**
* The human-readable name of the URL processor plugin.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $label;
/**
* The URL processor description.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $description;
}
......@@ -10,7 +10,7 @@ namespace Drupal\facets\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API Widget annotation.
* Defines a Facets Widget annotation.
*
* @see \Drupal\facets\Widget\WidgetPluginManager
* @see plugin_api
......
<?php
/**
* @file
* Contains \Drupal\facets\ContextProvider\FacetContextProvider.
*/
namespace Drupal\facets\ContextProvider;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* A provider for the core context system for facets.
*
* This provider is a provider for core's context system, it makes integration
* with blocks, panels and other layout systems easy.
*/
class FacetContextProvider implements ContextProviderInterface {
use StringTranslationTrait;
protected $facetStorage;
/**
* Create a new instance of the context provider.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->facetStorage = $entity_type_manager->getStorage('facets_facet');
}
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids = []) {
$ids = $this->facetStorage->getQuery()
->condition('uuid', $unqualified_context_ids, 'IN')
->execute();
$contexts = [];
foreach ($this->facetStorage->loadMultiple($ids) as $facet) {
$context = new Context(new ContextDefinition('entity:facets_facet'), $facet);
$contexts[$facet->uuid()] = $context;
}
return $contexts;
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
$facets = $this->facetStorage->loadMultiple();
$contexts = [];
/** @var \Drupal\facets\FacetInterface $facet */
foreach ($facets as $facet) {
$context = new Context(
new ContextDefinition('entity:facets_facet', $facet->label()),
$facet
);
$contexts[$facet->uuid()] = $context;
}
return $contexts;
}
}
......@@ -51,7 +51,7 @@ class FacetController extends ControllerBase {
* Returns a form to edit a facet on a search api index.
*
* @param \Drupal\facets\FacetInterface $facets_facet
* Facet currently being edited
* Facet currently being edited.
*
* @return array
* The facet edit form.
......@@ -64,7 +64,7 @@ class FacetController extends ControllerBase {
/**
* Returns the page title for an facets's "View" tab.
*
* @param \Drupal\facets/FacetInterface $facet
* @param \Drupal\facets\FacetInterface $facet
* The facet that is displayed.
*
* @return string
......
<?php
/**
* @file
* Contains \Drupal\facets\Controller\FacetSourceController.
*/
namespace Drupal\facets\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Provides route responses for facet source configuration.
*/
class FacetSourceController extends ControllerBase {
/**
* Configuration for the facet source.
*
* @param string $source_id
* The plugin id.
*
* @return array
* A renderable array containing the form.
*/
public function facetSourceConfigForm($source_id) {
// Returns the render array of the FacetSourceConfigForm.
return $this->formBuilder()->getForm('\Drupal\facets\Form\FacetSourceEditForm');
}
}
......@@ -11,7 +11,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\facets\FacetInterface;
/**
* Defines the search index configuration entity.
* Defines the facet configuration entity.
*
* @ConfigEntityType(
* id = "facets_facet",
......@@ -37,48 +37,60 @@ use Drupal\facets\FacetInterface;
* config_export = {
* "id",
* "name",
* "url_alias",
* "field_identifier",
* "query_type_name",
* "facet_source_id",
* "widget",
* "widget_configs",
* "options",
* "query_operator",
* "exclude",
* "only_visible_when_facet_source_is_visible",
* "processor_configs",
* "empty_behavior",
* "facet_configs",
* },
* links = {
* "canonical" = "/admin/config/search/facet-api",
* "add-form" = "/admin/config/search/facet-api/add-facet",
* "edit-form" = "/admin/config/search/facet-api/{facets_facet}/edit",
* "display-form" = "/admin/config/search/facet-api/{facets_facet}/display",
* "delete-form" = "/admin/config/search/facet-api/{facets_facet}/delete",
* "canonical" = "/admin/config/search/facets",
* "add-form" = "/admin/config/search/facets/add-facet",
* "edit-form" = "/admin/config/search/facets/{facets_facet}/edit",
* "display-form" = "/admin/config/search/facets/{facets_facet}/display",
* "delete-form" = "/admin/config/search/facets/{facets_facet}/delete",
* }
* )
*/
class Facet extends ConfigEntityBase implements FacetInterface {
/**
* The ID of the index.
* The ID of the facet.
*
* @var string
*/
protected $id;
/**
* A name to be displayed for the index.
* A name to be displayed for the facet.
*
* @var string
*/
protected $name;
/**
* A string describing the index.
* The name for the parameter when used in the URL.
*
* @var string
*/
protected $url_alias;
/**
* A string describing the facet.
*
* @var string
*/
protected $description;
/**
* A string describing the widget.
* The plugin name of the widget.
*
* @var string
*/
......@@ -87,18 +99,23 @@ class Facet extends ConfigEntityBase implements FacetInterface {
/**
* Configuration for the widget. This is a key-value stored array.
*
* @var string
* @var array
*/
protected $widget_configs;
protected $widget_configs = [];
/**
* An array of options configuring this index.
* The operator to hand over to the query, currently AND | OR.
*
* @var array
* @var string
*/
protected $query_operator;
/**
* A boolean flag indicating if search should exclude selected facets.
*
* @see getOptions()
* @var bool
*/
protected $options = array();
protected $exclude;
/**
* The field identifier.
......@@ -131,7 +148,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
/**
* The facet source belonging to this facet.
*
* @var \Drupal\facets\FacetSourceInterface
* @var \Drupal\facets\FacetSource\FacetSourcePluginInterface
*
* @see getFacetSource()
*/
......@@ -151,6 +168,18 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $results = [];
/**
* The results.
*
* @var \Drupal\facets\Result\ResultInterface[]
*/
protected $unfiltered_results = [];
/**
* An array of active values.
*
* @var string[]
*/
protected $active_values = [];
/**
......@@ -170,6 +199,22 @@ class Facet extends ConfigEntityBase implements FacetInterface {
protected $processors;
/**
* Configuration for the processors. This is an array of arrays.
*
* @var array
*/
protected $processor_configs = [];
/**
* Additional facet configurations.
*
* @var array
*/
protected $facet_configs = [];
/**
* Is the facet only visible when the facet source is only visible.
*
* A boolean that defines whether or not the facet is only visible when the
* facet source is visible.
*
......@@ -177,6 +222,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $only_visible_when_facet_source_is_visible;
/**
* The no-result configuration.
*
* @var string[];
*/
protected $empty_behavior;
/**
* The widget plugin manager.
*
......@@ -184,6 +236,14 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $widget_plugin_manager;
/**
* The facet source config object.
*
* @var \Drupal\Facets\FacetSourceInterface
* The facet source config object.
*/
protected $facetSourceConfig;
/**
* {@inheritdoc}
*/
......@@ -192,19 +252,23 @@ class Facet extends ConfigEntityBase implements FacetInterface {
}
/**
* Returns the widget plugin manager.
*
* @return \Drupal\facets\Widget\WidgetPluginManager
* The widget plugin manager.
*/
public function getWidgetManager() {
$container = \Drupal::getContainer();
return $this->widget_plugin_manager ? : $container->get('plugin.manager.facets.widget');
return $this->widget_plugin_manager ?: $container->get('plugin.manager.facets.widget');
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->id;
protected function urlRouteParameters($rel) {
$parameters = parent::urlRouteParameters($rel);
return $parameters;
}
/**
......@@ -222,6 +286,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this;
}
/**
* {@inheritdoc}
*/
public function getQueryTypes() {
return $this->query_type_name;
}
/**
* {@inheritdoc}
*/
......@@ -229,6 +300,48 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->widget;
}
/**
* Retrieves all processors supported by this facet.
*
* @return \Drupal\facets\Processor\ProcessorInterface[]
* The loaded processors, keyed by processor ID.
*/
protected function loadProcessors() {
if (!isset($this->processors)) {
/* @var $processor_plugin_manager \Drupal\facets\Processor\ProcessorPluginManager */
$processor_plugin_manager = \Drupal::service('plugin.manager.facets.processor');
$processor_settings = $this->getProcessorConfigs();
foreach ($processor_plugin_manager->getDefinitions() as $name => $processor_definition) {
if (class_exists($processor_definition['class']) && empty($this->processors[$name])) {
// Create our settings for this processor.
$settings = empty($processor_settings[$name]['settings']) ? [] : $processor_settings[$name]['settings'];
$settings['facet'] = $this;
/* @var $processor \Drupal\facets\Processor\ProcessorInterface */
$processor = $processor_plugin_manager->createInstance($name, $settings);
$this->processors[$name] = $processor;
}
elseif (!class_exists($processor_definition['class'])) {
\Drupal::logger('facets')
->warning('Processor @id specifies a non-existing @class.', array(
'@id' => $name,
'@class' => $processor_definition['class'],
));
}
}
}
return $this->processors;
}
/**
* {@inheritdoc}
*/
public function getProcessorConfigs() {
return !empty($this->processor_configs) ? $this->processor_configs : [];
}
/**
* {@inheritdoc}
*/
......@@ -245,64 +358,56 @@ class Facet extends ConfigEntityBase implements FacetInterface {
}
/**
* Get the field alias used to identify the facet in the url.
*
* @return mixed
* {@inheritdoc}
*/
public function getFieldAlias() {
// For now, create the field alias based on the field identifier.
$field_alias = preg_replace('/[:\/]+/', '_', $this->field_identifier);
return $field_alias;
public function setQueryOperator($operator = '') {
return $this->query_operator = $operator;
}
/**
* Sets an item with value to active.
*
* @param $value
* {@inheritdoc}
*/
public function setActiveItem($value) {
if (!in_array($value, $this->active_values)) {
$this->active_values[] = $value;
}
public function getQueryOperator() {
return $this->query_operator ?: 'OR';
}
/**
* Get all the active items in the facet.
*
* @return mixed
* {@inheritdoc}
*/
public function getActiveItems() {
return $this->active_values;
public function setExclude($exclude) {
return $this->exclude = $exclude;
}
/**
* {@inheritdoc}
*/
public function getOption($name, $default = NULL) {
return isset($this->options[$name]) ? $this->options[$name] : $default;
public function getExclude() {
return $this->exclude;
}
/**
* {@inheritdoc}
*/
public function getOptions() {
return $this->options;
public function getFieldAlias() {
// For now, create the field alias based on the field identifier.
$field_alias = preg_replace('/[:\/]+/', '_', $this->field_identifier);
return $field_alias;
}
/**
* {@inheritdoc}
*/
public function setOption($name, $option) {
$this->options[$name] = $option;
return $this;
public function setActiveItem($value) {
if (!in_array($value, $this->active_values)) {
$this->active_values[] = $value;
}
}
/**
* {@inheritdoc}
*/
public function setOptions(array $options) {
$this->options = $options;
return $this;
public function getActiveItems() {
return $this->active_values;
}
/**
......@@ -320,13 +425,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this;
}
/**
* {@inheritdoc}
*/
public function getQueryTypes() {
return $this->query_type_name;
}
/**
* {@inheritdoc}
*/
......@@ -342,6 +440,20 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->name;
}
/**
* {@inheritdoc}
*/
public function getUrlAlias() {
return $this->url_alias;
}
/**
* {@inheritdoc}
*/
public function setUrlAlias($url_alias) {
$this->url_alias = $url_alias;
}
/**
* {@inheritdoc}
*/
......@@ -356,9 +468,9 @@ class Facet extends ConfigEntityBase implements FacetInterface {
public function getFacetSource() {
if (!$this->facet_source_instance && $this->facet_source_id) {
/** @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
/* @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
$this->facet_source_instance = $facet_source_plugin_manager->createInstance($this->facet_source_id);
$this->facet_source_instance = $facet_source_plugin_manager->createInstance($this->facet_source_id, ['facet' => $this]);
}
return $this->facet_source_instance;
......@@ -372,42 +484,37 @@ class Facet extends ConfigEntityBase implements FacetInterface {
}
/**
* Retrieves all processors supported by this facet.
*
* @return \Drupal\facets\Processor\ProcessorInterface[]
* The loaded processors, keyed by processor ID.
* {@inheritdoc}
*/
protected function loadProcessors() {
if (!isset($this->processors)) {
/** @var $processor_plugin_manager \Drupal\facets\Processor\ProcessorPluginManager */
$processor_plugin_manager = \Drupal::service('plugin.manager.facets.processor');
$processor_settings = $this->getOption('processors', []);
public function getFacetSourceConfig() {
// Return the facet source config object, if it's already set on the facet.
if ($this->facetSourceConfig instanceof FacetSource) {
return $this->facetSourceConfig;
}
foreach ($processor_plugin_manager->getDefinitions() as $name => $processor_definition) {
if (class_exists($processor_definition['class']) && empty($this->processors[$name])) {
// Create our settings for this processor.
$settings = empty($processor_settings[$name]['settings']) ? [] : $processor_settings[$name]['settings'];
$settings['facet'] = $this;
$storage = \Drupal::entityTypeManager()->getStorage('facets_facet_source');
$source_id = str_replace(':', '__', $this->facet_source_id);
/** @var $processor \Drupal\facets\Processor\ProcessorInterface */
$processor = $processor_plugin_manager->createInstance($name, $settings);
$this->processors[$name] = $processor;
}
elseif (!class_exists($processor_definition['class'])) {
\Drupal::logger('facets')->warning('Processor @id specifies a non-existing @class.', array('@id' => $name, '@class' => $processor_definition['class']));
}
}
// Load and return the facet source config object from the storage.
$facet_source = $storage->load($source_id);
if ($facet_source instanceof FacetSource) {
$this->facetSourceConfig = $facet_source;
return $this->facetSourceConfig;
}
return $this->processors;
}
// We didn't have a facet source config entity yet for this facet source
// plugin, so we create it on the fly.
$facet_source = new FacetSource(
[
'id' => $source_id,
'name' => $this->facet_source_id,
],
'facets_facet_source'
);
$facet_source->save();
/**
* {@inheritdoc}
*/
protected function urlRouteParameters($rel) {
$parameters = parent::urlRouteParameters($rel);
return $parameters;
$this->facetSourceConfig = $facet_source;
return $this->facetSourceConfig;
}
/**
......@@ -418,10 +525,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
}
/**
* Set an array of Result objects.
*
* @param array $results
* Array containing \Drupal\facets\Result\Result objects.
* {@inheritdoc}
*/
public function setResults(array $results) {
$this->results = $results;
......@@ -437,16 +541,21 @@ class Facet extends ConfigEntityBase implements FacetInterface {
}
/**
* Until facet api supports more than just search api, this is enough.
*
* @return string
* {@inheritdoc}
*/
public function getManagerPluginId() {
return 'facets_default';
public function setUnfilteredResults(array $all_results = []) {
$this->unfiltered_results = $all_results;
}
/**
* @inheritdoc
* {@inheritdoc}
*/
public function getUnfilteredResults() {
return $this->unfiltered_results;
}
/**
* {@inheritdoc}
*/
public function isActiveValue($value) {
$is_active = FALSE;
......@@ -463,7 +572,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
if (!isset($this->facetSourcePlugins)) {
$this->facetSourcePlugins = [];
/** @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
/* @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
foreach ($facet_source_plugin_manager->getDefinitions() as $name => $facet_source_definition) {
......@@ -471,12 +580,16 @@ class Facet extends ConfigEntityBase implements FacetInterface {
// Create our settings for this facet source..
$config = isset($this->facetSourcePlugins[$name]) ? $this->facetSourcePlugins[$name] : [];
/** @var $facet_source \Drupal\facets\FacetSource\FacetSourceInterface */
/* @var $facet_source \Drupal\facets\FacetSource\FacetSourcePluginInterface */
$facet_source = $facet_source_plugin_manager->createInstance($name, $config);
$this->facetSourcePlugins[$name] = $facet_source;
}
elseif (!class_exists($facet_source_definition['class'])) {
\Drupal::logger('facets')->warning('Facet Source @id specifies a non-existing @class.', ['@id' => $name, '@class' => $facet_source_definition['class']]);
\Drupal::logger('facets')
->warning('Facet Source @id specifies a non-existing @class.', [
'@id' => $name,
'@class' => $facet_source_definition['class'],
]);
}
}
}
......@@ -496,9 +609,9 @@ class Facet extends ConfigEntityBase implements FacetInterface {
$processors = $this->loadProcessors();
// Filter processors by status if required. Enabled processors are those
// which have settings in the "processors" option.
// which have settings in the processor_configs.
if ($only_enabled) {
$processors_settings = $this->getOption('processors', array());
$processors_settings = $this->getProcessorConfigs();
$processors = array_intersect_key($processors, $processors_settings);
}
......@@ -510,7 +623,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
public function getProcessorsByStage($stage, $only_enabled = TRUE) {
$processors = $this->loadProcessors();
$processor_settings = $this->getOption('processors', array());
$processor_settings = $this->getProcessorConfigs();
$processor_weights = array();
// Get a list of all processors meeting the criteria (stage and, optionally,
......@@ -551,4 +664,66 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->only_visible_when_facet_source_is_visible;
}
/**
* {@inheritdoc}
*/
public function addProcessor(array $processor) {
$this->processor_configs[$processor['processor_id']] = [
'processor_id' => $processor['processor_id'],
'weights' => $processor['weights'],
'settings' => $processor['settings'],
];
// Sort the processors so we won't have unnecessary changes.
ksort($this->processor_configs);
}
/**
* {@inheritdoc}
*/
public function removeProcessor($processor_id) {
unset($this->processor_configs[$processor_id]);
}
/**
* {@inheritdoc}
*/
public function getEmptyBehavior() {
return $this->empty_behavior;
}
/**
* {@inheritdoc}
*/
public function setEmptyBehavior(array $empty_behavior) {
$this->empty_behavior = $empty_behavior;
}
/**
* {@inheritdoc}
*/
public function setWidgetConfigs(array $widget_configs) {
$this->widget_configs = $widget_configs;
}
/**
* {@inheritdoc}
*/
public function getWidgetConfigs() {
return $this->widget_configs;
}
/**
* {@inheritdoc}
*/
public function setFacetConfigs(array $facet_configs) {
$this->facet_configs = $facet_configs;
}
/**
* {@inheritdoc}
*/
public function getFacetConfigs() {
return $this->facet_configs;
}
}
<?php
/**
* @file
* Contains \Drupal\facets\Entity\FacetSource.
*/
namespace Drupal\facets\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\facets\FacetSourceInterface;
/**
* Defines the facet source configuration entity.
*
* @ConfigEntityType(
* id = "facets_facet_source",
* label = @Translation("Facet source"),
* handlers = {
* "storage" = "Drupal\Core\Config\Entity\ConfigEntityStorage",
* "list_builder" = "Drupal\facets\FacetListBuilder",
* "form" = {
* "default" = "Drupal\facets\Form\FacetSourceEditForm",
* "edit" = "Drupal\facets\Form\FacetSourceEditForm",
* "display" = "Drupal\facets\Form\FacetSourceDisplayForm",
* "delete" = "Drupal\facets\Form\FacetSourceDeleteConfirmForm",
* },
* },
* admin_permission = "administer facets",
* config_prefix = "facet_source",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid"
* },
* config_export = {
* "id",
* "name",
* "filter_key",
* "url_processor"
* },
* links = {
* "canonical" = "/admin/config/search/facets/facet-sources/",
* "edit-form" = "/admin/config/search/facets/facet-sources/{facets_facet_source}/edit"
* }
* )
*/
class FacetSource extends ConfigEntityBase implements FacetSourceInterface {
/**
* The ID of the facet source.
*
* @var string
*/
protected $id;
/**
* A name to be displayed for the facet source.
*
* @var string
*/
protected $name;
/**
* The key, used for filters in the query string.
*
* @var string
*/
protected $filter_key;
/**
* The url processor name.
*
* @var string
*/
protected $url_processor = 'query_string';
/**
* {@inheritdoc}
*/
public function getName() {
return $this->name;
}
/**
* {@inheritdoc}
*/
public function setFilterKey($filter_key) {
$this->filter_key = $filter_key;
}
/**
* {@inheritdoc}
*/
public function getFilterKey() {
return $this->filter_key;
}
/**
* {@inheritdoc}
*/
public function setUrlProcessor($processor_name) {
$this->url_processor = $processor_name;
}
/**
* {@inheritdoc}
*/
public function getUrlProcessorName() {
return $this->url_processor;
}
}
......@@ -8,6 +8,6 @@
namespace Drupal\facets\Exception;
/**
* Represents an exception that occurred in some part of the Facet API.
* Represents an exception that occurred in some part of the Facets.
*/
class Exception extends \Exception {}