Commit 09ee8c65 authored by StryKaizer's avatar StryKaizer

Issue #2598262 - Show settings in facet display page

parent d2b0613f
......@@ -27,3 +27,12 @@
#facetapi-groups-list tr.facet .facetapi-type {
padding-left: 3em;
}
/*
* Facet API Display page
*/
#edit-processors .facetapi-processor-settings {
margin-left: 20px;
margin-bottom: 25px;
}
\ No newline at end of file
entity.facetapi_facet.edit_form:
title: 'Edit'
route_name: entity.facetapi_facet.edit_form
base_route: entity.facetapi_facet.edit_form
entity.facetapi_facet.display:
title: 'Display'
route_name: entity.facetapi_facet.display
base_route: entity.facetapi_facet.edit_form
\ No newline at end of file
......@@ -43,7 +43,7 @@ use Drupal\facetapi\Result\Result;
* "facet_source_id",
* "widget",
* "widget_configs",
* "processor_configs",
* "options",
* "empty_behavior",
* "empty_behavior_configs",
* "only_visible_when_facet_source_is_visible",
......@@ -160,13 +160,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $facetSourcePlugins;
/**
* An array containing all processors and their configuration.
*
* @var array
*/
protected $processor_configs;
/**
* Cached information about the processors available for this facet.
*
......@@ -493,19 +486,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->path;
}
/**
* {@inheritdoc}
*/
public function getProcessorConfigs() {
return $this->processor_configs;
}
/**
* {@inheritdoc}
*/
public function setProcessorConfigs($processor_config = []) {
$this->processor_configs = $processor_config;
}
/**
* {@inheritdoc}
*/
......
......@@ -222,13 +222,6 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getPath();
/**
* Returns an array of processors with their configuration.
*
* @return array
*/
public function getProcessorConfigs();
/**
* Returns an array of processors with their configuration.
*
......@@ -237,13 +230,6 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getProcessors($only_enabled = TRUE);
/**
* Sets the processors with their config.
*
* @param array $processor_config
*/
public function setProcessorConfigs($processor_config = []);
/**
* Loads this facets processors for a specific stage.
*
......
......@@ -252,11 +252,11 @@ class DefaultFacetManager {
$this->facets = $this->getEnabledFacets();
foreach ($this->facets as $facet) {
foreach ($facet->getProcessorConfigs() as $processor_configuration) {
$processor_definition = $this->processor_plugin_manager->getDefinition($processor_configuration['processor_id']);
foreach ($facet->getProcessors() as $processor) {
$processor_definition = $processor->getPluginDefinition();
if (is_array($processor_definition['stages']) && array_key_exists(ProcessorInterface::STAGE_PRE_QUERY, $processor_definition['stages'])) {
/** @var PreQueryProcessorInterface $pre_query_processor */
$pre_query_processor = $this->processor_plugin_manager->createInstance($processor_configuration['processor_id']);
$pre_query_processor = $this->processor_plugin_manager->createInstance($processor->getPluginDefinition()['id']);
if (!$pre_query_processor instanceof PreQueryProcessorInterface) {
throw new InvalidProcessorException(new FormattableMarkup("The processor @processor has a pre_query definition but doesn't implement the required PreQueryProcessorInterface interface", ['@processor' => $processor_configuration['processor_id']]));
}
......@@ -326,11 +326,11 @@ class DefaultFacetManager {
// @see \Drupal\facetapi\Processor\WidgetOrderProcessorInterface
$results = $facet->getResults();
foreach ($facet->getProcessorConfigs() as $processor_configuration) {
$processor_definition = $this->processor_plugin_manager->getDefinition($processor_configuration['processor_id']);
foreach ($facet->getProcessors() as $processor) {
$processor_definition = $this->processor_plugin_manager->getDefinition($processor->getPluginDefinition()['id']);
if (is_array($processor_definition['stages']) && array_key_exists(ProcessorInterface::STAGE_BUILD, $processor_definition['stages'])) {
/** @var BuildProcessorInterface $build_processor */
$build_processor = $this->processor_plugin_manager->createInstance($processor_configuration['processor_id']);
$build_processor = $this->processor_plugin_manager->createInstance($processor->getPluginDefinition()['id']);
if (!$build_processor instanceof BuildProcessorInterface) {
throw new InvalidProcessorException(new FormattableMarkup("The processor @processor has a build definition but doesn't implement the required BuildProcessorInterface interface", ['@processor' => $processor_configuration['processor_id']]));
}
......
......@@ -109,7 +109,7 @@ class FacetDisplayForm extends EntityForm {
$form['description']['#markup'] = '<p>' . $this->t('Configure processors which will pre- and post-process data.') . '</p>';
// Add the list of processors with checkboxes to enable/disable them.
$form['status'] = array(
$form['processors'] = array(
'#type' => 'fieldset',
'#title' => $this->t('Enabled'),
'#attributes' => array('class' => array(
......@@ -117,8 +117,9 @@ class FacetDisplayForm extends EntityForm {
)),
);
foreach ($all_processors as $processor_id => $processor) {
$clean_css_id = Html::cleanCssIdentifier($processor_id);
$form['status'][$processor_id] = array(
$form['processors'][$processor_id]['status'] = array(
'#type' => 'checkbox',
'#title' => (string) $processor->getPluginDefinition()['label'],
'#default_value' => $processor->isLocked() || !empty($processor_settings[$processor_id]),
......@@ -132,13 +133,36 @@ class FacetDisplayForm extends EntityForm {
'#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' => (string) $processor->getPluginDefinition()['label'] . ' settings',
'#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),
),
),
);
$form['processors'][$processor_id]['settings'] += $processor_form;
}
}
$form['weights'] = array(
'#type' => 'fieldset',
'#title' => t('Processor order'),
'#type' => 'details',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['weights']['order'] = ['#markup' => "<h3>" . t('Processor order') . "</h3>"];
// Order enabled processors per stage, create all the containers for the
// different stages.
foreach ($stages as $stage => $description) {
......@@ -217,12 +241,12 @@ class FacetDisplayForm extends EntityForm {
$processors = $facet->getProcessors(FALSE);
// Iterate over all processors that have a form and are enabled. TODO: no settings atm
// foreach ($form['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['settings'][$processor_id], $processor_form_state);
// }
// }
foreach ($form['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['settings'][$processor_id], $processor_form_state);
}
}
}
/**
......@@ -242,7 +266,7 @@ class FacetDisplayForm extends EntityForm {
/** @var \Drupal\facetapi\Processor\ProcessorInterface $processor */
$processors = $facet->getProcessors(FALSE);
foreach ($processors as $processor_id => $processor) {
if (empty($values['status'][$processor_id])) {
if (empty($values['processors'][$processor_id]['status'])) {
continue;
}
$new_settings[$processor_id] = array(
......@@ -254,19 +278,20 @@ class FacetDisplayForm extends EntityForm {
if (!empty($processor_values['weights'])) {
$new_settings[$processor_id]['weights'] = $processor_values['weights'];
}
if (isset($form['settings'][$processor_id])) {
if (isset($form['processors'][$processor_id]['settings'])) {
$processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
$processor->submitConfigurationForm($form['settings'][$processor_id], $processor_form_state);
$new_settings[$processor_id]['settings'] = $processor->getConfiguration();
}
}
// Sort the processors so we won't have unnecessary changes.
ksort($new_settings);
if (!$this->entity->getOption('processors', array()) !== $new_settings) {
if ($this->entity->getOption('processors', array()) !== $new_settings) {
$this->entity->setOption('processors', $new_settings);
$this->entity->save();
drupal_set_message($this->t('The indexing workflow was successfully edited. All content was scheduled for reindexing so the new settings can take effect.'));
drupal_set_message($this->t('The facet was updated with the new settings.'));
}
else {
drupal_set_message($this->t('No values were changed.'));
......
......@@ -327,33 +327,6 @@ class FacetForm extends EntityForm {
$this->buildEmptyBehaviorConfigForm($form, $form_state);
$form['processor_configs'] = [
'#type' => 'details',
'#title' => $this->t('Processors'),
'#tree' => TRUE,
'#open' => TRUE,
];
foreach ($this->getProcessorPluginManager()->getDefinitions() as $id => $definition) {
$form['processor_configs'][$id] = [
'#type' => 'details',
'#title' => $this->t('Processor: %id', ['%id' => $id]),
'#open' => TRUE,
];
$form['processor_configs'][$id]['processor_id'] = [
'#title' => 'id',
'#type' => 'hidden',
'#value' => $id
];
$form['processor_configs'][$id]['settings'] = [
'#type' => 'container'
];
/** @var ProcessorInterface $build_processor */
$build_processor = $this->getProcessorPluginManager()->createInstance($id);
$form['processor_configs'][$id]['settings'] = $build_processor->buildConfigurationForm($form, $form_state, $facet);
}
$form['only_visible_when_facet_source_is_visible'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show the facet only when the facet source is also visible.'),
......
......@@ -30,12 +30,12 @@ class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcesso
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$processor_configs = $facet->getProcessorConfigs();
$config = $processor_configs[$this->getPluginId()];
$processors = $facet->getProcessors();
$config = $processors[$this->getPluginId()];
/** @var Result $result */
foreach ($results as $id => $result) {
if ($result->getCount() < $config['settings']['minimum_items']) {
if ($result->getCount() < $config->getConfiguration()['minimum_items']) {
unset($results[$id]);
}
}
......@@ -47,18 +47,25 @@ class MinimumCountProcessor extends ProcessorPluginBase implements BuildProcesso
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$processor_configs = $facet->getProcessorConfigs();
$config = $processor_configs[$this->getPluginId()];
$processors = $facet->getProcessors();
$config = $processors[$this->getPluginId()];
$build['minimum_items'] = array(
'#title' => 'Minimum items',
'#type' => 'number',
'#min' => 1,
'#default_value' => $config['settings']['minimum_items'],
'#default_value' => isset($config) ? $config->getConfiguration()['minimum_items'] : $this->defaultConfiguration()['minimum_items'],
'#description' => 'Hide block if the facet contains less than this number of results',
);
return $build;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array('minimum_items' => 1);
}
}
......@@ -9,11 +9,12 @@ namespace Drupal\facetapi\Processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facetapi\FacetInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
/**
* Describes a processor
*/
interface ProcessorInterface {
interface ProcessorInterface extends ConfigurablePluginInterface {
/**
* Processing stage: pre_query.
......@@ -39,6 +40,15 @@ interface ProcessorInterface {
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet);
/**
* Validates a configuration form for this processor
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\facetapi\FacetInterface $facet
*/
public function validateConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet);
/**
* Checks whether this processor implements a particular stage.
*
......
......@@ -17,6 +17,20 @@ class ProcessorPluginBase extends PluginBase implements ProcessorInterface {
return [];
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
return;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$this->setConfiguration($form_state->getValues());
}
/**
* {@inheritdoc}
*/
......@@ -55,4 +69,34 @@ class ProcessorPluginBase extends PluginBase implements ProcessorInterface {
return isset($plugin_definition['description']) ? $plugin_definition['description'] : '';
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
unset($this->configuration['facet']); // TODO: Do we need this here anyway?
return $this->configuration + $this->defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$this->addDependency('module', $this->getPluginDefinition()['provider']);
return $this->dependencies;
}
}
......@@ -11,8 +11,8 @@ abstract class WidgetOrderPluginBase extends ProcessorPluginBase implements Widg
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$processor_configs = $facet->getProcessorConfigs();
$config = $processor_configs[$this->getPluginId()];
$processors = $facet->getProcessors();
$config = $processors[$this->getPluginId()];
$build['sort'] = [
'#type' => 'radios',
......@@ -21,7 +21,7 @@ abstract class WidgetOrderPluginBase extends ProcessorPluginBase implements Widg
'ASC' => $this->t('Ascending'),
'DESC' => $this->t('Descending')
],
'#default_value' => $config['settings']['sort'],
'#default_value' => isset($config) ? $config->getConfiguration()['sort'] : $this->defaultConfiguration()['sort'],
];
......@@ -32,11 +32,18 @@ abstract class WidgetOrderPluginBase extends ProcessorPluginBase implements Widg
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$processor_configs = $facet->getProcessorConfigs();
$config = $processor_configs[$this->getPluginId()];
$processors = $facet->getProcessors();
$config = $processors[$this->getPluginId()];
// This should load the facet's config to find the ordering direction.
return $this->sortResults($results, $config['settings']['sort']);
return $this->sortResults($results, $config->getConfiguration()['sort']);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['sort' => 'ASC'];
}
}
......@@ -53,12 +53,11 @@ class MinimumCountProcessorTest extends UnitTestCase {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setProcessorConfigs([
$facet->setOption('processors', [
'minimum_count' => [
'settings' => ['minimum_items' => 4]
]
]);
$sorted_results = $this->processor->build($facet, $this->original_results);
$this->assertCount(3, $sorted_results);
......@@ -75,7 +74,7 @@ class MinimumCountProcessorTest extends UnitTestCase {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setProcessorConfigs([
$facet->setOption('processors', [
'minimum_count' => [
'settings' => ['minimum_items' => 5]
]
......@@ -97,7 +96,7 @@ class MinimumCountProcessorTest extends UnitTestCase {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$facet->setProcessorConfigs([
$facet->setOption('processors', [
'minimum_count' => [
'settings' => ['minimum_items' => 8]
]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment