Commit 3ac6b333 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2610084 by borisson_, StryKaizer: Refactor facet source interface to...

Issue #2610084 by borisson_, StryKaizer: Refactor facet source interface to use the core PluginFormInterface
parent b3e1b546
......@@ -178,7 +178,7 @@ class CoreNodeSearchFacetSource extends FacetSourcePluginBase implements CoreSea
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet, FacetSourcePluginInterface $facet_source) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['field_identifier'] = [
'#type' => 'select',
......@@ -186,7 +186,7 @@ class CoreNodeSearchFacetSource extends FacetSourcePluginBase implements CoreSea
'#title' => $this->t('Facet field'),
'#description' => $this->t('Choose the indexed field.'),
'#required' => TRUE,
'#default_value' => $facet->getFieldIdentifier(),
'#default_value' => $this->facet->getFieldIdentifier(),
];
return $form;
......
......@@ -411,7 +411,7 @@ class Facet extends ConfigEntityBase implements FacetInterface {
if (!$this->facet_source_instance && $this->facet_source_id) {
/* @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
$this->facet_source_instance = $facet_source_plugin_manager->createInstance($this->facet_source_id);
$this->facet_source_instance = $facet_source_plugin_manager->createInstance($this->facet_source_id, ['facet' => $this]);
}
return $this->facet_source_instance;
......
......@@ -7,8 +7,10 @@
namespace Drupal\facets\FacetSource;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\Exception\InvalidProcessorException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Facets\FacetInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -44,17 +46,23 @@ abstract class FacetSourcePluginBase extends PluginBase implements FacetSourcePl
*/
protected $keys;
/**
* The facet we're editing for.
*
* @var \Drupal\facets\FacetInterface
*/
protected $facet;
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
$query_type_plugin_manager
) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queryTypePluginManager = $query_type_plugin_manager;
if (isset($configuration['facet'])) {
$this->facet = $configuration['facet'];
}
}
/**
......@@ -107,10 +115,17 @@ abstract class FacetSourcePluginBase extends PluginBase implements FacetSourcePl
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(FormStateInterface $form_state, FacetInterface &$facet) {
$facet_source_id = $facet->getFacetSourceId();
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$facet_source_id = $this->facet->getFacetSourceId();
$field_identifier = $form_state->getValue('facet_source_configs')[$facet_source_id]['field_identifier'];
$facet->setFieldIdentifier($field_identifier);
$this->facet->setFieldIdentifier($field_identifier);
}
}
......@@ -7,7 +7,7 @@
namespace Drupal\facets\FacetSource;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\facets\FacetInterface;
/**
......@@ -19,29 +19,7 @@ use Drupal\facets\FacetInterface;
*
* @see plugin_api
*/
interface FacetSourcePluginInterface {
/**
* Adds a configuration form for this facet source.
*
* @param array $form
* The configuration form definition.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param \Drupal\facets\FacetInterface $facet
* The facet being edited.
* @param \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source
* The facet source being edited.
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet, FacetSourcePluginInterface $facet_source);
/**
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param \Drupal\facets\FacetInterface $facet
* The facet being edited.
*/
public function submitConfigurationForm(FormStateInterface $form_state, FacetInterface &$facet);
interface FacetSourcePluginInterface extends PluginFormInterface {
/**
* Fill in facet data in to the configured facets.
......@@ -51,14 +29,6 @@ interface FacetSourcePluginInterface {
*/
public function fillFacetsWithResults($facets);
/**
* Returns the path where a facet should link to.
*
* @return string
* The path of the facet.
*/
public function getPath();
/**
* Get the allowed query types for a given facet for the facet source.
*
......@@ -73,6 +43,14 @@ interface FacetSourcePluginInterface {
*/
public function getQueryTypesForFacet(FacetInterface $facet);
/**
* Returns the path where a facet should link to.
*
* @return string
* The path of the facet.
*/
public function getPath();
/**
* Returns true if the Facet source is being rendered in the current request.
*
......
......@@ -256,9 +256,9 @@ class FacetForm extends EntityForm {
if (!is_null($facet_source_id) && $facet_source_id !== '') {
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id);
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
if ($config_form = $facet_source->buildConfigurationForm([], $form_state, $this->getEntity(), $facet_source)) {
if ($config_form = $facet_source->buildConfigurationForm([], $form_state)) {
$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;
......@@ -315,8 +315,8 @@ class FacetForm extends EntityForm {
$facet_source_id = $form_state->getValue('facet_source_id');
if (!is_null($facet_source_id) && $facet_source_id !== '') {
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id);
$facet_source->submitConfigurationForm($form_state, $facet);
$facet_source = $this->getFacetSourcePluginManager()->createInstance($facet_source_id, ['facet' => $this->getEntity()]);
$facet_source->submitConfigurationForm($form, $form_state);
}
$facet->save();
......
......@@ -10,7 +10,6 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\facets\Exception\InvalidQueryTypeException;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\FacetSourcePluginInterface;
use Drupal\search_api\Backend\BackendInterface;
use Drupal\facets\FacetSource\FacetSourcePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -65,7 +64,7 @@ abstract class SearchApiBaseFacetSource extends FacetSourcePluginBase {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet, FacetSourcePluginInterface $facet_source) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['field_identifier'] = [
'#type' => 'select',
......@@ -73,7 +72,7 @@ abstract class SearchApiBaseFacetSource extends FacetSourcePluginBase {
'#title' => $this->t('Facet field'),
'#description' => $this->t('Choose the indexed field.'),
'#required' => TRUE,
'#default_value' => $facet->getFieldIdentifier(),
'#default_value' => $this->facet->getFieldIdentifier(),
];
return $form;
......
......@@ -8,6 +8,7 @@
namespace Drupal\facets\Tests;
use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\Tests\WebTestBase as FacetWebTestBase;
use Drupal\facets\Entity\Facet;
use Drupal\facets\FacetSourceInterface;
......@@ -81,6 +82,7 @@ class UrlIntegrationTest extends FacetWebTestBase {
/** @var \Drupal\facets\FacetInterface $facet */
$facet = Facet::load($id);
$this->assertTrue($facet instanceof FacetInterface);
$config = $facet->getFacetSourceConfig();
$this->assertTrue($config instanceof FacetSourceInterface);
$this->assertEqual(NULL, $config->getFilterKey());
......
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