Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / redirect_404.module
1 <?php
2
3 /**
4  * @file
5  * Module file for redirect_404.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10 use Drupal\redirect\Entity\Redirect;
11
12 /**
13  * Implements hook_cron().
14  *
15  * Adds clean up job to drop the irrelevant rows from the redirect_404 table.
16  */
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();
21 }
22
23 /**
24  * Implements hook_form_FORM_ID_alter() for system_logging_settings().
25  */
26 function redirect_404_form_redirect_settings_form_alter(&$form, FormStateInterface $form_state, $form_id) {
27   $config = \Drupal::configFactory()->getEditable('redirect_404.settings');
28
29   $row_limits = [100, 1000, 10000, 100000, 1000000];
30   $form['row_limit'] = [
31     '#type' => 'select',
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()])
36   ];
37
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;
42   }
43
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>',
51     ]),
52   ];
53
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."),
59   ];
60
61   $form['#submit'][] = 'redirect_404_logging_settings_submit';
62 }
63
64 /**
65  * Form submission handler for system_logging_settings().
66  *
67  * @see redirect_404_form_redirect_settings_form_alter()
68  */
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'));
72   $pages = '';
73   foreach ($ignore_pages as $page) {
74     if (!empty($page)) {
75       $pages .= '/' . ltrim($page, '/') . "\n";
76     }
77   }
78
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'))
84     ->save();
85 }
86
87 /**
88  * Implements hook_ENTITY_TYPE_presave() for redirect entities.
89  */
90 function redirect_404_redirect_presave(Redirect $redirect) {
91   $path = $redirect->getSourcePathWithQuery();
92   $langcode = $redirect->get('language')->value;
93
94   // Mark a potentially existing log entry for this path as resolved.
95   \Drupal::service('redirect.not_found_storage')->resolveLogRequest($path, $langcode);
96 }