diff --git a/config/install/search404.settings.yml b/config/install/search404.settings.yml index a968acc..e030692 100644 --- a/config/install/search404.settings.yml +++ b/config/install/search404.settings.yml @@ -18,3 +18,4 @@ search404_page_title: Page not found search404_ignore: and or the search404_custom_error_message: '' search404_page_redirect: '' +search404_ignore_paths : '' diff --git a/config/schema/search404.schema.yml b/config/schema/search404.schema.yml index abd3061..1202b6f 100644 --- a/config/schema/search404.schema.yml +++ b/config/schema/search404.schema.yml @@ -36,3 +36,9 @@ search404.settings: type: string search404_page_redirect: type: string + search404_page_title: + type: string + search404_ignore: + type: string + search404_ignore_paths: + type: string diff --git a/src/Controller/Search404Controller.php b/src/Controller/Search404Controller.php index c4c3514..de72aa0 100644 --- a/src/Controller/Search404Controller.php +++ b/src/Controller/Search404Controller.php @@ -8,7 +8,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Drupal\search\Entity\SearchPage; use Drupal\Component\Utility\Html; - /** * Route controller for search. */ @@ -56,6 +55,31 @@ class Search404Controller extends ControllerBase { */ public function search404Page(Request $request) { $keys = $this->search404GetKeys(); + + // If the current path is set as one of the ignore path, then do not get into + // the complex search functions. + $paths_to_ignore = \Drupal::config('search404.settings')->get('search404_ignore_paths'); + if (!empty($paths_to_ignore)) { + $path_array = preg_split('/\R/', $paths_to_ignore); + // If OR case enabled. + if (\Drupal::config('search404.settings')->get('search404_use_or')) { + $keywords = str_replace(' OR ', '/', $keys); + } + else { + $keywords = str_replace(' ', '/', $keys); + } + foreach ($path_array as $key => $path) { + $ignore_paths[$key] = preg_replace('[ |-]', '/', $path); + } + // If the page matches to any of the listed paths to ignore, + // then return default drupal 404 page title and text. + if (in_array($keywords, $ignore_paths)) { + $build['#title'] = 'Page not found'; + $build['#markup'] = 'The requested page could not be found.'; + return $build; + } + } + if (\Drupal::moduleHandler()->moduleExists('search') && (\Drupal::currentUser()->hasPermission('search content') || \Drupal::currentUser()->hasPermission('search by page'))) { // Get and use the default search engine for the site. diff --git a/src/Form/Search404Settings.php b/src/Form/Search404Settings.php index a7d1084..74d8052 100644 --- a/src/Form/Search404Settings.php +++ b/src/Form/Search404Settings.php @@ -122,6 +122,12 @@ class Search404Settings extends ConfigFormBase { '#description' => t('These words will be ignored from the search query. Separate words with a space, e.g.: "and or the".'), '#default_value' => \Drupal::config('search404.settings')->get('search404_ignore'), ); + $form['advanced']['search404_ignore_paths'] = array( + '#type' => 'textarea', + '#title' => t('Specific paths to ignore'), + '#description' => t('These paths will be ignored. Site default "Page not found" page will be displayed. Enter one path per line. The "*" character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog.'), + '#default_value' => \Drupal::config('search404.settings')->get('search404_ignore_paths', ''), + ); $form['advanced']['search404_ignore_extensions'] = array( '#type' => 'textfield', '#title' => t('Extensions to ignore'), @@ -223,6 +229,7 @@ class Search404Settings extends ConfigFormBase { ->set('search404_jump', $form_state->getValue('search404_jump')) ->set('search404_use_or', $form_state->getValue('search404_use_or')) ->set('search404_ignore', $form_state->getValue('search404_ignore')) + ->set('search404_ignore_paths', $form_state->getValue('search404_ignore_paths')) ->set('search404_ignore_query', $form_state->getValue('search404_ignore_query')) ->set('search404_ignore_extensions', $form_state->getValue('search404_ignore_extensions')) ->set('search404_page_text', $form_state->getValue('search404_page_text'))