Commit 7abc3fa0 authored by StryKaizer's avatar StryKaizer
Browse files

Refactor display page and removed the empty behavior plugin

parent f9109e3c
......@@ -32,15 +32,6 @@ facetapi.facet.*:
sequence:
type: string
label: 'Widget plugin configurations'
empty_behavior:
type: string
label: 'Empty Behavior'
empty_behavior_configs:
type: sequence
label: 'Empty Behavior plugin configurations'
sequence:
type: sequence
label: 'Empty Behavior plugin configurations'
only_visible_when_facet_source_is_visible:
type: boolean
label: 'Show this facet only when the facet source is visible.'
......
......@@ -32,7 +32,13 @@
/*
* Facet API Display page
*/
#edit-processors .facetapi-processor-settings {
.facetapi-processor-settings-sorting {
margin-bottom: -7px;
margin-left: 20px;
margin-bottom: 25px;
margin-top: -16px;
}
.facetapi-processor-settings-facet {
margin-left: 20px;
margin-bottom: 20px;
}
\ No newline at end of file
......@@ -11,9 +11,6 @@ services:
plugin.manager.facetapi.processor:
class: Drupal\facetapi\Processor\ProcessorPluginManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@string_translation']
plugin.manager.facetapi.empty_behavior:
class: Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
parent: default_plugin_manager
facetapi.manager:
class: Drupal\facetapi\FacetManager\DefaultFacetManager
arguments:
......@@ -21,7 +18,6 @@ services:
- '@plugin.manager.facetapi.widget'
- '@plugin.manager.facetapi.facet_source'
- '@plugin.manager.facetapi.processor'
- '@plugin.manager.facetapi.empty_behavior'
- '@entity_type.manager'
facetapi.facet_context:
......
<?php
/**
* @file
* Contains \Drupal\facetapi\Annotation\FacetApiEmptyBehavior.
*/
namespace Drupal\facetapi\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API EmptyBehavior annotation.
*
* @see \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
* @see plugin_api
*
* @ingroup plugin_api
*
* @Annotation
*/
class FacetApiEmptyBehavior extends Plugin {
/**
* The empty behavior plugin ID.
*
* @var string
*/
public $id;
/**
* The human-readable name of the empty behavior plugin.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $label;
/**
* The empty behavior description.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $description;
}
<?php
/**
* @file
* Contains Drupal\facetap\EmptyBehavior\EmptyBehaviorInterface.
*/
namespace Drupal\facetapi\EmptyBehavior;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Specifies the publicly available methods of an empty behavior plugin.
*
* @see \Drupal\facetapi\Annotation\FacetApiEmptyBehavior
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorPluginManager
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorInterface
* @see plugin_api
*/
interface EmptyBehaviorInterface extends PluginInspectionInterface, PluginFormInterface {
/**
* Returns the render array used for the facet that is empty, or has no items.
*
* @param array $facet_empty_behavior_configs
* Configuration for the empty behavior.
*
* @return
* The element's render array.
*/
public function build(array $facet_empty_behavior_configs);
}
<?php
/**
* @file
* Contains \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginBase.
*/
namespace Drupal\facetapi\EmptyBehavior;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Common base class for empty behavior plugins.
*
* @see \Drupal\facetapi\Annotation\FacetApiEmptyBehavior
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorPluginManager
* @see \Drupal\facetapi\Plugin\EmptyBehavior\EmptyBehaviorInterface
* @see plugin_api
*/
abstract class EmptyBehaviorPluginBase extends PluginBase implements EmptyBehaviorInterface, ContainerFactoryPluginInterface {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a EmptyBehaviorPluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $container->get('config.factory');
return new static($configuration, $plugin_id, $plugin_definition, $config_factory);
}
/**
* {@inheritdoc}
*/
public function build(array $config) {
return [];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {}
}
<?php
/**
* @file
* Contains \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager.
*/
namespace Drupal\facetapi\EmptyBehavior;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Provides an EmptyBehavior plugin manager.
*
* @see \Drupal\facetapi\Annotation\FacetApiEmptyBehavior
* @see \Drupal\facetapi\EmptyBehavior\EmptyBehaviorInterface
*/
class EmptyBehaviorPluginManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/facetapi/empty_behavior', $namespaces, $module_handler, 'Drupal\facetapi\EmptyBehavior\EmptyBehaviorInterface', 'Drupal\facetapi\Annotation\FacetApiEmptyBehavior');
}
}
......@@ -43,8 +43,6 @@ use Drupal\facetapi\FacetInterface;
* "widget",
* "widget_configs",
* "options",
* "empty_behavior",
* "empty_behavior_configs",
* "only_visible_when_facet_source_is_visible",
* },
* links = {
......@@ -93,13 +91,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $widget_configs;
/**
* Configuration for the empty behavior.
*
* @var string
*/
protected $empty_behavior;
/**
* An array of options configuring this index.
*
......@@ -327,21 +318,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->query_type_name;
}
/**
* {@inheritdoc}
*/
public function setFieldEmptyBehavior($behavior_id) {
$this->empty_behavior = $behavior_id;
return $this;
}
/**
* {@inheritdoc}
*/
public function getFieldEmptyBehavior() {
return $this->empty_behavior;
}
/**
* {@inheritdoc}
*/
......
......@@ -35,23 +35,6 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getFieldIdentifier();
/**
* Sets the empty_behavior id.
*
* @param $behavior_id
* The id for the empty behavior.
*
* @return mixed
*/
public function setFieldEmptyBehavior($behavior_id);
/**
* Get field empty_behavior.
*
* @return mixed
*/
public function getFieldEmptyBehavior();
/**
* Set field identifier.
*
......
......@@ -10,7 +10,6 @@ namespace Drupal\facetapi\FacetManager;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager;
use Drupal\facetapi\Exception\InvalidProcessorException;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\FacetSource\FacetSourcePluginManager;
......@@ -51,13 +50,6 @@ class DefaultFacetManager {
*/
protected $processor_plugin_manager;
/**
* The empty behavior plugin manager.
*
* @var \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
*/
protected $empty_behavior_plugin_manager;
/**
* An array of facets that are being rendered.
*
......@@ -121,16 +113,14 @@ class DefaultFacetManager {
* @param \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager
* @param \Drupal\facetapi\FacetSource\FacetSourcePluginManager $facet_source_manager
* @param \Drupal\facetapi\Processor\ProcessorPluginManager $processor_plugin_manager
* @param \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager $empty_behavior_plugin_manager
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
*/
public function __construct(QueryTypePluginManager $query_type_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_manager, ProcessorPluginManager $processor_plugin_manager, EmptyBehaviorPluginManager $empty_behavior_plugin_manager, EntityTypeManager $entity_type_manager) {
public function __construct(QueryTypePluginManager $query_type_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_manager, ProcessorPluginManager $processor_plugin_manager, EntityTypeManager $entity_type_manager) {
$this->query_type_plugin_manager = $query_type_plugin_manager;
$this->widget_plugin_manager = $widget_plugin_manager;
$this->facet_source_manager = $facet_source_manager;
$this->processor_plugin_manager = $processor_plugin_manager;
$this->empty_behavior_plugin_manager = $empty_behavior_plugin_manager;
$this->facet_storage = $entity_type_manager->getStorage('facetapi_facet');
// Immediately initialize the facets. This can be done directly because the
......@@ -283,17 +273,15 @@ class DefaultFacetManager {
}
$facet->setResults($results);
// Returns the render array, this render array contains the empty behaviour
// if no results are found. If there are results we're going to initialize
// the widget from the widget plugin manager and return it's build method.
// No results behavior handling. Return a custom text or false depending on
// settings.
if (empty($facet->getResults())) {
// Get the empty behavior id and the configuration.
$facet_empty_behavior_configs = $facet->get('empty_behavior_configs');
$behavior_id = $facet->get('empty_behavior');
// Build the result using the empty behavior configuration.
$empty_behavior_plugin = $this->empty_behavior_plugin_manager->createInstance($behavior_id);
return $empty_behavior_plugin->build($facet_empty_behavior_configs);
$empty_behavior = $facet->getOption('empty_behavior');
if($empty_behavior['behavior'] == 'text'){
return ['#markup' => $empty_behavior['text']];
}else{
return;
}
}
// Let the widget plugin render the facet.
......
......@@ -16,7 +16,7 @@ use Drupal\facetapi\Processor\ProcessorInterface;
use Drupal\facetapi\Processor\ProcessorPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\facetapi\Widget\WidgetPluginManager;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
/**
* Provides a form for configuring the processors of a facet.
......@@ -51,13 +51,6 @@ class FacetDisplayForm extends EntityForm {
*/
protected $widgetPluginManager;
/**
* The plugin manager for facet sources.
*
* @var \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
*/
protected $emptyBehaviorPluginManager;
/**
* Constructs an FacetDisplayForm object.
*
......@@ -67,14 +60,11 @@ class FacetDisplayForm extends EntityForm {
* The processor plugin manager.
* @param \Drupal\facetapi\Widget\WidgetPluginManager $widgetPluginManager
* The plugin manager for widgets.
* @param \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager $emptyBehaviorPluginManager
* The plugin manager for empty behaviors.
*/
public function __construct(EntityTypeManager $entity_type_manager, ProcessorPluginManager $processor_plugin_manager, WidgetPluginManager $widgetPluginManager, EmptyBehaviorPluginManager $emptyBehaviorPluginManager) {
public function __construct(EntityTypeManager $entity_type_manager, ProcessorPluginManager $processor_plugin_manager, WidgetPluginManager $widgetPluginManager) {
$this->entityTypeManager = $entity_type_manager;
$this->processorPluginManager = $processor_plugin_manager;
$this->widgetPluginManager = $widgetPluginManager;
$this->emptyBehaviorPluginManager = $emptyBehaviorPluginManager;
}
/**
......@@ -90,10 +80,7 @@ class FacetDisplayForm extends EntityForm {
/** @var \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager */
$widget_plugin_manager = $container->get('plugin.manager.facetapi.widget');
/** @var \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager $empty_behavior_plugin_manager */
$empty_behavior_plugin_manager = $container->get('plugin.manager.facetapi.empty_behavior');
return new static($entity_type_manager, $processor_plugin_manager, $widget_plugin_manager, $empty_behavior_plugin_manager);
return new static($entity_type_manager, $processor_plugin_manager, $widget_plugin_manager);
}
/**
......@@ -113,31 +100,6 @@ class FacetDisplayForm extends EntityForm {
return $this->widgetPluginManager ?: \Drupal::service('plugin.manager.facetapi.widget');
}
/**
* Returns the empty behavior plugin manager.
*
* @return \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
* The processor plugin manager.
*/
protected function getEmptyBehaviorPluginManager() {
return $this->emptyBehaviorPluginManager ?: \Drupal::service('plugin.manager.facetapi.empty_behavior');
}
/**
* Form submission handler for the empty behavior subform.
*/
public function submitAjaxEmptyBehaviorConfigForm($form, FormStateInterface $form_state) {
$form_state->setRebuild();
}
/**
* Handles changes to the selected empty behavior.
*/
public function buildAjaxEmptyBehaviorConfigForm(array $form, FormStateInterface $form_state) {
return $form['empty_behavior_configs'];
}
/**
* Builds the configuration forms for all selected widgets.
*
......@@ -188,7 +150,7 @@ class FacetDisplayForm extends EntityForm {
$form['widget'] = [
'#type' => 'radios',
'#title' => $this->t('Widget'),
'#description' => $this->t('Select the widget used for displaying this facet.'),
'#description' => $this->t('The widget used for displaying this facet.'),
'#options' => $widget_options,
'#default_value' => $facet->getWidget(),
'#required' => TRUE,
......@@ -245,99 +207,132 @@ class FacetDisplayForm extends EntityForm {
$form['#attached']['library'][] = 'search_api/drupal.search_api.index-active-formatters';
$form['#title'] = $this->t('Manage processors for facet %label', array('%label' => $facet->label()));
// Add the list of processors with checkboxes to enable/disable them.
$form['processors'] = array(
// Add the list of all other processors with checkboxes to enable/disable them.
$form['facet_settings'] = array(
'#type' => 'fieldset',
'#title' => $this->t('Other processors'),
'#title' => $this->t('Facet settings'),
'#attributes' => array('class' => array(
'search-api-status-wrapper',
)),
);
foreach ($all_processors as $processor_id => $processor) {
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['processors'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($processor_settings[$processor_id]),
'#description' => $processor->getDescription(),
'#attributes' => array(
'class' => array(
'search-api-processor-status-' . $clean_css_id,
if(!($processor instanceof WidgetOrderProcessorInterface)){
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['facet_settings'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($processor_settings[$processor_id]),
'#description' => $processor->getDescription(),
'#attributes' => array(
'class' => array(
'search-api-processor-status-' . $clean_css_id,
),
'data-id' => $clean_css_id,
),
'data-id' => $clean_css_id,
),
'#disabled' => $processor->isLocked(),
'#access' => !$processor->isHidden(),
);
'#disabled' => $processor->isLocked(),
'#access' => !$processor->isHidden(),
);
$processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
$processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet);
if ($processor_form) {
$form['processors'][$processor_id]['settings'] = array(
'#type' => 'fieldset',
'#title' => $this->t('%processor settings', ['%processor' => (string) $processor->getPluginDefinition()['label']]),
'#attributes' => array('class' => array(
'facetapi-processor-settings-' . Html::cleanCssIdentifier($processor_id),
'facetapi-processor-settings'
),),
'#states' => array(
'visible' => array(
':input[name="processors[' . $processor_id . '][status]"]' => array('checked' => TRUE),
$processor_form_state = new SubFormState($form_state, array('facet_settings', $processor_id, 'settings'));
$processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet);
if ($processor_form) {
$form['facet_settings'][$processor_id]['settings'] = array(
'#type' => 'details',
'#title' => $this->t('%processor settings', ['%processor' => (string) $processor->getPluginDefinition()['label']]),
'#open' => true,
'#attributes' => array('class' => array(
'facetapi-processor-settings-' . Html::cleanCssIdentifier($processor_id),
'facetapi-processor-settings-facet',
'facetapi-processor-settings'
),),
'#states' => array(
'visible' => array(
':input[name="facet_settings[' . $processor_id . '][status]"]' => array('checked' => TRUE),
),
),
);
$form['facet_settings'][$processor_id]['settings'] += $processor_form;
}
}
}
// Add the list of widget sort processors with checkboxes to enable/disable them.
$form['facet_sorting'] = array(
'#type' => 'fieldset',
'#title' => $this->t('Facet sorting'),
'#attributes' => array('class' => array(
'search-api-status-wrapper',
)),
);
foreach ($all_processors as $processor_id => $processor) {
if($processor instanceof WidgetOrderProcessorInterface){
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['facet_sorting'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($processor_settings[$processor_id]),
'#description' => $processor->getDescription(),
'#attributes' => array(
'class' => array(
'search-api-processor-status-' . $clean_css_id,
),
'data-id' => $clean_css_id,
),
'#disabled' => $processor->isLocked(),
'#access' => !$processor->isHidden(),
);
$form['processors'][$processor_id]['settings'] += $processor_form;
$processor_form_state = new SubFormState($form_state, array('facet_sorting', $processor_id, 'settings'));
$processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet);
if ($processor_form) {
$form['facet_sorting'][$processor_id]['settings'] = array(
'#type' => 'container',
// '#title' => $this->t('%processor settings', ['%processor' => (string) $processor->getPluginDefinition()['label']]),
'#open' => true,
'#attributes' => array('class' => array(
'facetapi-processor-settings-' . Html::cleanCssIdentifier($processor_id),
'facetapi-processor-settings-sorting',
'facetapi-processor-settings'
),),
'#states' => array(
'visible' => array(
':input[name="facet_sorting[' . $processor_id . '][status]"]' => array('checked' => TRUE),
),
),
);
$form['facet_sorting'][$processor_id]['settings'] += $processor_form;
}
}
}
$form['facet_settings']['only_visible_when_facet_source_is_visible'] = [
'#type' => 'checkbox',
'#title' => $this->t('Hide facet when facet source is not rendered'),
'#description' => $this->t('When checked, this facet will only be rendered when the facet source is rendered. If you want to show facets on other pages too, you need to uncheck this setting.'),
'#default_value' => $facet->getOnlyVisibleWhenFacetSourceIsVisible(),
];
// Behavior for empty facets.
$behavior_options = [];
$empty_behavior = $facet->getFieldEmptyBehavior();
foreach ($this->getEmptyBehaviorPluginManager()->getDefinitions() as $behavior_id => $definition) {