View source
<?php
namespace Drupal\facets\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\facets\Exception\Exception;
use Drupal\facets\Exception\InvalidProcessorException;
use Drupal\facets\Exception\InvalidQueryTypeException;
use Drupal\facets\FacetInterface;
class Facet extends ConfigEntityBase implements FacetInterface {
protected $id;
protected $name;
protected $url_alias;
protected $description;
protected $widget;
protected $widgetInstance;
protected $hierarchy;
protected $hierarchy_processor;
protected $query_operator;
protected $hard_limit;
protected $use_hierarchy = FALSE;
protected $keep_hierarchy_parents_active = FALSE;
protected $expand_hierarchy = FALSE;
protected $enable_parent_when_child_gets_disabled = TRUE;
protected $exclude = FALSE;
protected $field_identifier;
protected $facet_source_id;
protected $facet_source_instance = NULL;
protected $path;
protected $results = [];
protected $active_values = [];
protected $facetSourcePlugins;
protected $processors;
protected $processor_configs = [];
protected $only_visible_when_facet_source_is_visible;
protected $show_only_one_result = FALSE;
protected $empty_behavior;
protected $widget_plugin_manager;
protected $hierarchy_manager;
protected $facetSourceConfig;
protected $weight;
protected $min_count = 1;
public function getWidgetManager() {
return $this->widget_plugin_manager ?: \Drupal::service('plugin.manager.facets.widget');
}
public function getHierarchyManager() {
return $this->hierarchy_manager ?: \Drupal::service('plugin.manager.facets.hierarchy');
}
public function getDescription() {
return $this->description;
}
public function setWidget($id, array $configuration = NULL) {
if ($configuration === NULL) {
$instance = $this
->getWidgetManager()
->createInstance($id);
$configuration = $instance
->getConfiguration();
}
$this->widget = [
'type' => $id,
'config' => $configuration,
];
unset($this->widgetInstance);
}
public function getWidget() {
return $this->widget;
}
public function getWidgetInstance() {
if ($this->widget === NULL) {
throw new InvalidProcessorException();
}
if (!isset($this->widgetInstance)) {
$definition = $this
->getWidget();
$this->widgetInstance = $this
->getWidgetManager()
->createInstance($definition['type'], (array) $definition['config']);
}
return $this->widgetInstance;
}
public function setHierarchy($id, array $configuration = NULL) {
if ($configuration === NULL) {
$instance = $this
->getHierarchyManager()
->createInstance($id);
$configuration = $instance
->getConfiguration();
}
$this->hierarchy = [
'type' => $id,
'config' => $configuration,
];
unset($this->hierarchy_instance);
}
public function getHierarchy() {
return [
'type' => 'taxonomy',
'config' => [],
];
}
public function getHierarchyInstance() {
if (!isset($this->hierarchy_instance)) {
$definition = $this
->getHierarchy();
$this->hierarchy_instance = $this
->getHierarchyManager()
->createInstance($definition['type'], (array) $definition['config']);
}
return $this->hierarchy_instance;
}
protected function loadProcessors() {
if (is_array($this->processors)) {
return $this->processors;
}
$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])) {
$settings = empty($processor_settings[$name]['settings']) ? [] : $processor_settings[$name]['settings'];
$settings['facet'] = $this;
$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.', [
'@id' => $name,
'@class' => $processor_definition['class'],
]);
}
}
return $this->processors;
}
public function getProcessorConfigs() {
return !empty($this->processor_configs) ? $this->processor_configs : [];
}
public function getQueryType() {
$facet_source = $this
->getFacetSource();
if (is_null($facet_source)) {
throw new Exception("No facet source defined for facet.");
}
$query_types = $facet_source
->getQueryTypesForFacet($this);
$widget = $this
->getWidgetInstance();
$widgetQueryType = $widget
->getQueryType();
$processorQueryTypes = [];
foreach ($this
->getProcessors() as $processor) {
$pqt = $processor
->getQueryType();
if ($pqt !== NULL) {
$processorQueryTypes[] = $pqt;
}
}
$processorQueryTypes = array_flip($processorQueryTypes);
if ($widgetQueryType === NULL && count($processorQueryTypes) === 0) {
return $this
->pickQueryType($query_types, 'string');
}
if ($widgetQueryType === NULL && count($processorQueryTypes) === 1) {
return $this
->pickQueryType($query_types, key($processorQueryTypes));
}
if ($widgetQueryType !== NULL && count($processorQueryTypes) === 0) {
return $this
->pickQueryType($query_types, $widgetQueryType);
}
if ($widgetQueryType !== NULL && count($processorQueryTypes) === 1 && key($processorQueryTypes) === $widgetQueryType) {
return $this
->pickQueryType($query_types, $widgetQueryType);
}
throw new InvalidQueryTypeException("Invalid query type combination in widget / processors. Widget: {$widgetQueryType}, Processors: " . implode(', ', array_keys($processorQueryTypes)) . ".");
}
protected function pickQueryType(array $allTypes, $type) {
if (!isset($allTypes[$type])) {
throw new InvalidQueryTypeException("Query type {$type} doesn't exist.");
}
return $allTypes[$type];
}
public function setQueryOperator($operator = '') {
return $this->query_operator = $operator;
}
public function getQueryOperator() {
return $this->query_operator ?: 'or';
}
public function setUseHierarchy($use_hierarchy) {
return $this->use_hierarchy = $use_hierarchy;
}
public function getUseHierarchy() {
return $this->use_hierarchy;
}
public function setKeepHierarchyParentsActive($keep_hierarchy_parents_active) {
return $this->keep_hierarchy_parents_active = $keep_hierarchy_parents_active;
}
public function getKeepHierarchyParentsActive() {
return $this->keep_hierarchy_parents_active;
}
public function setExpandHierarchy($expand_hierarchy) {
return $this->expand_hierarchy = $expand_hierarchy;
}
public function getExpandHierarchy() {
return $this->expand_hierarchy;
}
public function setEnableParentWhenChildGetsDisabled($enable_parent_when_child_gets_disabled) {
return $this->enable_parent_when_child_gets_disabled = $enable_parent_when_child_gets_disabled;
}
public function getEnableParentWhenChildGetsDisabled() {
return $this->enable_parent_when_child_gets_disabled;
}
public function setHardLimit($limit) {
return $this->hard_limit = $limit;
}
public function getHardLimit() {
return $this->hard_limit ?: 0;
}
public function getDataDefinition() {
return $this
->getFacetSource()
->getDataDefinition($this->field_identifier);
}
public function setExclude($exclude) {
return $this->exclude = $exclude;
}
public function getExclude() {
return $this->exclude;
}
public function getFieldAlias() {
$field_alias = preg_replace('/[:\\/]+/', '_', $this->field_identifier);
return $field_alias;
}
public function setActiveItem($value) {
if (!in_array($value, $this->active_values)) {
$this->active_values[] = $value;
}
}
public function getActiveItems() {
return $this->active_values;
}
public function setActiveItems(array $values) {
$this->active_values = $values;
}
public function getFieldIdentifier() {
return $this->field_identifier;
}
public function setFieldIdentifier($field_identifier) {
$this->field_identifier = $field_identifier;
}
public function getName() {
return $this->name;
}
public function getUrlAlias() {
return $this->url_alias;
}
public function setUrlAlias($url_alias) {
$this->url_alias = $url_alias;
}
public function setFacetSourceId($facet_source_id) {
$this->facet_source_id = $facet_source_id;
}
public function getFacetSourceId() {
return $this->facet_source_id;
}
public function getFacetSource() {
if (is_null($this->facet_source_instance) && $this->facet_source_id) {
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
if (!$facet_source_plugin_manager
->hasDefinition($this->facet_source_id)) {
return NULL;
}
$this->facet_source_instance = $facet_source_plugin_manager
->createInstance($this->facet_source_id, [
'facet' => $this,
]);
}
return $this->facet_source_instance;
}
public function getShowOnlyOneResult() {
return $this->show_only_one_result;
}
public function setShowOnlyOneResult($show_only_one_result) {
$this->show_only_one_result = $show_only_one_result;
}
public function getFacetSourceConfig() {
if ($this->facetSourceConfig instanceof FacetSource) {
return $this->facetSourceConfig;
}
$storage = \Drupal::entityTypeManager()
->getStorage('facets_facet_source');
$source_id = str_replace(':', '__', $this->facet_source_id);
$facet_source = $storage
->load($source_id);
if ($facet_source instanceof FacetSource) {
$this->facetSourceConfig = $facet_source;
return $this->facetSourceConfig;
}
$facet_source = new FacetSource([
'id' => $source_id,
'name' => $this->facet_source_id,
'filter_key' => 'f',
'url_processor' => 'query_string',
], 'facets_facet_source');
return $facet_source;
}
public function getResults() {
return $this->results;
}
public function setResults(array $results) {
$this->results = $results;
if (count($this->active_values)) {
foreach ($this->results as $result) {
if (in_array($result
->getRawValue(), $this->active_values)) {
$result
->setActiveState(TRUE);
}
}
}
}
public function isActiveValue($value) {
$is_active = FALSE;
if (in_array($value, $this->active_values)) {
$is_active = TRUE;
}
return $is_active;
}
public function getFacetSources($only_enabled = FALSE) {
if (!isset($this->facetSourcePlugins)) {
$this->facetSourcePlugins = [];
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
foreach ($facet_source_plugin_manager
->getDefinitions() as $name => $facet_source_definition) {
if (class_exists($facet_source_definition['class']) && empty($this->facetSourcePlugins[$name])) {
$config = isset($this->facetSourcePlugins[$name]) ? $this->facetSourcePlugins[$name] : [];
$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'],
]);
}
}
}
if (!$only_enabled) {
return $this->facetSourcePlugins;
}
return array_intersect_key($this->facetSourcePlugins, array_flip($this->facetSourcePlugins));
}
public function getProcessors($only_enabled = TRUE) {
$processors = $this
->loadProcessors();
if ($only_enabled) {
$processors_settings = $this
->getProcessorConfigs();
$processors = array_intersect_key($processors, $processors_settings);
}
return $processors;
}
public function getProcessorsByStage($stage, $only_enabled = TRUE) {
$processors = $this
->getProcessors($only_enabled);
$processor_settings = $this
->getProcessorConfigs();
$processor_weights = [];
foreach ($processors as $name => $processor) {
if ($processor
->supportsStage($stage)) {
if (!empty($processor_settings[$name]['weights'][$stage])) {
$processor_weights[$name] = $processor_settings[$name]['weights'][$stage];
}
else {
$processor_weights[$name] = $processor
->getDefaultWeight($stage);
}
}
}
asort($processor_weights);
$return_processors = [];
foreach ($processor_weights as $name => $weight) {
$return_processors[$name] = $processors[$name];
}
return $return_processors;
}
public function setOnlyVisibleWhenFacetSourceIsVisible($only_visible_when_facet_source_is_visible) {
$this->only_visible_when_facet_source_is_visible = $only_visible_when_facet_source_is_visible;
}
public function getOnlyVisibleWhenFacetSourceIsVisible() {
return $this->only_visible_when_facet_source_is_visible;
}
public function addProcessor(array $processor) {
$this->processor_configs[$processor['processor_id']] = [
'processor_id' => $processor['processor_id'],
'weights' => $processor['weights'],
'settings' => $processor['settings'],
];
ksort($this->processor_configs);
}
public function removeProcessor($processor_id) {
unset($this->processor_configs[$processor_id]);
unset($this->processors[$processor_id]);
}
public function getEmptyBehavior() {
return $this->empty_behavior;
}
public function setEmptyBehavior(array $empty_behavior) {
$this->empty_behavior = $empty_behavior;
}
public function getWeight() {
return $this->weight;
}
public function setWeight($weight) {
$this->weight = $weight;
}
public function setMinCount($min_count) {
$this->min_count = $min_count;
}
public function getMinCount() {
return $this->min_count;
}
public function calculateDependencies() {
parent::calculateDependencies();
$source = $this
->getFacetSource();
if ($source === NULL) {
return $this;
}
$facet_dependencies = $source
->calculateDependencies();
if (!empty($facet_dependencies)) {
$this
->addDependencies($facet_dependencies);
}
return $this;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
self::clearBlockCache();
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
self::clearBlockCache();
}
protected static function clearBlockCache() {
$container = \Drupal::getContainer();
$container
->set('plugin.manager.block', NULL);
$container
->get('plugin.manager.block')
->clearCachedDefinitions();
}
public function __sleep() {
unset($this->facet_source_instance);
unset($this->processors);
return parent::__sleep();
}
}