diff --git a/config/schema/search_api_page.schema.yml b/config/schema/search_api_page.schema.yml index 09e913d..bba7f18 100644 --- a/config/schema/search_api_page.schema.yml +++ b/config/schema/search_api_page.schema.yml @@ -46,6 +46,9 @@ search_api_page.search_api_page.*: show_search_form: type: boolean label: 'Show search form' + parse_mode: + type: string + label: 'Parse mode' uuid: type: string diff --git a/src/Controller/SearchApiPageController.php b/src/Controller/SearchApiPageController.php index c934bf5..6b0deb3 100644 --- a/src/Controller/SearchApiPageController.php +++ b/src/Controller/SearchApiPageController.php @@ -10,7 +10,6 @@ use Drupal\search_api\ParseMode\ParseModePluginManager; use Drupal\search_api\Query\ResultSetInterface; use Drupal\search_api\SearchApiException; use Drupal\search_api_page\Entity\SearchApiPage; -use Drupal\search_api_page\Form\SearchApiPageBlockForm; use Drupal\search_api_page\SearchApiPageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -144,12 +143,13 @@ class SearchApiPageController extends ControllerBase { protected function createItemRenderArray(ItemInterface $item, SearchApiPageInterface $search_api_page) { try { $originalObject = $item->getOriginalObject(); - if ($originalObject === null) { + if ($originalObject === NULL) { return []; } /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $originalObject->getValue(); - } catch (SearchApiException $e) { + } + catch (SearchApiException $e) { return []; } @@ -231,7 +231,7 @@ class SearchApiPageController extends ControllerBase { $query->setSearchID('search_api_page:' . $search_api_page->id()); /** @var \Drupal\search_api\ParseMode\ParseModeInterface $parse_mode */ - $parse_mode = $this->parseModePluginManager->createInstance('direct'); + $parse_mode = $this->parseModePluginManager->createInstance($search_api_page->getParseMode()); $query->setParseMode($parse_mode); // Add filter for current language. @@ -240,7 +240,7 @@ class SearchApiPageController extends ControllerBase { ->getId(); $query->setLanguages([ $langcode, - LanguageInterface::LANGCODE_NOT_SPECIFIED + LanguageInterface::LANGCODE_NOT_SPECIFIED, ]); $query->setFulltextFields($search_api_page->getSearchedFields()); diff --git a/src/Entity/SearchApiPage.php b/src/Entity/SearchApiPage.php index 5b27fa7..a865170 100644 --- a/src/Entity/SearchApiPage.php +++ b/src/Entity/SearchApiPage.php @@ -116,6 +116,13 @@ class SearchApiPage extends ConfigEntityBase implements SearchApiPageInterface { */ protected $show_search_form = TRUE; + /** + * The query parse mode. + * + * @var string + */ + protected $parse_mode = 'direct'; + /** * {@inheritdoc} */ @@ -244,6 +251,13 @@ class SearchApiPage extends ConfigEntityBase implements SearchApiPageInterface { return new ViewMode($this->view_mode_configuration); } + /** + * {@inheritdoc} + */ + public function getParseMode() { + return $this->parse_mode; + } + /** * {@inheritdoc} */ diff --git a/src/Form/SearchApiPageForm.php b/src/Form/SearchApiPageForm.php index 1081b2e..8ae10a2 100644 --- a/src/Form/SearchApiPageForm.php +++ b/src/Form/SearchApiPageForm.php @@ -164,6 +164,25 @@ class SearchApiPageForm extends EntityForm { '#states' => $default_index_states, ]; + $plugin_manager = \Drupal::service('plugin.manager.search_api.parse_mode'); + $instances = $plugin_manager->getInstances(); + $options = []; + foreach ($instances as $name => $instance) { + if ($instance->isHidden()) { + continue; + } + $options[$name] = $instance->label(); + } + + $form['page_fieldset']['parse_mode'] = [ + '#type' => 'select', + '#title' => $this->t('Parse mode'), + '#description' => $this->t('Parse mode for search keywords'), + '#options' => $options, + '#default_value' => $search_api_page->getParseMode(), + '#required' => TRUE, + ]; + if (empty($default_index)) { return $form; } @@ -175,7 +194,7 @@ class SearchApiPageForm extends EntityForm { '#states' => [ 'visible' => [ ':input[name="style"]' => ['value' => 'view_modes'], - ':input[name="index"]' => ['value' => $default_index] + ':input[name="index"]' => ['value' => $default_index], ], ], ]; diff --git a/src/SearchApiPageInterface.php b/src/SearchApiPageInterface.php index 9c691b7..8ff8671 100644 --- a/src/SearchApiPageInterface.php +++ b/src/SearchApiPageInterface.php @@ -113,4 +113,12 @@ interface SearchApiPageInterface extends ConfigEntityInterface { */ public function getViewModeConfig(); + /** + * The parse mode to use for query keywords. + * + * @return string + * Can be any ID of a parse mode plugin. + */ + public function getParseMode(); + } diff --git a/tests/src/Functional/ParseModeTest.php b/tests/src/Functional/ParseModeTest.php new file mode 100644 index 0000000..e44b772 --- /dev/null +++ b/tests/src/Functional/ParseModeTest.php @@ -0,0 +1,64 @@ +drupalLogin($this->adminUser); + $this->setupSearchApi(); + } + + /** + * Test the parse mode configuration in the admin form of a Search API Page. + */ + public function testAdminFormParseMode() { + $assert_session = $this->assertSession(); + + // Create a Search API Page and verify that it exists. + $step1 = [ + 'label' => 'Search Page Test', + 'id' => 'search_page_test', + 'index' => $this->index->id(), + ]; + $this->drupalPostForm('admin/config/search/search-api-pages/add', $step1, 'Next'); + $step2 = [ + 'path' => 'search-page-test', + ]; + $this->drupalPostForm(NULL, $step2, 'Save'); + $this->drupalGet('admin/config/search/search-api-pages'); + $assert_session->statusCodeEquals(200); + $assert_session->pageTextContains('Search Page Test'); + + // Test whether all parse mode plugins can be chosen. + $this->drupalGet('admin/config/search/search-api-pages/search_page_test'); + $assert_session->statusCodeEquals(200); + $assert_session->pageTextContains('Parse mode for search keywords'); + + $plugin_manager = \Drupal::service('plugin.manager.search_api.parse_mode'); + $instances = $plugin_manager->getInstances(); + foreach ($instances as $name => $instance) { + $assert_session->responseContains($name); + } + + // Test whether the field can be filled and submitted. + $edit = [ + 'parse_mode' => 'terms', + ]; + $this->drupalPostForm('admin/config/search/search-api-pages/search_page_test', $edit, 'Save'); + $this->drupalGet('admin/config/search/search-api-pages/search_page_test'); + $assert_session->statusCodeEquals(200); + $assert_session->responseContains('terms'); + } + +}