diff --git a/core/lib/Drupal/Core/Config/Entity/DraggableListController.php b/core/lib/Drupal/Core/Config/Entity/DraggableListController.php index 5370be5..117bba0 100644 --- a/core/lib/Drupal/Core/Config/Entity/DraggableListController.php +++ b/core/lib/Drupal/Core/Config/Entity/DraggableListController.php @@ -39,6 +39,13 @@ protected $weightKey = FALSE; /** + * The form builder. + * + * @var \Drupal\Core\Form\FormBuilderInterface + */ + protected $formBuilder; + + /** * {@inheritdoc} */ public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler) { @@ -87,7 +94,7 @@ public function buildRow(EntityInterface $entity) { */ public function render() { if (!empty($this->weightKey)) { - return drupal_get_form($this); + return $this->formBuilder()->getForm($this); } return parent::render(); } @@ -144,4 +151,17 @@ public function submitForm(array &$form, array &$form_state) { } } + /** + * Returns the form builder. + * + * @return \Drupal\Core\Form\FormBuilderInterface + * The form builder. + */ + protected function formBuilder() { + if (!$this->formBuilder) { + $this->formBuilder = \Drupal::formBuilder(); + } + return $this->formBuilder; + } + } diff --git a/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php b/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php index 393de7d..0bc3443 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php @@ -7,7 +7,6 @@ namespace Drupal\node\Plugin\Search; -use Drupal\Core\Annotation\Translation; use Drupal\Core\Config\Config; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Query\SelectExtender; @@ -19,7 +18,6 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Access\AccessibleInterface; use Drupal\Core\Database\Query\Condition; -use Drupal\search\Annotation\SearchPlugin; use Drupal\search\Plugin\ConfigurableSearchPluginBase; use Drupal\search\Plugin\SearchIndexingInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -267,7 +265,7 @@ public function execute() { } /** - * Gathers the rankings from the the hook_ranking() implementations. + * Adds the configured rankings to the search query. * * @param $query * A query object that has been extended with the Search DB Extender. @@ -523,10 +521,10 @@ public function searchFormSubmit(array &$form, array &$form_state) { } /** - * Gathers additional rankings from hook_ranking(). + * Gathers ranking definitions from hook_ranking(). * * @return array - * An array of ranking information. + * An array of ranking definitions. */ protected function getRankings() { if (!$this->rankings) { diff --git a/core/modules/search/lib/Drupal/search/Controller/SearchController.php b/core/modules/search/lib/Drupal/search/Controller/SearchController.php index 07f5399..6767767 100644 --- a/core/modules/search/lib/Drupal/search/Controller/SearchController.php +++ b/core/modules/search/lib/Drupal/search/Controller/SearchController.php @@ -9,8 +9,8 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\Core\Form\FormBuilderInterface; use Drupal\search\SearchPageInterface; +use Drupal\search\SearchPageRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -20,20 +20,20 @@ class SearchController extends ControllerBase implements ContainerInjectionInterface { /** - * The form builder. + * The search page repository. * - * @var \Drupal\Core\Form\FormBuilderInterface + * @var \Drupal\search\SearchPageRepositoryInterface */ - protected $formBuilder; + protected $searchPageRepository; /** * Constructs a new search controller. * - * @param \Drupal\Core\Form\FormBuilderInterface $form_builder - * The form builder. + * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository + * The search page repository. */ - public function __construct(FormBuilderInterface $form_builder) { - $this->formBuilder = $form_builder; + public function __construct(SearchPageRepositoryInterface $search_page_repository) { + $this->searchPageRepository = $search_page_repository; } /** @@ -41,7 +41,7 @@ public function __construct(FormBuilderInterface $form_builder) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('form_builder') + $container->get('search.search_page_repository') ); } @@ -118,7 +118,7 @@ public function redirectSearchPage(SearchPageInterface $entity) { * The title for the search page edit form. */ public function editTitle(SearchPageInterface $search_page) { - return $this->t('Edit @label search', array('@label' => $search_page->label())); + return $this->t('Edit @label search page', array('@label' => $search_page->label())); } /** @@ -137,4 +137,21 @@ public function performOperation(SearchPageInterface $search_page, $op) { return $this->redirect('search.settings'); } + /** + * Sets the search page as the default. + * + * @param \Drupal\search\SearchPageInterface $search_page + * The search page entity. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect to the search settings page. + */ + public function setAsDefault(SearchPageInterface $search_page) { + // Set the default page to this search page. + $this->searchPageRepository->setDefaultSearchPage($search_page); + + drupal_set_message($this->t('The default search page is now %label. Be sure to check the ordering of your search pages.', array('%label' => $search_page->label()))); + return $this->redirect('search.settings'); + } + } diff --git a/core/modules/search/lib/Drupal/search/Entity/SearchPage.php b/core/modules/search/lib/Drupal/search/Entity/SearchPage.php index 9834477..ecf347a 100644 --- a/core/modules/search/lib/Drupal/search/Entity/SearchPage.php +++ b/core/modules/search/lib/Drupal/search/Entity/SearchPage.php @@ -111,7 +111,7 @@ class SearchPage extends ConfigEntityBase implements SearchPageInterface { public function __construct(array $values, $entity_type) { parent::__construct($values, $entity_type); - $this->pluginBag = new SearchPluginBag($this->searchPluginManager(), array($this->plugin), $this->configuration); + $this->pluginBag = new SearchPluginBag($this->searchPluginManager(), array($this->plugin), $this->configuration, $this->id()); } /** @@ -140,7 +140,7 @@ public function isIndexable() { * {@inheritdoc} */ public function isDefaultSearch() { - return $this->configFactory()->get('search.settings')->get('default_page') == $this->id(); + return $this->searchPageRepository()->getDefaultSearchPage() == $this->id(); } /** @@ -215,7 +215,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $ public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) { parent::postDelete($storage_controller, $entities); - $search_page_repository = static::searchPageRepository(); + $search_page_repository = \Drupal::service('search.search_page_repository'); if (!$search_page_repository->isSearchActive()) { $search_page_repository->clearDefaultSearchPage(); } @@ -258,7 +258,7 @@ protected function configFactory() { * * @return \Drupal\search\SearchPageRepositoryInterface */ - protected static function searchPageRepository() { + protected function searchPageRepository() { return \Drupal::service('search.search_page_repository'); } diff --git a/core/modules/search/lib/Drupal/search/Form/SearchPageAddForm.php b/core/modules/search/lib/Drupal/search/Form/SearchPageAddForm.php index bf8a089..fb47b25 100644 --- a/core/modules/search/lib/Drupal/search/Form/SearchPageAddForm.php +++ b/core/modules/search/lib/Drupal/search/Form/SearchPageAddForm.php @@ -39,11 +39,9 @@ public function save(array $form, array &$form_state) { drupal_set_message($this->t('The search page has been added.')); - $search_settings = $this->config('search.settings'); // If there is no default search page, make the added search the default. - if (!$search_settings->get('default_page')) { - $search_settings->set('default_page', $this->entity->id()); - $search_settings->save(); + if (!$this->searchPageRepository->getDefaultSearchPage()) { + $this->searchPageRepository->setDefaultSearchPage($this->entity); } } diff --git a/core/modules/search/lib/Drupal/search/Form/SearchPageForm.php b/core/modules/search/lib/Drupal/search/Form/SearchPageForm.php index 710d931..d817f7c 100644 --- a/core/modules/search/lib/Drupal/search/Form/SearchPageForm.php +++ b/core/modules/search/lib/Drupal/search/Form/SearchPageForm.php @@ -8,7 +8,6 @@ namespace Drupal\search\Form; use Drupal\Core\Entity\EntityFormController; -use Drupal\search\SearchPageInterface; /** * Provides a search form for site wide search. diff --git a/core/modules/search/lib/Drupal/search/Form/SearchPageFormBase.php b/core/modules/search/lib/Drupal/search/Form/SearchPageFormBase.php index e263be1..e3f0763 100644 --- a/core/modules/search/lib/Drupal/search/Form/SearchPageFormBase.php +++ b/core/modules/search/lib/Drupal/search/Form/SearchPageFormBase.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityFormController; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Plugin\PluginFormInterface; +use Drupal\search\SearchPageRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -39,13 +40,23 @@ protected $entityQuery; /** + * The search page repository. + * + * @var \Drupal\search\SearchPageRepositoryInterface + */ + protected $searchPageRepository; + + /** * Constructs a new search form. * * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query * The entity query. + * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository + * The search page repository. */ - public function __construct(QueryFactory $entity_query) { + public function __construct(QueryFactory $entity_query, SearchPageRepositoryInterface $search_page_repository) { $this->entityQuery = $entity_query; + $this->searchPageRepository = $search_page_repository; } /** @@ -53,7 +64,8 @@ public function __construct(QueryFactory $entity_query) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.query') + $container->get('entity.query'), + $container->get('search.search_page_repository') ); } @@ -131,15 +143,6 @@ public function exists($id) { /** * {@inheritdoc} */ - protected function actions(array $form, array &$form_state) { - $actions = parent::actions($form, $form_state); - unset($actions['delete']); - return $actions; - } - - /** - * {@inheritdoc} - */ public function validate(array $form, array &$form_state) { parent::validate($form, $form_state); diff --git a/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php b/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php index 4ea29a5..8ac2511 100644 --- a/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php +++ b/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php @@ -7,13 +7,17 @@ namespace Drupal\search\Plugin; -use Drupal\Component\Plugin\ConfigurablePluginInterface; -use Drupal\Core\Plugin\PluginFormInterface; - /** * Provides a base implementation for a configurable Search plugin. */ -abstract class ConfigurableSearchPluginBase extends SearchPluginBase implements ConfigurablePluginInterface, PluginFormInterface { +abstract class ConfigurableSearchPluginBase extends SearchPluginBase implements ConfigurableSearchPluginInterface { + + /** + * The unique ID for the search page using this plugin. + * + * @var string + */ + protected $searchPageId; /** * {@inheritdoc} @@ -51,4 +55,12 @@ public function setConfiguration(array $configuration) { public function validateConfigurationForm(array &$form, array &$form_state) { } + /** + * {@inheritdoc} + */ + public function setSearchPageId($search_page_id) { + $this->searchPageId = $search_page_id; + return $this; + } + } diff --git a/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginInterface.php b/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginInterface.php new file mode 100644 index 0000000..62899c6 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginInterface.php @@ -0,0 +1,28 @@ +searchPageId = $search_page_id; + } + + /** * {@inheritdoc} * * @return \Drupal\search\Plugin\SearchInterface @@ -23,4 +40,16 @@ public function &get($instance_id) { return parent::get($instance_id); } + /** + * {@inheritdoc} + */ + protected function initializePlugin($instance_id) { + parent::initializePlugin($instance_id); + + $plugin_instance = $this->pluginInstances[$instance_id]; + if ($plugin_instance instanceof ConfigurableSearchPluginInterface) { + $plugin_instance->setSearchPageId($this->searchPageId); + } + } + } diff --git a/core/modules/search/lib/Drupal/search/Routing/SearchPluginRoutes.php b/core/modules/search/lib/Drupal/search/Routing/SearchPageRoutes.php similarity index 95% rename from core/modules/search/lib/Drupal/search/Routing/SearchPluginRoutes.php rename to core/modules/search/lib/Drupal/search/Routing/SearchPageRoutes.php index 1378ecd..9a5f505 100644 --- a/core/modules/search/lib/Drupal/search/Routing/SearchPluginRoutes.php +++ b/core/modules/search/lib/Drupal/search/Routing/SearchPageRoutes.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\search\Routing\SearchPluginRoutes. + * Contains \Drupal\search\Routing\SearchPageRoutes. */ namespace Drupal\search\Routing; @@ -15,7 +15,7 @@ /** * Provides dynamic routes for search. */ -class SearchPluginRoutes implements ContainerInjectionInterface { +class SearchPageRoutes implements ContainerInjectionInterface { /** * The search page repository. diff --git a/core/modules/search/lib/Drupal/search/SearchPageInterface.php b/core/modules/search/lib/Drupal/search/SearchPageInterface.php index 7b40280..715bf0f 100644 --- a/core/modules/search/lib/Drupal/search/SearchPageInterface.php +++ b/core/modules/search/lib/Drupal/search/SearchPageInterface.php @@ -10,7 +10,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; /** - * Provides an interface defining a search entity. + * Provides an interface defining a search page entity. */ interface SearchPageInterface extends ConfigEntityInterface { @@ -18,7 +18,7 @@ * Returns the search plugin. * * @return \Drupal\search\Plugin\SearchInterface - * The search plugin used by this search entity. + * The search plugin used by this search page entity. */ public function getPlugin(); @@ -31,18 +31,18 @@ public function getPlugin(); public function setPlugin($plugin_id); /** - * Determines if this search entity is currently the default search. + * Determines if this search page entity is currently the default search. * * @return bool - * TRUE if this search entity is the default search, FALSE otherwise. + * TRUE if this search page entity is the default search, FALSE otherwise. */ public function isDefaultSearch(); /** - * Determines if this search entity is indexable. + * Determines if this search page entity is indexable. * * @return bool - * TRUE if this search entity is indexable, FALSE otherwise. + * TRUE if this search page entity is indexable, FALSE otherwise. */ public function isIndexable(); diff --git a/core/modules/search/lib/Drupal/search/SearchPageListController.php b/core/modules/search/lib/Drupal/search/SearchPageListController.php index a4dc655..70c8c90 100644 --- a/core/modules/search/lib/Drupal/search/SearchPageListController.php +++ b/core/modules/search/lib/Drupal/search/SearchPageListController.php @@ -14,13 +14,11 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormInterface; -use Drupal\Core\KeyValueStore\StateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a listing of search entities. + * Provides a listing of search page entities. */ class SearchPageListController extends DraggableListController implements FormInterface { @@ -39,13 +37,6 @@ class SearchPageListController extends DraggableListController implements FormIn protected $configFactory; /** - * The form builder. - * - * @var \Drupal\Core\Form\FormBuilderInterface - */ - protected $formBuilder; - - /** * The search manager. * * @var \Drupal\search\SearchPluginManager @@ -53,13 +44,6 @@ class SearchPageListController extends DraggableListController implements FormIn protected $searchManager; /** - * The state service. - * - * @var \Drupal\Core\KeyValueStore\StateInterface - */ - protected $state; - - /** * Constructs a new SearchPageListController object. * * @param string $entity_type @@ -76,20 +60,14 @@ class SearchPageListController extends DraggableListController implements FormIn * The factory for configuration objects. * @param \Drupal\Core\Config\Context\ContextInterface $context * The configuration context to use. - * @param \Drupal\Core\Form\FormBuilderInterface $form_builder - * The form builder. - * @param \Drupal\Core\KeyValueStore\StateInterface $state - * The state service. */ - public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, SearchPluginManager $search_manager, ModuleHandlerInterface $module_handler, ConfigFactory $config_factory, ContextInterface $context, FormBuilderInterface $form_builder, StateInterface $state) { + public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, SearchPluginManager $search_manager, ModuleHandlerInterface $module_handler, ConfigFactory $config_factory, ContextInterface $context) { parent::__construct($entity_type, $entity_info, $storage, $module_handler); $this->configFactory = $config_factory; $this->configFactory->enterContext($context); - $this->formBuilder = $form_builder; $this->searchManager = $search_manager; - $this->state = $state; } /** @@ -103,9 +81,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ $container->get('plugin.manager.search'), $container->get('module_handler'), $container->get('config.factory'), - $container->get('config.context.free'), - $container->get('form_builder'), - $container->get('state') + $container->get('config.context.free') ); } @@ -119,13 +95,6 @@ public function getFormID() { /** * {@inheritdoc} */ - public function render() { - return $this->formBuilder->getForm($this); - } - - /** - * {@inheritdoc} - */ public function buildHeader() { $header['label'] = array( 'data' => $this->t('Label'), @@ -164,7 +133,16 @@ public function buildRow(EntityInterface $entity) { $definition = $entity->getPlugin()->getPluginDefinition(); $row['plugin']['#markup'] = $definition['title']; - $row['status']['#markup'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled'); + if ($entity->isDefaultSearch()) { + $status = $this->t('Default'); + } + elseif ($entity->status()) { + $status = $this->t('Enabled'); + } + else { + $status = $this->t('Disabled'); + } + $row['status']['#markup'] = $status; return $row + parent::buildRow($entity); } @@ -177,9 +155,7 @@ public function buildForm(array $form, array &$form_state) { // Collect some stats. $remaining = 0; $total = 0; - $options = array(); - foreach ($this->entities as $entity_id => $entity) { - $options[$entity_id] = $this->getLabel($entity); + foreach ($this->entities as $entity) { if ($entity->isIndexable() && $status = $entity->getPlugin()->indexStatus()) { $remaining += $status['remaining']; $total += $status['total']; @@ -242,12 +218,6 @@ public function buildForm(array $form, array &$form_state) { '#type' => 'details', '#title' => $this->t('Search pages'), ); - $form['search_pages']['default_page'] = array( - '#type' => 'select', - '#title' => $this->t('Default search page'), - '#options' => $options, - '#default_value' => $search_settings->get('default_page'), - ); $form['search_pages']['add_page'] = array( '#type' => 'container', '#attributes' => array( @@ -303,6 +273,16 @@ public function getOperations(EntityInterface $entity) { if ($entity->isDefaultSearch()) { unset($operations['disable'], $operations['delete']); } + else { + $operations['default'] = array( + 'title' => $this->t('Set as default'), + 'route_name' => 'search.set_default', + 'route_parameters' => array( + 'search_page' => $entity->id(), + ), + 'weight' => 50, + ); + } return $operations; } @@ -328,21 +308,8 @@ public function submitForm(array &$form, array &$form_state) { search_reindex(); } - // If the default search is disabled, enable it. - /** @var $default_search_page \Drupal\search\SearchPageInterface */ - $default_search_page = $this->storage->load($form_state['values']['default_page']); - if (!$default_search_page->status()) { - $default_search_page->enable()->save(); - } - - // If the default page is changing, rebuild the menu router. - if ($search_settings->get('default_page') != $form_state['values']['default_page']) { - $this->state->set('menu_rebuild_needed', TRUE); - } - $search_settings ->set('index.cron_limit', $form_state['values']['cron_limit']) - ->set('default_page', $form_state['values']['default_page']) ->save(); drupal_set_message($this->t('The configuration options have been saved.')); @@ -362,7 +329,7 @@ public function searchAdminReindexSubmit(array &$form, array &$form_state) { */ public function validateAddSearchPage(array &$form, array &$form_state) { if (empty($form_state['values']['search_type'])) { - $this->formBuilder->setErrorByName('search_type', $form_state, $this->t('You must select the new search page type.')); + $this->formBuilder()->setErrorByName('search_type', $form_state, $this->t('You must select the new search page type.')); } } @@ -378,14 +345,4 @@ public function submitAddSearchPage(array &$form, array &$form_state) { ); } - /** - * Returns all search entities. - * - * @return \Drupal\search\SearchPageInterface[] - * An array of search entities. - */ - public function load() { - return parent::load(); - } - } diff --git a/core/modules/search/lib/Drupal/search/SearchPageRepository.php b/core/modules/search/lib/Drupal/search/SearchPageRepository.php index 1c917ea..66c8688 100644 --- a/core/modules/search/lib/Drupal/search/SearchPageRepository.php +++ b/core/modules/search/lib/Drupal/search/SearchPageRepository.php @@ -100,6 +100,14 @@ public function clearDefaultSearchPage() { /** * {@inheritdoc} */ + public function setDefaultSearchPage(SearchPageInterface $search_page) { + $this->configFactory->get('search.settings')->set('default_page', $search_page->id())->save(); + $search_page->enable()->save(); + } + + /** + * {@inheritdoc} + */ public function sortSearchPages($search_pages) { $entity_info = $this->storage->entityInfo(); uasort($search_pages, array($entity_info['class'], 'sort')); diff --git a/core/modules/search/lib/Drupal/search/SearchPageRepositoryInterface.php b/core/modules/search/lib/Drupal/search/SearchPageRepositoryInterface.php index 5eb8e26..3bb3224 100644 --- a/core/modules/search/lib/Drupal/search/SearchPageRepositoryInterface.php +++ b/core/modules/search/lib/Drupal/search/SearchPageRepositoryInterface.php @@ -13,10 +13,10 @@ interface SearchPageRepositoryInterface { /** - * Returns all active search entities. + * Returns all active search page entities. * * @return \Drupal\search\SearchPageInterface[] - * An array of active search entities. + * An array of active search page entities. */ public function getActiveSearchPages(); @@ -29,10 +29,10 @@ public function getActiveSearchPages(); public function isSearchActive(); /** - * Returns all indexable search entities. + * Returns all indexable search page entities. * * @return \Drupal\search\SearchPageInterface[] - * An array of indexable search entities. + * An array of indexable search page entities. */ public function getIndexableSearchPages(); @@ -40,11 +40,21 @@ public function getIndexableSearchPages(); * Returns the default search page. * * @return \Drupal\search\SearchPageInterface|bool - * The search entity, or FALSE if no pages are active. + * The search page entity, or FALSE if no pages are active. */ public function getDefaultSearchPage(); /** + * Sets a given search page as the default. + * + * @param \Drupal\search\SearchPageInterface $search_page + * The search page entity. + * + * @return static + */ + public function setDefaultSearchPage(SearchPageInterface $search_page); + + /** * Clears the default search page. */ public function clearDefaultSearchPage(); diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php index 43af6f2..ae35fc5 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchConfigSettingsFormTest.php @@ -93,27 +93,8 @@ function testSearchSettingsPage() { * Verifies plugin-supplied settings form. */ function testSearchModuleSettingsPage() { - - // Test that the settings form displays the correct count of items left to index. $this->drupalGet('admin/config/search/settings'); - - // Ensure that the settings fieldset for the test plugin is not present on - // the page - $this->assertNoText(t('Extra type settings')); - $this->assertNoText(t('Boost method')); - - // Enable search for the test plugin - $edit = array( - 'default_page' => 'dummy_search_type', - 'minimum_word_size' => 5, - ); - $this->drupalPostForm('admin/config/search/settings', $edit, t('Save configuration')); - $this->assertTrue($this->xpath('//input[@id="edit-minimum-word-size" and @value="5"]'), 'Common search settings can be modified if a module-specific form is active'); - - // Ensure that the modifications took effect. - $this->assertText(t('The configuration options have been saved.')); - $this->assertText(t('Dummy search type')); - $this->drupalGet('admin/config/search/settings/manage/dummy_search_type'); + $this->clickLink(t('Edit'), 1); // Ensure that the default setting was picked up from the default config $this->assertTrue($this->xpath('//select[@id="edit-extra-type-settings-boost"]//option[@value="bi" and @selected="selected"]'), 'Module specific settings are picked up from the default config'); @@ -153,17 +134,15 @@ function testSearchModuleDisabling() { $plugins = array_keys($plugin_info); /** @var $entities \Drupal\search\SearchPageInterface[] */ $entities = entity_load_multiple('search_page'); + // Disable all of the search pages. foreach ($entities as $entity) { $entity->disable()->save(); } // Test each plugin if it's enabled as the only search plugin. foreach ($entities as $entity_id => $entity) { - $entity->enable()->save(); - // Enable the one plugin and disable other ones. - $edit = array(); - $edit['default_page'] = $entity_id; - $this->drupalPostForm('admin/config/search/settings', $edit, t('Save configuration')); + // Set this as default. + $this->drupalGet("admin/config/search/settings/manage/$entity_id/set-default"); // Run a search from the correct search URL. $info = $plugin_info[$entity_id]; @@ -198,13 +177,11 @@ function testSearchModuleDisabling() { // Test with all search plugins enabled. When you go to the search // page or run search, all plugins should be shown. - $edit = array(); foreach ($entities as $entity) { $entity->enable()->save(); } - $edit['default_page'] = 'node_search'; - - $this->drupalPostForm('admin/config/search/settings', $edit, t('Save configuration')); + // Set the node search as default. + $this->drupalGet('admin/config/search/settings/manage/node_search/set-default'); foreach (array('search/node/pizza', 'search/node') as $path) { $this->drupalGet($path); @@ -295,9 +272,8 @@ public function testMultipleSearchPages() { $this->assertSearchPageOperations($second_id, TRUE, TRUE, TRUE, FALSE); // Change the default search page. - $edit = array(); - $edit['default_page'] = $second_id; - $this->drupalPostForm(NULL, $edit, t('Save configuration')); + $this->clickLink(t('Set as default')); + $this->assertRaw(t('The default search page is now %label. Be sure to check the ordering of your search pages.', array('%label' => $second['label']))); $this->assertSearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE); $this->assertSearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE); @@ -367,8 +343,9 @@ protected function assertSearchPageOperations($id, $edit, $delete, $disable, $en * (optional) The group this message is in. */ protected function assertDefaultSearch($expected, $message = '', $group = 'Other') { - $search_settings = \Drupal::config('search.settings'); - $this->assertIdentical($search_settings->get('default_page'), $expected, $message, $group); + /** @var $search_page_repository \Drupal\search\SearchPageRepositoryInterface */ + $search_page_repository = \Drupal::service('search.search_page_repository'); + $this->assertIdentical($search_page_repository->getDefaultSearchPage(), $expected, $message, $group); } } diff --git a/core/modules/search/search.routing.yml b/core/modules/search/search.routing.yml index 235f8d1..8cdec79 100644 --- a/core/modules/search/search.routing.yml +++ b/core/modules/search/search.routing.yml @@ -46,6 +46,13 @@ search.disable: requirements: _entity_access: 'search_page.disable' +search.set_default: + path: '/admin/config/search/settings/manage/{search_page}/set-default' + defaults: + _controller: '\Drupal\search\Controller\SearchController::setAsDefault' + requirements: + _entity_access: 'search_page.update' + search.delete: path: '/admin/config/search/settings/manage/{search_page}/delete' defaults: @@ -54,4 +61,4 @@ search.delete: _entity_access: 'search_page.delete' route_callbacks: - - '\Drupal\search\Routing\SearchPluginRoutes::routes' + - '\Drupal\search\Routing\SearchPageRoutes::routes' diff --git a/core/modules/search/tests/Drupal/search/Tests/SearchPageRepositoryTest.php b/core/modules/search/tests/Drupal/search/Tests/SearchPageRepositoryTest.php index d56a67d..82309e7 100644 --- a/core/modules/search/tests/Drupal/search/Tests/SearchPageRepositoryTest.php +++ b/core/modules/search/tests/Drupal/search/Tests/SearchPageRepositoryTest.php @@ -231,6 +231,39 @@ public function testGetDefaultSearchPageWithInactiveDefault() { } /** + * Tests the setDefaultSearchPage() method. + */ + public function testSetDefaultSearchPage() { + $id = 'bananas'; + $config = $this->getMockBuilder('Drupal\Core\Config\Config') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->once()) + ->method('set') + ->with('default_page', $id) + ->will($this->returnValue($config)); + $config->expects($this->once()) + ->method('save') + ->will($this->returnValue($config)); + $this->configFactory->expects($this->once()) + ->method('get') + ->with('search.settings') + ->will($this->returnValue($config)); + + $search_page = $this->getMock('Drupal\search\SearchPageInterface'); + $search_page->expects($this->once()) + ->method('id') + ->will($this->returnValue($id)); + $search_page->expects($this->once()) + ->method('enable') + ->will($this->returnValue($search_page)); + $search_page->expects($this->once()) + ->method('save') + ->will($this->returnValue($search_page)); + $this->searchPageRepository->setDefaultSearchPage($search_page); + } + + /** * Tests the sortSearchPages() method. */ public function testSortSearchPages() { diff --git a/core/modules/search/tests/Drupal/search/Tests/SearchPluginBagTest.php b/core/modules/search/tests/Drupal/search/Tests/SearchPluginBagTest.php new file mode 100644 index 0000000..5ee70f0 --- /dev/null +++ b/core/modules/search/tests/Drupal/search/Tests/SearchPluginBagTest.php @@ -0,0 +1,90 @@ + 'Search plugin bag test', + 'description' => 'Tests the \Drupal\search\Plugin\SearchPluginBag class', + 'group' => 'Search', + ); + } + /** + * {@inheritdoc} + */ + protected function setUp() { + $this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface'); + $this->searchPluginBag = new SearchPluginBag($this->pluginManager, array('banana'), array('id' => 'banana', 'color' => 'yellow'), 'fruit_stand'); + } + + /** + * Tests the get() method. + */ + public function testGet() { + $plugin = $this->getMock('Drupal\search\Plugin\SearchInterface'); + $this->pluginManager->expects($this->once()) + ->method('createInstance') + ->will($this->returnValue($plugin)); + $this->assertSame($plugin, $this->searchPluginBag->get('banana')); + } + + /** + * Tests the get() method with a configurable plugin. + */ + public function testGetWithConfigurablePlugin() { + $plugin = $this->getMock('Drupal\search\Plugin\ConfigurableSearchPluginInterface'); + $plugin->expects($this->once()) + ->method('setSearchPageId') + ->with('fruit_stand') + ->will($this->returnValue($plugin)); + + $this->pluginManager->expects($this->once()) + ->method('createInstance') + ->will($this->returnValue($plugin)); + + $this->assertSame($plugin, $this->searchPluginBag->get('banana')); + } + +} diff --git a/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php b/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php index b5a47a5..920f14b 100644 --- a/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php +++ b/core/modules/search/tests/modules/search_extra_type/lib/Drupal/search_extra_type/Plugin/Search/SearchExtraTypeSearch.php @@ -7,8 +7,6 @@ namespace Drupal\search_extra_type\Plugin\Search; -use Drupal\Core\Annotation\Translation; -use Drupal\search\Annotation\SearchPlugin; use Drupal\search\Plugin\ConfigurableSearchPluginBase; /** diff --git a/core/modules/user/lib/Drupal/user/Plugin/Search/UserSearch.php b/core/modules/user/lib/Drupal/user/Plugin/Search/UserSearch.php index 165abaa..518d9c0 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Search/UserSearch.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Search/UserSearch.php @@ -7,16 +7,13 @@ namespace Drupal\user\Plugin\Search; -use Drupal\Core\Annotation\Translation; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Access\AccessibleInterface; -use Drupal\search\Annotation\SearchPlugin; use Drupal\search\Plugin\SearchPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; /** * Executes a keyword search for users against the {users} database table.