Commit 455ac0ed authored by borisson_'s avatar borisson_
Browse files

Issue #2581027: Save widget information on facet entity

parent 61ee67cb
...@@ -23,3 +23,9 @@ facetapi.facet.*: ...@@ -23,3 +23,9 @@ facetapi.facet.*:
widget: widget:
type: string type: string
label: 'Field identifier' label: 'Field identifier'
widget_configs:
type: sequence
label: 'Widget plugin configurations'
sequence:
type: string
label: 'Widget plugin configurations'
...@@ -41,6 +41,7 @@ use Drupal\facetapi\Result\ResultInterface; ...@@ -41,6 +41,7 @@ use Drupal\facetapi\Result\ResultInterface;
* "search_api_index", * "search_api_index",
* "field_identifier", * "field_identifier",
* "widget", * "widget",
* "widget_configs",
* }, * },
* links = { * links = {
* "canonical" = "/admin/config/search/search-api/index/{search_api_index}/facets", * "canonical" = "/admin/config/search/search-api/index/{search_api_index}/facets",
...@@ -80,6 +81,13 @@ class Facet extends ConfigEntityBase implements FacetInterface { ...@@ -80,6 +81,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/ */
protected $widget; protected $widget;
/**
* Configuration for the widget. This is a key-value stored array.
*
* @var string
*/
protected $widget_configs;
/** /**
* An array of options configuring this index. * An array of options configuring this index.
* *
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\facetapi\Form; namespace Drupal\facetapi\Form;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
...@@ -236,12 +237,13 @@ class FacetForm extends EntityForm { ...@@ -236,12 +237,13 @@ class FacetForm extends EntityForm {
$widget_instance = $this->getWidgetPluginManager()->createInstance($widget); $widget_instance = $this->getWidgetPluginManager()->createInstance($widget);
// @todo Create, use and save SubFormState already here, not only in // @todo Create, use and save SubFormState already here, not only in
// validate(). Also, use proper subset of $form for first parameter? // validate(). Also, use proper subset of $form for first parameter?
if ($config_form = $widget_instance->buildConfigurationForm([], $form_state)) { $config = $this->config('facetapi.facet.' . $facet->id());
$form['widget_config']['#type'] = 'details'; if ($config_form = $widget_instance->buildConfigurationForm([], $form_state, ($config instanceof Config) ? $config : null )) {
$form['widget_config']['#title'] = $this->t('Configure the %widget widget', ['%widget' => $this->getWidgetPluginManager()->getDefinition($widget)['label']]); $form['widget_configs']['#type'] = 'details';
$form['widget_config']['#open'] = $facet->isNew(); $form['widget_configs']['#title'] = $this->t('Configure the %widget widget', ['%widget' => $this->getWidgetPluginManager()->getDefinition($widget)['label']]);
$form['widget_configs']['#open'] = $facet->isNew();
$form['widget_config'] += $config_form; $form['widget_configs'] += $config_form;
} }
} }
} }
......
...@@ -9,7 +9,7 @@ use Drupal\facetapi\Widget\WidgetInterface; ...@@ -9,7 +9,7 @@ use Drupal\facetapi\Widget\WidgetInterface;
/** /**
* @FacetApiWidget( * @FacetApiWidget(
* id = "checkbox", * id = "checkbox",
* label = @Translation("Checkbox widget"), * label = @Translation("List of checkboxes"),
* description = @Translation("A widget that shows checkboxes"), * description = @Translation("A widget that shows checkboxes"),
* ) * )
* *
...@@ -36,7 +36,26 @@ class CheckboxWidget implements WidgetInterface { ...@@ -36,7 +36,26 @@ class CheckboxWidget implements WidgetInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state, $config) {
$checkbox_options = [
'before' => $this->t('Before'),
'after' => $this->t('After'),
];
$form['checkbox_placement'] = [
'#type' => 'radios',
'#title' => $this->t('Checkbox placement'),
'#description' => $this->t('Choose where the checkboxes should be placed'),
'#options' => $checkbox_options,
'#required' => TRUE,
];
if (!is_null($config)) {
$widget_configs = $config->get('widget_configs');
if (isset($widget_configs['checkbox_placement'])) {
$form['checkbox_placement']['#default_value'] = $widget_configs['checkbox_placement'];
}
}
return $form; return $form;
} }
......
Supports Markdown
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