Skip to content
Snippets Groups Projects
Commit 34f0d6db authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2086201 by tim.plunkett, pwolanin: Fixed Use PluginFormInterface...

Issue #2086201 by tim.plunkett, pwolanin: Fixed Use PluginFormInterface instead of one-off form methods for search plugins.
parent 1146ebd4
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AccessibleInterface;
use Drupal\Core\Database\Query\Condition;
......@@ -34,7 +35,7 @@
* path = "node"
* )
*/
class NodeSearch extends SearchPluginBase implements AccessibleInterface, SearchIndexingInterface {
class NodeSearch extends SearchPluginBase implements AccessibleInterface, SearchIndexingInterface, PluginFormInterface {
/**
* A database connection object.
......@@ -516,7 +517,7 @@ public function searchFormSubmit(array &$form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function addToAdminForm(array &$form, array &$form_state) {
public function buildConfigurationForm(array $form, array &$form_state) {
// Output form for defining rank factor weights.
$form['content_ranking'] = array(
'#type' => 'details',
......@@ -537,12 +538,19 @@ public function addToAdminForm(array &$form, array &$form_state) {
'#default_value' => variable_get('node_rank_' . $var, 0),
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitAdminForm(array &$form, array &$form_state) {
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
foreach ($this->moduleHandler->invokeAll('ranking') as $var => $values) {
if (isset($form_state['values']['node_rank_' . $var])) {
// @todo Fix when https://drupal.org/node/1831632 is in.
......
......@@ -10,6 +10,7 @@
use Drupal\Core\Config\Context\ContextInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\search\SearchPluginManager;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -195,7 +196,9 @@ public function buildForm(array $form, array &$form_state) {
// Per plugin settings.
foreach ($active_plugins as $plugin) {
$plugin->addToAdminForm($form, $form_state);
if ($plugin instanceof PluginFormInterface) {
$form = $plugin->buildConfigurationForm($form, $form_state);
}
}
// Set #submit so we are sure it's invoked even if one of
// the active search plugins added its own #submit.
......@@ -218,6 +221,12 @@ public function validateForm(array &$form, array &$form_state) {
form_set_error('default_plugin', $this->t('Your default search plugin is not selected as an active plugin.'));
}
}
// Handle per-plugin validation logic.
foreach ($this->searchPluginManager->getActivePlugins() as $plugin) {
if ($plugin instanceof PluginFormInterface) {
$plugin->validateConfigurationForm($form, $form_state);
}
}
}
/**
......@@ -238,7 +247,9 @@ public function submitForm(array &$form, array &$form_state) {
// Handle per-plugin submission logic.
foreach ($this->searchPluginManager->getActivePlugins() as $plugin) {
$plugin->submitAdminForm($form, $form_state);
if ($plugin instanceof PluginFormInterface) {
$plugin->submitConfigurationForm($form, $form_state);
}
}
// Check whether we are resetting the values.
......
......@@ -98,32 +98,4 @@ public function buildResults();
*/
public function searchFormAlter(array &$form, array &$form_state);
/**
* Adds elements to the search settings form.
*
* The core search module only invokes this method on active module plugins.
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param array $form_state
* A keyed array containing the current state of the form. The arguments
* that drupal_get_form() was originally called with are available in the
* array $form_state['build_info']['args'].
*/
public function addToAdminForm(array &$form, array &$form_state);
/**
* Handles any submission for elements on the search settings form.
*
* The core search module only invokes this method on active module plugins.
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param array $form_state
* A keyed array containing the current state of the form. The arguments
* that drupal_get_form() was originally called with are available in the
* array $form_state['build_info']['args'].
*/
public function submitAdminForm(array &$form, array &$form_state);
}
......@@ -94,18 +94,4 @@ public function searchFormAlter(array &$form, array &$form_state) {
// Empty default implementation.
}
/**
* {@inheritdoc}
*/
public function addToAdminForm(array &$form, array &$form_state) {
// Empty default implementation.
}
/**
* {@inheritdoc}
*/
public function submitAdminForm(array &$form, array &$form_state) {
// Empty default implementation.
}
}
......@@ -9,6 +9,7 @@
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Config\Config;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\search\Plugin\SearchPluginBase;
use Drupal\search\Annotation\SearchPlugin;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -22,7 +23,7 @@
* path = "dummy_path"
* )
*/
class SearchExtraTypeSearch extends SearchPluginBase {
class SearchExtraTypeSearch extends SearchPluginBase implements PluginFormInterface {
/**
* @var \Drupal\Core\Config\Config
......@@ -124,7 +125,7 @@ public function buildResults() {
/**
* {@inheritdoc}
*/
public function addToAdminForm(array &$form, array &$form_state) {
public function buildConfigurationForm(array $form, array &$form_state) {
// Output form for defining rank factor weights.
$form['extra_type_settings'] = array(
'#type' => 'fieldset',
......@@ -141,12 +142,19 @@ public function addToAdminForm(array &$form, array &$form_state) {
),
'#default_value' => $this->configSettings->get('boost'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitAdminForm(array &$form, array &$form_state) {
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
$this->configSettings
->set('boost', $form_state['values']['extra_type_settings']['boost'])
->save();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment