5 * Module file for redirect_404.
8 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\redirect\Entity\Redirect;
13 * Implements hook_cron().
15 * Adds clean up job to drop the irrelevant rows from the redirect_404 table.
17 function redirect_404_cron() {
18 /** @var \Drupal\redirect_404\SqlRedirectNotFoundStorage $redirect_storage */
19 $redirect_storage = \Drupal::service('redirect.not_found_storage');
20 $redirect_storage->purgeOldRequests();
24 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
26 function redirect_404_form_redirect_settings_form_alter(&$form, FormStateInterface $form_state, $form_id) {
27 $config = \Drupal::configFactory()->getEditable('redirect_404.settings');
29 $row_limits = [100, 1000, 10000, 100000, 1000000];
30 $form['row_limit'] = [
32 '#title' => t('404 error database logs to keep'),
33 '#default_value' => $config->get('row_limit'),
34 '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
35 '#description' => t('The maximum number of 404 error logs to keep in the database log. Requires a <a href=":cron">cron maintenance task</a>.', [':cron' => Url::fromRoute('system.status')->toString()])
38 $ignored_pages = $config->get('pages');
39 // Add a new path to be ignored, if there is an ignore argument in the query.
40 if ($path_to_ignore = \Drupal::request()->query->get('ignore')) {
41 $ignored_pages .= "\n" . $path_to_ignore;
44 $form['ignore_pages'] = [
45 '#type' => 'textarea',
46 '#title' => t('Pages to ignore'),
47 '#default_value' => $ignored_pages,
48 '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [
49 '%user-wildcard' => '/user/*',
50 '%front' => '<front>',
54 $form['suppress_404'] = [
55 '#type' => 'checkbox',
56 '#title' => t("Suppress 'page not found' log messages"),
57 '#default_value' => $config->get('suppress_404'),
58 '#description' => t("Prevents logging 'page not found' events. Can be safely enabled when redirect_404 module is used, which stores them separately, nothing else relies on those messages."),
61 $form['#submit'][] = 'redirect_404_logging_settings_submit';
65 * Form submission handler for system_logging_settings().
67 * @see redirect_404_form_redirect_settings_form_alter()
69 function redirect_404_logging_settings_submit($form, FormStateInterface $form_state) {
70 // Make sure to store the 'pages to ignore' with the leading slash.
71 $ignore_pages = explode(PHP_EOL, $form_state->getValue('ignore_pages'));
73 foreach ($ignore_pages as $page) {
75 $pages .= '/' . ltrim($page, '/') . "\n";
79 \Drupal::configFactory()
80 ->getEditable('redirect_404.settings')
81 ->set('row_limit', $form_state->getValue('row_limit'))
82 ->set('pages', $pages)
83 ->set('suppress_404', $form_state->getValue('suppress_404'))
88 * Implements hook_ENTITY_TYPE_presave() for redirect entities.
90 function redirect_404_redirect_presave(Redirect $redirect) {
91 $path = $redirect->getSourcePathWithQuery();
92 $langcode = $redirect->get('language')->value;
94 // Mark a potentially existing log entry for this path as resolved.
95 \Drupal::service('redirect.not_found_storage')->resolveLogRequest($path, $langcode);