diff --git a/search_api_page.services.yml b/search_api_page.services.yml new file mode 100644 index 0000000..b419c65 --- /dev/null +++ b/search_api_page.services.yml @@ -0,0 +1,6 @@ +services: + path_processor.search_api_page: + class: Drupal\search_api_page\PathProcessor\PathProcessorSearchApiPage + arguments: ['@entity_type.manager', '@language_manager', '@config.factory'] + tags: + - { name: path_processor_inbound, priority: 300 } \ No newline at end of file diff --git a/src/PathProcessor/PathProcessorSearchApiPage.php b/src/PathProcessor/PathProcessorSearchApiPage.php new file mode 100644 index 0000000..ffc7ef9 --- /dev/null +++ b/src/PathProcessor/PathProcessorSearchApiPage.php @@ -0,0 +1,84 @@ +entityTypeManager = $entityTypeManager; + $this->languageManager = $languageManager; + $this->config = $config; + } + + public function processInbound($path, Request $request) { + foreach ($this->getSearchApiPagePathsUsingCleanUrls() as $search_api_clean_url_path) { + $regex = '~^' . $search_api_clean_url_path . '~'; + if (preg_match($regex, $path)) { + $keys = str_replace($search_api_clean_url_path, '', $path); + return $search_api_clean_url_path . urlencode($keys); + } + } + return $path; + } + + protected function getSearchApiPagePathsUsingCleanUrls() { + $paths = []; + $is_multilingual = $this->languageManager->isMultilingual(); + + /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */ + foreach ($this->entityTypeManager->getStorage('search_api_page')->loadMultiple() as $search_api_page) { + // Default path. + $default_path = $search_api_page->getPath(); + + // Loop over all languages so we can get the translated path (if any). + foreach ($this->languageManager->getLanguages() as $language) { + $path = ''; + + // Check if we are multilingual or not. + if ($is_multilingual) { + $path = $this->languageManager + ->getLanguageConfigOverride($language->getId(), 'search_api_page.search_api_page.' . $search_api_page->id()) + ->get('path'); + } + if (empty($path)) { + $path = $default_path; + } + + // Use clean urls or not. + if ($search_api_page->getCleanUrl()) { + $path .= '/'; + $paths[] = '/' . $path; + } + } + } + + return $paths; + } + +} \ No newline at end of file diff --git a/src/Routing/SearchApiPageRoutes.php b/src/Routing/SearchApiPageRoutes.php index 9738962..6880135 100644 --- a/src/Routing/SearchApiPageRoutes.php +++ b/src/Routing/SearchApiPageRoutes.php @@ -101,6 +101,7 @@ class SearchApiPageRoutes implements ContainerInjectionInterface { $args, array( '_permission' => 'view search api pages', + 'keys' => '.*', ) ); }