public function Search404Settings::buildForm in Search 404 8
Same name and namespace in other branches
- 2.x src/Form/Search404Settings.php \Drupal\search404\Form\Search404Settings::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ Search404Settings.php, line 30
Class
- Search404Settings
- Configure settings for search404.
Namespace
Drupal\search404\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form['search404_jump'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Jump directly to the search result when there is only one result'),
'#description' => $this
->t('Works only with Core, Apache Solr, Lucene and Xapian searches. An HTTP status of 301 or 302 will be returned for this redirect.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_jump'),
];
$form['search404_first'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Jump directly to the first search result even when there are multiple results'),
'#description' => $this
->t('Works only with Core, Apache Solr, Lucene and Xapian searches. An HTTP status of 301 or 302 will be returned for this redirect.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_first'),
];
$form['search404_first_on_paths'] = [
'#type' => 'textarea',
'#title' => t('Jump directly to the first search result only on the listed paths.'),
'#description' => t('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_first_on_paths'),
];
$form['search404_do_google_cse'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Do a Google CSE Search instead of a Drupal Search when a 404 occurs'),
'#description' => $this
->t('Requires Google CSE and Google CSE Search modules to be enabled.'),
'#attributes' => \Drupal::moduleHandler()
->moduleExists('google_cse') ? [] : [
'disabled' => 'disabled',
],
'#default_value' => \Drupal::config('search404.settings')
->get('search404_do_google_cse'),
];
$form['search404_do_search_by_page'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Do a "Search by page" Search instead of a Drupal Search when a 404 occurs'),
'#description' => $this
->t('Requires "Search by page" module to be enabled.'),
'#attributes' => \Drupal::moduleHandler()
->moduleExists('search_by_page') ? [] : [
'disabled' => 'disabled',
],
'#default_value' => \Drupal::config('search404.settings')
->get('search404_do_search_by_page'),
];
// Custom search path implementation.
$form['search404_do_custom_search'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Do a "Search" with custom path instead of a Drupal Search when a 404 occurs'),
'#description' => $this
->t('Redirect the user to a Custom search path to be entered below. Can be used to open a view with path parameter.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_do_custom_search'),
];
$form['search404_custom_search_path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom search path'),
'#description' => $this
->t('The custom search path: example: myownsearch/@keys. The token "@keys" will be replaced with the search keys from the URL.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_custom_search_path'),
'#required' => !empty($form_state
->getValue("search404_do_custom_search")) && $form_state
->getValue("search404_do_custom_search") == TRUE ? TRUE : FALSE,
'#states' => [
"visible" => [
"input[name='search404_do_custom_search']" => [
"checked" => TRUE,
],
],
],
];
// Added for having a 301 redirect instead of the standard 302
// (offered by the drupal_goto) than Core, Apache Solr,
// Lucene and Xapian. Can this even be done? Meta refresh?
$form['search404_redirect_301'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use a 301 Redirect instead of 302 Redirect'),
'#description' => $this
->t('This applies when the option to jump to first result is enabled and also for search404 results pages other than for Core, Apache Solr, Lucene and Xapian.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_redirect_301'),
];
// Added for preventing automatic search for large sites.
$form['search404_skip_auto_search'] = [
'#title' => $this
->t('Disable auto search'),
'#description' => $this
->t('Disable automatically searching for the keywords when a page is not found and instead show the populated search form with the keywords. Useful for large sites to reduce server loads.'),
'#type' => 'checkbox',
'#default_value' => \Drupal::config('search404.settings')
->get('search404_skip_auto_search'),
];
// Disable the drupal error message when showing search results.
$form['search404_disable_error_message'] = [
'#title' => $this
->t('Disable error message'),
'#type' => 'checkbox',
'#description' => $this
->t('Disable the Drupal error message when search results are shown on a 404 page.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_disable_error_message'),
];
// To add custom error message.
$form['search404_custom_error_message'] = [
'#title' => $this
->t('Custom error message'),
'#type' => 'textfield',
'#placeholder' => 'For example, Invalid search for @keys, Sorry the page does not exist, etc.',
'#description' => $this
->t('A custom error message instead of default Drupal message, that should be displayed when search results are shown on a 404 page, use "@keys" to insert the searched key value if necessary.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_custom_error_message'),
];
$form['advanced'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['advanced']['search404_use_or'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use OR between keywords when searching'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_use_or'),
];
$form['advanced']['search404_use_search_engine'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use auto-detection of keywords from search engine referer'),
'#description' => $this
->t('This feature will conduct a search based on the query string got from a search engine if the URL of the search result points to a 404 page in the current website. Currently supported search engines: Google, Yahoo, Altavista, Lycos, Bing and AOL.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_use_search_engine'),
];
$form['advanced']['search404_ignore'] = [
'#type' => 'textarea',
'#title' => $this
->t('Words to ignore'),
'#description' => $this
->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'] = [
'#type' => 'textarea',
'#title' => $this
->t('Specific paths to ignore'),
'#description' => $this
->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'] = [
'#type' => 'textfield',
'#title' => $this
->t('Extensions to ignore'),
'#description' => $this
->t('These extensions will be ignored from the search query, e.g.: http://www.example.com/invalid/page.php will only search for "invalid page". Separate extensions with a space, e.g.: "htm html php". Do not include leading dot.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_ignore_extensions'),
];
$form['advanced']['search404_ignore_query'] = [
'#type' => 'textfield',
'#title' => $this
->t('Extensions to abort search'),
'#description' => $this
->t('A search will not be performed for a query ending in these extensions. Separate extensions with a space, e.g.: "gif jpg jpeg bmp png". Do not include leading dot.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_ignore_query'),
];
$form['advanced']['search404_regex'] = [
'#type' => 'textfield',
'#title' => $this
->t('PCRE filter'),
'#description' => $this
->t('This regular expression will be applied to filter all queries. The parts of the path that match the expression will be EXCLUDED from the search. You do NOT have to enclose the regex in forward slashes when defining the PCRE. e.g.: use "[foo]bar" instead of "/[foo]bar/". On how to use a PCRE Regex please refer <a href="http://php.net/pcre">PCRE pages in the PHP Manual</a>.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_regex'),
];
// Show custom title for the 404 search results page.
$form['advanced']['search404_page_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom page title'),
'#description' => $this
->t('You can enter a value that will be displayed at the title of the webpage e.g. "Page not found".'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_page_title'),
];
// Show custom text below the search form for the 404 search
// results page.
$form['advanced']['search404_page_text'] = [
'#type' => 'textarea',
'#title' => $this
->t('Custom page text'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_page_text'),
'#description' => $this
->t('You can enter a custom text message that can be displayed at the top of the search results, HTML formatting can be used.'),
];
// Add a redirect url option for handling empty results display.
$form['advanced']['search404_page_redirect'] = [
'#title' => $this
->t('Add a redirection url for empty search results.'),
'#type' => 'textfield',
'#placeholder' => 'For example, /node, /node/10, etc.',
'#description' => $this
->t('You can enter a valid url with a leading "/" to display instead of an empty result.'),
'#default_value' => \Drupal::config('search404.settings')
->get('search404_page_redirect'),
];
// Helps reset the site_404 variable to search404 in case the
// user changes it manually.
$form['site_404'] = [
'#type' => 'hidden',
'#value' => 'search404',
];
// Tell the user about the site_404 issue.
$form['search404_variable_message'] = [
'#type' => 'markup',
'#prefix' => '<p>',
'#suffix' => '</p>',
'#value' => $this
->t('Saving this form will revert the 404 handling on the site to this module.'),
];
return parent::buildForm($form, $form_state);
}