Skip to content
<?php
/**
* @file
* Contains \Drupal\facets\Exception\InvalidQueryTypeException.
*/
namespace Drupal\facets\Exception;
/**
* Represents an exception that occurred when calling an invalid query type.
*/
class InvalidQueryTypeException extends \Exception {}
<?php
/**
* @file
* Contains Drupal\facetapi\FacetInterface
* Contains Drupal\facets\FacetInterface.
*/
namespace Drupal\facetapi;
namespace Drupal\facets;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\facetapi\FacetSource\FacetSourceInterface;
use Drupal\facetapi\Result\ResultInterface;
/**
* The facet entity.
*/
interface FacetInterface extends ConfigEntityInterface {
/**
* Sets the facet's widget plugin id.
*
* @param string $widget
* @param string $widget
* The widget plugin id.
*
* @return $this
* Returns self
*/
public function setWidget($widget);
......@@ -24,157 +28,161 @@ interface FacetInterface extends ConfigEntityInterface {
* Returns the facet's widget plugin id.
*
* @return string
* The widget plugin id.
*/
public function getWidget();
/**
* Get field identifier.
* Returns field identifier.
*
* @return string
* The field identifier of this facet.
*/
public function getFieldIdentifier();
/**
* Sets the empty_behavior id.
* Sets field identifier.
*
* @param $behavior_id
* The id for the empty behavior.
* @param string $field_identifier
* The field identifier of this facet.
*
* @return mixed
* @return $this
* Returns self.
*/
public function setFieldEmptyBehavior($behavior_id);
public function setFieldIdentifier($field_identifier);
/**
* Get field empty_behavior.
* Returns the field alias used to identify the facet in the url.
*
* @return mixed
* @return string
* The field alias for the facet.
*/
public function getFieldEmptyBehavior();
public function getFieldAlias();
/**
* Set field identifier.
/**
* Returns the field name of the facet as used in the index.
*
* @return mixed
* @TODO: Check if fieldIdentifier can be used as well!
*
* @return string
* The name of the facet.
*/
public function setFieldIdentifier($field_identifier);
public function getName();
/**
* Get the field alias used to identify the facet in the url.
* Returns the name of the facet for use in the URL.
*
* @return mixed
* @return string
* The name of the facet for use in the URL.
*/
public function getFieldAlias();
public function getUrlAlias();
/**
* Get the field name of the facet as used in the index.
*
* @TODO: Check if fieldIdentifier can be used as well!
* Sets the name of the facet for use in the URL.
*
* @return mixed
* @param string $url_alias
* The name of the facet for use in the URL.
*/
public function getName();
public function setUrlAlias($url_alias);
/**
* Sets an item with value to active.
*
* @param $value
* @param string $value
* An item that is active.
*/
public function setActiveItem($value);
/**
* Get all the active items in the facet.
* Returns all the active items in the facet.
*
* @return mixed
* An array containing all active items.
*/
public function getActiveItems();
/**
* Check if a value is active.
* Checks if a value is active.
*
* @param string $value
* The value to be checked.
*
* @return bool
* Is an active value.
*/
public function isActiveValue($value);
/**
* Get the result for the facet.
* Returns the result for the facet.
*
* @return ResultInterface[] $results
* @return \Drupal\facets\Result\ResultInterface[] $results
* The results of the facet.
*/
public function getResults();
/**
* Sets the reuslts for the facet.
* Sets the results for the facet.
*
* @param ResultInterface[] $results
* @param \Drupal\facets\Result\ResultInterface[] $results
* The results of the facet.
*/
public function setResults(array $results);
/**
* Get the query type plugin name.
* Sets an array of unfiltered results.
*
* @return mixed
*/
public function getQueryType();
/**
* Get the plugin name for the url processor.
* These unfiltered results are used to set the correct count of the actual
* facet results when using the OR query operator. They are not results value
* objects like those in ::$results.
*
* @return mixed
* @param array $all_results
* Unfiltered results.
*/
public function getUrlProcessorName();
public function setUnfilteredResults(array $all_results = []);
/**
* Retrieves an option.
*
* @param string $name
* The name of an option.
* @param mixed $default
* The value return if the option wasn't set.
* Returns an array of unfiltered results.
*
* @return mixed
* The value of the option.
*
* @see getOptions()
* @return array
* Unfiltered results.
*/
public function getOption($name, $default = NULL);
public function getUnfilteredResults();
/**
* Retrieves an array of all options.
* Returns the query type instance.
*
* @return array
* An associative array of option values, keyed by the option name.
* @return string
* The query type plugin being used.
*/
public function getOptions();
public function getQueryType();
/**
* Sets an option.
*
* @param string $name
* The name of an option.
* @param mixed $option
* The new option.
* Returns the query operator.
*
* @return $this
* @return string
* The query operator being used.
*/
public function setOption($name, $option);
public function getQueryOperator();
/**
* Sets the index's options.
* Returns the value of the exclude boolean.
*
* @param array $options
* The new index options.
* This will return true when the current facet's value should be exclusive
* from the search rather than inclusive.
* When this returns TRUE, the operator will be "<>" instead of "=".
*
* @return $this
* @return bool
* A boolean flag indicating if search should exlude selected facets
*/
public function setOptions(array $options);
public function getExclude();
/**
* Gets the facet manager plugin id
* Returns the plugin name for the url processor.
*
* @return string
* The id of the url processor.
*/
public function getManagerPluginId();
public function getUrlProcessorName();
/**
* Sets a string representation of the Facet source plugin.
......@@ -182,75 +190,182 @@ interface FacetInterface extends ConfigEntityInterface {
* This is usually the name of the Search-api view.
*
* @param string $facet_source_id
* The facet source id.
*
* @return $this
* Returns self.
*/
public function setFacetSourceId($facet_source_id);
/**
* Sets the query operator.
*
* @param string $operator
* The query operator being used.
*/
public function setQueryOperator($operator);
/**
* Sets the exclude.
*
* @param bool $exclude
* A boolean flag indicating if search should exclude selected facets
*/
public function setExclude($exclude);
/**
* Returns the Facet source id.
*
* @return string
* The id of the facet source.
*/
public function getFacetSourceId();
/**
* Returns the plugin instance of a facet source.
*
* @return \Drupal\facetapi\FacetSource\FacetSourceInterface
* @return \Drupal\facets\FacetSource\FacetSourcePluginInterface
* The plugin instance for the facet source.
*/
public function getFacetSource();
/**
* Load the facet sources for this facet.
* Returns the facet source configuration object.
*
* @param bool|TRUE $only_enabled
* @return FacetSourceInterface[]
* @return \Drupal\facets\FacetSourceInterface
* A facet source configuration object.
*/
public function getFacetSources($only_enabled = TRUE);
public function getFacetSourceConfig();
/**
* Get the path to which the facet should link.
* Loads the facet sources for this facet.
*
* @param bool|TRUE $only_enabled
* Only return enabled facet sources.
*
* @param string $path
* @return \Drupal\facets\FacetSource\FacetSourcePluginInterface[]
* An array of facet sources.
*/
public function setPath($path);
public function getFacetSources($only_enabled = TRUE);
/**
* Get the path to which the facet should link.
* Returns an array of processors with their configuration.
*
* @return NULL|string
* @param bool|TRUE $only_enabled
* Only return enabled processors.
*
* @return \Drupal\facets\Processor\ProcessorInterface[]
* An array of processors.
*/
public function getPath();
public function getProcessors($only_enabled = TRUE);
/**
* Returns an array of processors with their configuration.
*
* @return array
* Loads this facets processors for a specific stage.
*
* @param string $stage
* The stage for which to return the processors. One of the
* \Drupal\facets\Processor\ProcessorInterface::STAGE_* constants.
* @param bool $only_enabled
* (optional) If FALSE, also include disabled processors. Otherwise, only
* load enabled ones.
*
* @return \Drupal\facets\Processor\ProcessorInterface[]
* An array of all enabled (or available, if if $only_enabled is FALSE)
* processors that support the given stage, ordered by the weight for that
* stage.
*/
public function getProcessorConfigs();
public function getProcessorsByStage($stage, $only_enabled = TRUE);
/**
* Sets the processors with their config.
* Retrieves this facets's processor configs.
*
* @param array $processor_config
* @return array
* An array of processors and their configs.
*/
public function setProcessorConfigs($processor_config = []);
public function getProcessorConfigs();
/**
* Sets the "only visible when facet source is visible" boolean flag.
*
* @param boolean $only_visible_when_facet_source_is_visible
* @param bool $only_visible_when_facet_source_is_visible
* A boolean flag indicating if the facet should be hidden on a page that
* does not show the facet source.
*
* @return $this
* Returns self.
*/
public function setOnlyVisibleWhenFacetSourceIsVisible($only_visible_when_facet_source_is_visible);
/**
* Returns the "only visible when facet source is visible" boolean flag.
*
* @return boolean
* @return bool
* True when the facet is only shown on a page with the facet source.
*/
public function getOnlyVisibleWhenFacetSourceIsVisible();
/**
* Adds a processor for this facet.
*
* @param array $processor
* An array definition for a processor.
*/
public function addProcessor(array $processor);
/**
* Removes a processor for this facet.
*
* @param string $processor_id
* The plugin id of the processor.
*/
public function removeProcessor($processor_id);
/**
* Defines the no-results behavior.
*
* @param array $behavior
* The definition of the behavior.
*/
public function setEmptyBehavior(array $behavior);
/**
* Returns the defined no-results behavior or NULL if none defined.
*
* @return array|NULL
* The behavior definition or NULL.
*/
public function getEmptyBehavior();
/**
* Returns the configuration of the selected widget.
*
* @return array
* The configuration settings for the widget.
*/
public function getWidgetConfigs();
/**
* Sets the configuration for the widget of this facet.
*
* @param array $widget_config
* The configuration settings for the widget.
*/
public function setWidgetConfigs(array $widget_config);
/**
* Returns any additional configuration for this facet, not defined above.
*
* @return array
* An array of additional configuration for the facet.
*/
public function getFacetConfigs();
/**
* Defines any additional configuration for this facet not defined above.
*
* @param array $facet_config
* An array of additional configuration for the facet.
*/
public function setFacetConfigs(array $facet_config);
}
......@@ -2,15 +2,15 @@
/**
* @file
* Contains \Drupal\facetapi\FacetListBuilder.
* Contains \Drupal\facets\FacetListBuilder.
*/
namespace Drupal\facetapi;
namespace Drupal\facets;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\facetapi\FacetSource\FacetSourceInterface;
use Drupal\Core\Link;
/**
* Builds a listing of facet entities.
......@@ -41,6 +41,13 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'url' => $entity->urlInfo('edit-form'),
);
}
if ($entity->access('update') && $entity->hasLinkTemplate('display-form')) {
$operations['display'] = array(
'title' => $this->t('Display'),
'weight' => 20,
'url' => $entity->urlInfo('display-form'),
);
}
if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) {
$operations['delete'] = array(
'title' => $this->t('Delete'),
......@@ -74,7 +81,7 @@ class FacetListBuilder extends ConfigEntityListBuilder {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
/** @var \Drupal\facets\FacetInterface $entity */
$row = parent::buildRow($entity);
$status_label = $entity->status() ? $this->t('Enabled') : $this->t('Disabled');
......@@ -91,14 +98,14 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'data' => array(
'type' => array(
'data' => 'Facet',
'class' => array('facetapi-type'),
'class' => array('facets-type'),
),
'title' => array(
'data' => array(
'#type' => 'link',
'#title' => $entity->getName(),
'#suffix' => '<div>' . $entity->getFieldAlias() . ' - ' . $entity->getWidget() . '</div>',
) + $entity->urlInfo('edit-form')->toRenderArray(),
'#type' => 'link',
'#title' => $entity->getName(),
'#suffix' => '<div>' . $entity->getFieldAlias() . ' - ' . $entity->getWidget() . '</div>',
) + $entity->urlInfo('edit-form')->toRenderArray(),
'class' => array('search-api-title'),
),
'status' => array(
......@@ -113,14 +120,14 @@ class FacetListBuilder extends ConfigEntityListBuilder {
}
/**
* {@inheritdoc}
* Builds an array of facet sources for display in the overview.
*/
public function buildFacetSourceRow(array $facet_source = []) {
return array(
'data' => array(
'type' => array(
'data' => 'Facet source',
'class' => array('facetapi-type'),
'class' => array('facets-type'),
),
'title' => array(
'data' => $facet_source['id'],
......@@ -128,7 +135,13 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'status' => array(
'data' => '',
),
'operations' => array(),
'operations' => array(
'data' => Link::createFromRoute(
$this->t('Configure'),
'entity.facets_facet_source.edit_form',
['source_id' => $facet_source['id']]
)->toRenderable(),
),
),
'class' => array('facet-source'),
);
......@@ -139,7 +152,20 @@ class FacetListBuilder extends ConfigEntityListBuilder {
*/
public function render() {
$groups = $this->loadGroups();
$list['#attached']['library'][] = 'facetapi/drupal.facetapi.admin_css';
// When no facet sources are found, we should show a message that you can't
// add facets yet.
if (empty($groups['facet_source_groups'])) {
return [
'#markup' => $this->t(
'You currently have no facet sources defined. You should start by adding a facet source before creating facets.<br />
An example of a facet source is a view based on Search API or a Search API page.
Other modules can also implement a facet source by providing a plugin that implements the FacetSourcePluginInterface.'
),
];
}
$list['#attached']['library'][] = 'facets/drupal.facets.admin_css';
$list['#type'] = 'container';
......@@ -149,7 +175,9 @@ class FacetListBuilder extends ConfigEntityListBuilder {
'#rows' => array(),
'#empty' => $groups['lone_facets'] ? '' : $this->t('There are no facet sources or facets defined.'),
'#attributes' => array(
'id' => 'facetapi-groups-list',
'class' => array(
'facets-groups-list',
),
),
);
......@@ -186,22 +214,21 @@ class FacetListBuilder extends ConfigEntityListBuilder {
* - lone_facets: All facets that aren't attached to any facet source.
*/
public function loadGroups() {
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facetapi.facet_source');
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
$facets = $this->storage->loadMultiple();
$facet_sources = $facet_source_plugin_manager->getDefinitions();
$this->sortByStatusThenAlphabetically($facets);
// $this->sortByStatusThenAlphabetically($facet_sources);
$facet_source_groups = array();
foreach ($facet_sources as $facet_source) {
$facet_source_groups[$facet_source['id']] = [
'facet_source' => $facet_source,
'facets' => []
'facets' => [],
];
foreach ($facets as $facet) {
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $facet */
/** @var \Drupal\facets\FacetInterface $facet */
if ($facet->getFacetSourceId() == $facet_source['id']) {
$facet_source_groups[$facet_source['id']]['facets'][$facet->id()] = $facet;
// Remove this facet from $facet so it will finally only contain those
......@@ -211,10 +238,10 @@ class FacetListBuilder extends ConfigEntityListBuilder {
}
}
return array(
return [
'facet_source_groups' => $facet_source_groups,
'lone_facets' => $facets,
);
];
}
/**
......
This diff is collapsed.
<?php
/**
* @file
* Contains Drupal\facetapi\FacetManager\FacetManagerInterface.
*/
namespace Drupal\facetapi\FacetManager;
use Drupal\facetapi\FacetInterface;
interface FacetManagerInterface {
/**
* Set the search id.
*
* @return mixed
*/
public function setSearchId($search_id);
/**
* Returns the search path associated with this searcher.
*
* @return string
* A string containing the search path.
*
* @todo D8 should provide an API function for this.
*/
public function getSearchPath();
/**
* Sets the search keys, or query text, submitted by the user.
*
* @param string $keys
* The search keys, or query text, submitted by the user.
*
* @return FacetapiFacetManager
* An instance of this class.
*/
public function setSearchKeys($keys);
/**
* Gets the search keys, or query text, submitted by the user.
*
* @return string
* The search keys, or query text, submitted by the user.
*/
public function getSearchKeys();
/**
* Returns the number of results returned by the search query.
*
* @return int
* The number of results returned by the search query.
*/
public function getResultCount();
/**
* Returns the number of results per page.
*
* @return int
* The number of results per page, or the limit.
*/
public function getPageLimit();
/**
* Returns the page number of the search result set.
*
* @return int
* The current page of the result set.
*/
public function getPageNumber();
/**
* Returns the total number of pages in the result set.
*
* @return int
* The total number of pages.
*/
public function getPageTotal();
/**
* Allows the backend to add facet queries to its native query object.
*
* This method is called by the implementing module to initialize the facet
* display process. The following actions are taken:
* - FacetapiFacetManager::initActiveFilters() hook is invoked.
* - Dependency plugins are instantiated and executed.
* - Query type plugins are executed.
*
* @param mixed $query
* The backend's native query object.
*
* @todo Should this method be deprecated in favor of one name init()? This
* might make the code more readable in implementing modules.
*
* @see FacetapiFacetManager::initActiveFilters()
*/
public function alterQuery(&$query);
/**
* Returns enabled facets for the searcher associated with this FacetManager.
*
* @return array
* An array of enabled facets.
*/
public function getEnabledFacets();
/**
* Returns the searcher id.
*
* @return string
*/
public function getSearcherId();
/**
* Initializes facet builds, sets the breadcrumb trail.
*
* Facets are built via FacetapiFacetProcessor objects. Facets only need to be
* processed, or built, once regardless of how many realms they are rendered
* in. The FacetapiFacetManager::processed semaphore is set when this method is
* called ensuring that facets are built only once regardless of how many
* times this method is called.
*
* @todo For clarity, should this method be named buildFacets()?
*/
public function processFacets();
/**
* Update the facet results.
*
* Each facet should be updated with a list of Result objects.
*/
public function updateResults();
/**
* Build the facets and get the render arrays for all facets.
*
* @param FacetInterface $facet
*
* @return array
* Facet render arrays.
*/
public function build(FacetInterface $facet);
}
This diff is collapsed.
<?php
/**
* Contains Drupal\facetapi\FacetManager\FacetManagerPluginManager
*/
namespace Drupal\facetapi\FacetManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
class FacetManagerPluginManager extends DefaultPluginManager implements FacetManagerPluginManagerInterface {
/**
* @var \Drupal\facetapi\FacetManager\FacetManagerInterface[]
*/
protected $facet_managers = [];
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/facetapi/facet_manager', $namespaces, $module_handler, 'Drupal\facetapi\FacetManager\FacetManagerInterface', 'Drupal\facetapi\Annotation\FacetApiFacetManager');
$this->alterInfo('facetapi_facet_manager_info');
$this->setCacheBackend($cache_backend, 'facetapi_facet_manager_plugins');
}
public function getMyOwnChangeLaterInstance($plugin_id, $search_id) {
if (isset($this->facet_managers[$search_id])) {
return $this->facet_managers[$search_id];
}
/** @var FacetManagerInterface $facet_manager */
$facet_manager = $this->createInstance($plugin_id, array());
$facet_manager->setSearchId($search_id);
$this->facet_managers[$search_id] = $facet_manager;
return $facet_manager;
}
}
<?php
/**
* @file Contains Drupal\facetapi\FacetManager\FacetManagerPluginManagerInterface
*/
namespace Drupal\facetapi\FacetManager;
use Drupal\Component\Plugin\PluginManagerInterface;
interface FacetManagerPluginManagerInterface extends PluginManagerInterface {
/**
* Get an instance based on search id.
*
* @TODO: Rename to getInstance when http://drupal.org/node/1894130 is fixed.
*
* @param string $plugin_id
* @param string $search_id
*
* @return mixed
*/
public function getMyOwnChangeLaterInstance($plugin_id, $search_id);
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.