Skip to content
Snippets Groups Projects
Commit 7abc3fa0 authored by Jimmy Henderickx's avatar Jimmy Henderickx
Browse files

Refactor display page and removed the empty behavior plugin

parent f9109e3c
Branches
Tags
No related merge requests found
Showing
with 171 additions and 653 deletions
......@@ -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,18 +207,18 @@ 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) {
if(!($processor instanceof WidgetOrderProcessorInterface)){
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['processors'][$processor_id]['status'] = array(
$form['facet_settings'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($processor_settings[$processor_id]),
......@@ -271,73 +233,106 @@ class FacetDisplayForm extends EntityForm {
'#access' => !$processor->isHidden(),
);
$processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
$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['processors'][$processor_id]['settings'] = array(
'#type' => 'fieldset',
$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="processors[' . $processor_id . '][status]"]' => array('checked' => TRUE),
':input[name="facet_settings[' . $processor_id . '][status]"]' => array('checked' => TRUE),
),
),
);
$form['processors'][$processor_id]['settings'] += $processor_form;
$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(),
);
// Behavior for empty facets.
$behavior_options = [];
$empty_behavior = $facet->getFieldEmptyBehavior();
foreach ($this->getEmptyBehaviorPluginManager()->getDefinitions() as $behavior_id => $definition) {
$behavior_options[$behavior_id] = !empty($definition['label']) ? $definition['label'] : $behavior_id;
$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['empty_behavior'] = [
'#type' => 'select',
}
}
$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.
$empty_behavior_config = $facet->getOption('empty_behavior');
$form['facet_settings']['empty_behavior'] = [
'#type' => 'radios',
'#title' => t('Empty facet behavior'),
'#default_value' => $empty_behavior ? $empty_behavior : 'none',
'#options' => $behavior_options,
'#default_value' => $empty_behavior_config['behavior'] ?: 'none',
'#options' => ['none' => t('Do not display facet'), 'text' => t('Display text')],
'#description' => $this->t('The action to take when a facet has no items.'),
'#required' => TRUE,
'#ajax' => [
'trigger_as' => ['name' => 'empty_behavior_configure'],
'callback' => '::buildAjaxEmptyBehaviorConfigForm',
'wrapper' => 'facet-api-empty-behavior-config-form',
'method' => 'replace',
'effect' => 'fade',
],
];
$form['empty_behavior_configs'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'facet-api-empty-behavior-config-form',
],
'#tree' => TRUE,
];
$form['empty_behavior_configure_button'] = [
'#type' => 'submit',
'#name' => 'empty_behavior_configure',
'#value' => $this->t('Configure empty behavior'),
'#limit_validation_errors' => [['empty_behavior']],
'#submit' => ['::submitAjaxEmptyBehaviorConfigForm'],
'#ajax' => [
'callback' => '::buildAjaxEmptyBehaviorConfigForm',
'wrapper' => 'facet-api-empty-behavior-config-form',
],
'#attributes' => ['class' => ['js-hide']],
];
$this->buildEmptyBehaviorConfigForm($form, $form_state);
$form['only_visible_when_facet_source_is_visible'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show the facet only when the facet source is also visible.'),
'#description' => $this->t('If checked, the facet will only be rendered on pages where the facet source is being rendered too. If not checked, the facet can be shown on every page.'),
'#default_value' => $facet->getOnlyVisibleWhenFacetSourceIsVisible(),
$form['facet_settings']['empty_behavior_text'] = [
'#type' => 'text_format',
'#title' => $this->t('Empty text'),
'#format' => isset($empty_behavior_config['text_format']) ? $empty_behavior_config['text_format'] : 'plain_text',
'#editor' => true,
'#default_value' => isset($empty_behavior_config['text_format']) ? $empty_behavior_config['text'] : '',
// TODO: Next code (ajax) needs work, editor has an issue where format is still shown, looks like core issue.
// '#states' => array(
// 'visible' => array(
// ':input[name="facet_settings[empty_behavior]"]' => array('value' => 'text'),
// ),
// ),
];
$form['weights'] = array(
......@@ -427,10 +422,17 @@ class FacetDisplayForm extends EntityForm {
$processors = $facet->getProcessors(FALSE);
// Iterate over all processors that have a form and are enabled.
foreach ($form['processors'] as $processor_id => $processor_form) {
foreach ($form['facet_settings'] as $processor_id => $processor_form) {
if (!empty($values['status'][$processor_id])) {
$processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
$processors[$processor_id]->validateConfigurationForm($form['processors'][$processor_id], $processor_form_state);
$processor_form_state = new SubFormState($form_state, array('facet_settings', $processor_id, 'settings'));
$processors[$processor_id]->validateConfigurationForm($form['facet_settings'][$processor_id], $processor_form_state);
}
}
// Iterate over all sorting processors that have a form and are enabled.
foreach ($form['facet_sorting'] as $processor_id => $processor_form) {
if (!empty($values['status'][$processor_id])) {
$processor_form_state = new SubFormState($form_state, array('facet_sorting', $processor_id, 'settings'));
$processors[$processor_id]->validateConfigurationForm($form['facet_sorting'][$processor_id], $processor_form_state);
}
}
}
......@@ -452,7 +454,8 @@ class FacetDisplayForm extends EntityForm {
/** @var \Drupal\facetapi\Processor\ProcessorInterface $processor */
$processors = $facet->getProcessors(FALSE);
foreach ($processors as $processor_id => $processor) {
if (empty($values['processors'][$processor_id]['status'])) {
$form_container_key = $processor instanceof WidgetOrderProcessorInterface ? 'facet_sorting' : 'facet_settings';
if (empty($values[$form_container_key][$processor_id]['status'])) {
continue;
}
$new_settings[$processor_id] = array(
......@@ -460,13 +463,13 @@ class FacetDisplayForm extends EntityForm {
'weights' => array(),
'settings' => array(),
);
$processor_values = $values['processors'][$processor_id];
$processor_values = $values[$form_container_key][$processor_id];
if (!empty($processor_values['weights'])) {
$new_settings[$processor_id]['weights'] = $processor_values['weights'];
}
if (isset($form['processors'][$processor_id]['settings'])) {
$processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
$processor->submitConfigurationForm($form['processors'][$processor_id]['settings'], $processor_form_state, $facet);
if (isset($form[$form_container_key][$processor_id]['settings'])) {
$processor_form_state = new SubFormState($form_state, array($form_container_key, $processor_id, 'settings'));
$processor->submitConfigurationForm($form[$form_container_key][$processor_id]['settings'], $processor_form_state, $facet);
$new_settings[$processor_id]['settings'] = $processor->getConfiguration();
}
}
......@@ -477,9 +480,16 @@ class FacetDisplayForm extends EntityForm {
$facet->setOption('processors', $new_settings);
$facet->setWidget($form_state->getValue('widget'));
$facet->set('widget_configs', $form_state->getValue('widget_configs'));
$facet->setFieldEmptyBehavior($form_state->getValue('empty_behavior'));
$facet->set('empty_behavior_configs', $form_state->getValue('empty_behavior_configs'));
$facet->set('only_visible_when_facet_source_is_visible', $form_state->getValue('only_visible_when_facet_source_is_visible'));
$facet->set('only_visible_when_facet_source_is_visible', $form_state->getValue(['facet_settings','only_visible_when_facet_source_is_visible']));
$empty_behavior_config = [];
$empty_behavior = $form_state->getValue(['facet_settings', 'empty_behavior']);
$empty_behavior_config['behavior'] = $empty_behavior;
if($empty_behavior == 'text'){
$empty_behavior_config['text_format'] = $form_state->getValue(['facet_settings', 'empty_behavior_text', 'format']);
$empty_behavior_config['text'] = $form_state->getValue(['facet_settings', 'empty_behavior_text', 'value']);
}
$facet->setOption('empty_behavior', $empty_behavior_config);
$facet->save();
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
......@@ -511,35 +521,4 @@ class FacetDisplayForm extends EntityForm {
return $actions;
}
/**
* Builds the configuration forms for all the empty behaviors.
*
* @param array $form
* An associative array containing the initial structure of the plugin form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*/
public function buildEmptyBehaviorConfigForm(array &$form, FormStateInterface $form_state) {
$behavior_id = $form_state->getValue('empty_behavior') ?: $this->getEntity()->getFieldEmptyBehavior();
if (!is_null($behavior_id) && $behavior_id !== '') {
$empty_behavior_instance = $this->getEmptyBehaviorPluginManager()->createInstance($behavior_id);
if ($config_form = $empty_behavior_instance->buildConfigurationForm([], $form_state)) {
$form['empty_behavior_configs']['#type'] = 'fieldset';
$form['empty_behavior_configs']['#title'] = $this->t('Configure the %behavior empty behavior', ['%behavior' => $this->emptyBehaviorPluginManager->getDefinition($behavior_id)['label']]);
$form['empty_behavior_configs'] += $config_form;
}
else {
$form['empty_behavior_configs']['#type'] = 'container';
$form['empty_behavior_configs']['#open'] = TRUE;
$form['empty_behavior_configs']['empty_behavior_information_dummy'] = [
'#type' => 'hidden',
'#value' => [],
'#default_value' => '1',
];
}
}
}
}
......@@ -254,7 +254,7 @@ class FacetForm extends EntityForm {
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id);
if ($config_form = $facet_source->buildConfigurationForm([], $form_state, $this->getEntity(), $facet_source)) {
$form['facet_source_configs'][$facet_source_id]['#type'] = 'fieldset';
$form['facet_source_configs'][$facet_source_id]['#type'] = 'container';
$form['facet_source_configs'][$facet_source_id]['#title'] = $this->t('%plugin settings', ['%plugin' => $facet_source->getPluginDefinition()['label']]);
$form['facet_source_configs'][$facet_source_id] += $config_form;
}
......@@ -305,7 +305,8 @@ class FacetForm extends EntityForm {
$facet->setWidget('links');
// Set default empty behaviour
$facet->set('empty_behavior', 'none');
$facet->setOption('empty_behavior', ['behavior' => 'none']);
$facet->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
}
// Make sure the field identifier is copied from within the facet source
......@@ -336,39 +337,15 @@ class FacetForm extends EntityForm {
if ($is_new) {
if (\Drupal::moduleHandler()->moduleExists('block')) {
$message = $this->t('Go to the <a href=":block_overview">Block overview page</a> and add a new "Facet block". If this is your first and only facet, just adding that block make it link to this facet, if you have addded more facets already, please make sure to select the correct Facet to render.', [':block_overview' => \Drupal::urlGenerator()->generateFromRoute('block.admin_display')]);
$message = $this->t('Facet %name has been created. Go to the <a href=":block_overview">Block overview page</a> and add a new "Facet block". If this is your first and only facet, just adding that block make it link to this facet, if you have addded more facets already, please make sure to select the correct Facet to render.', ['%name' => $facet->getName(), ':block_overview' => \Drupal::urlGenerator()->generateFromRoute('block.admin_display')]);
drupal_set_message($message);
$form_state->setRedirect('entity.facetapi_facet.display_form', ['facetapi_facet' => $facet->id()]);
}
}
return $facet;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
// Only save the facet if the form doesn't need to be rebuilt.
if (!$form_state->isRebuilding()) {
try {
$facet = $this->getEntity();
// Check if this is a new facet, if so, ensure it has a widget.
if(!$facet->getWidget()){
$facet->save();
drupal_set_message(t('Facet %name has been created.', ['%name' => $facet->getName()]));
}else{
$facet->save();
drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()]));
}
}
catch (Exception $e) {
$form_state->setRebuild();
watchdog_exception('facetapi', $e);
drupal_set_message($this->t('The facet could not be saved.'), 'error');
}
}
return $facet;
}
/**
......
<?php
/**
* @file
* Contains \Drupal\facetapi\Plugin\facetapi\empty_behavior\EmptyBehaviorNone.
*/
namespace Drupal\facetapi\Plugin\facetapi\empty_behavior;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginBase;
/**
* @FacetApiEmptyBehavior(
* id = "none",
* label = @Translation("Do not display facet"),
* description = @Translation("Do not display a facet when no results"),
* )
*/
class EmptyBehaviorNone extends EmptyBehaviorPluginBase {}
<?php
/**
* @file
* Contains \Drupal\facetapi\Plugin\facetapi\empty_behavior\EmptyBehaviorText.
*/
namespace Drupal\facetapi\Plugin\facetapi\empty_behavior;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginBase;
/**
* @FacetApiEmptyBehavior(
* id = "text",
* label = @Translation("Display text"),
* description = @Translation("Display a text when no results"),
* )
*/
class EmptyBehaviorText extends EmptyBehaviorPluginBase {
/**
* {@inheritdoc}
*/
public function build(array $config) {
return ['#markup' => $config['empty_text']['value']];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Get the facet.
$facet = $form_state->getFormObject()->getEntity();
// Get the configuration for the facet.
$config = $this->configFactory->get('facetapi.facet.' . $facet->id());
// Get the empty behavior configuration from the current facet.
$value_empty_text = isset($config->get('empty_behavior_configs')['empty_text']) ? $config->get('empty_behavior_configs')['empty_text']['value'] : '';
$value_empty_format = isset($config->get('empty_behavior_configs')['empty_text']) ? $config->get('empty_behavior_configs')['empty_text']['format'] : 'plain_text';
// Add the new field to the form.
$form['empty_text'] = [
'#type' => 'text_format',
'#title' => $this->t('Empty text'),
'#format' => $value_empty_format ?: 'plain_text',
'#editor' => FALSE,
'#default_value' => $value_empty_text ?: $value_empty_text,
];
return $form;
}
}
......@@ -54,7 +54,7 @@ class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcesso
'#type' => 'number',
'#min' => 1,
'#default_value' => !is_null($config) ? $config->getConfiguration()['minimum_items'] : $this->defaultConfiguration()['minimum_items'],
'#description' => $this->t('Hide block if the facet contains less than this number of results'),
'#description' => $this->t('Hide block if the facet contains less than this number of results.'),
);
return $build;
......
......@@ -17,8 +17,8 @@ use Drupal\user\Entity\User;
*
* @FacetApiProcessor(
* id = "uid_to_username_callback",
* label = @Translation("uid to username callback"),
* description = @Translation("Transform the uid to user name in facet results"),
* label = @Translation("Transform uid to username"),
* description = @Translation("Show the username instead, when the source field is a user id."),
* stages = {
* "build" = 5
* }
......
......@@ -85,12 +85,6 @@ class IntegrationTest extends FacetWebTestBase {
// Verify that facet blocks appear as expected.
$this->assertFacetBlocksAppear();
// Verify that the facet is visible when removing a facet for example.
$this->goToDeleteFacetPage("Test Facet Name");
$this->assertText('item');
$this->assertText('article');
// Show the facet only when the facet source is visible.
// @TODO Only for SearchApiViewsPage for the moment.
$this->setOptionShowOnlyWhenFacetSourceVisible("Test Facet name");
......@@ -200,10 +194,11 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertResponse(200);
// Configure the text for empty results behavior.
$this->drupalPostForm(NULL, ['empty_behavior' => 'text'], $this->t('Configure empty behavior'));
$this->drupalPostForm(NULL, ['empty_behavior_configs[empty_text][value]' => 'No results found for this block!'], $this->t('Configure empty behavior'));
$this->drupalPostForm(NULL, NULL, $this->t('Save'));
$edit = [
'facet_settings[empty_behavior]' => 'text',
'facet_settings[empty_behavior_text][value]' => 'No results found for this block!'
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
}
......@@ -220,10 +215,9 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertResponse(200);
$edit = [
'only_visible_when_facet_source_is_visible' => 1,
'facet_settings[only_visible_when_facet_source_is_visible]' => TRUE,
'widget' => 'links',
'widget_configs[show_numbers]' => '0',
// 'processors[query_string][status]' => '1',
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
}
......@@ -290,7 +284,7 @@ class IntegrationTest extends FacetWebTestBase {
// Make sure that the redirection back to the overview was successful and
// the newly added facet is shown on the overview page.
$this->assertRaw(t('Facet %name has been updated.', ['%name' => $facet_name]));
$this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
}
......
<?php
/**
* @file Contains \Drupal\Tests\facetapi\Unit\EmptyBehavior\EmptyBehaviorPluginManagerTest.
*/
namespace Drupal\Tests\facetapi\Unit\EmptyBehavior;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager;
use Drupal\Tests\UnitTestCase;
use Zend\Stdlib\ArrayObject;
/**
* @group facetapi
*/
class EmptyBehaviorPluginManagerTest extends UnitTestCase {
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cache;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $discovery;
/**
* The plugin factory.
*
* @var \Drupal\Component\Plugin\Factory\DefaultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $factory;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The plugin manager under test.
*
* @var \Drupal\facetapi\EmptyBehavior\EmptyBehaviorPluginManager
*/
public $sut;
/**
* {@inheritdoc}
*/
public function setUp() {
$this->discovery = $this->getMock(DiscoveryInterface::class);
$this->factory = $this->getMockBuilder(DefaultFactory::class)
->disableOriginalConstructor()
->getMock();
$this->moduleHandler = $this->getMock(ModuleHandlerInterface::class);
$this->cache = $this->getMock(CacheBackendInterface::class);
$namespaces = new ArrayObject();
$this->sut = new EmptyBehaviorPluginManager($namespaces, $this->cache, $this->moduleHandler);
$discovery_property = new \ReflectionProperty($this->sut, 'discovery');
$discovery_property->setAccessible(TRUE);
$discovery_property->setValue($this->sut, $this->discovery);
$factory_property = new \ReflectionProperty($this->sut, 'factory');
$factory_property->setAccessible(TRUE);
$factory_property->setValue($this->sut, $this->factory);
}
/**
* Tests plugin manager constructor.
*/
public function testConstruct() {
$namespaces = new ArrayObject();
$this->sut = new EmptyBehaviorPluginManager($namespaces, $this->cache, $this->moduleHandler);
}
/**
* Tests plugin manager's getDefinitions method.
*/
public function testGetDefinitions() {
$definitions = array(
'foo' => array(
'label' => $this->randomMachineName(),
),
);
$this->discovery->expects($this->once())
->method('getDefinitions')
->willReturn($definitions);
$this->assertSame($definitions, $this->sut->getDefinitions());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment