Chris@0: ' . t('About') . ''; Chris@0: $output .= '
' . t('The Database Logging module logs system events in the Drupal database. For more information, see the online documentation for the Database Logging module.', [':dblog' => 'https://www.drupal.org/documentation/modules/dblog']) . '
'; Chris@0: $output .= '' . t('The Database Logging module logs system events in the Drupal database. Monitor your site or debug site problems on this page.') . '
'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_menu_links_discovered_alter(). Chris@0: */ Chris@0: function dblog_menu_links_discovered_alter(&$links) { Chris@0: if (\Drupal::moduleHandler()->moduleExists('search')) { Chris@0: $links['dblog.search'] = [ Chris@0: 'title' => new TranslatableMarkup('Top search phrases'), Chris@0: 'route_name' => 'dblog.search', Chris@0: 'description' => new TranslatableMarkup('View most popular search phrases.'), Chris@0: 'parent' => 'system.admin_reports', Chris@0: ]; Chris@0: } Chris@0: Chris@0: return $links; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_cron(). Chris@0: * Chris@0: * Controls the size of the log table, paring it to 'dblog_row_limit' messages. Chris@0: */ Chris@0: function dblog_cron() { Chris@0: // Cleanup the watchdog table. Chris@0: $row_limit = \Drupal::config('dblog.settings')->get('row_limit'); Chris@0: Chris@0: // For row limit n, get the wid of the nth row in descending wid order. Chris@0: // Counting the most recent n rows avoids issues with wid number sequences, Chris@0: // e.g. auto_increment value > 1 or rows deleted directly from the table. Chris@0: if ($row_limit > 0) { Chris@18: $connection = \Drupal::database(); Chris@18: $min_row = $connection->select('watchdog', 'w') Chris@0: ->fields('w', ['wid']) Chris@0: ->orderBy('wid', 'DESC') Chris@0: ->range($row_limit - 1, 1) Chris@0: ->execute()->fetchField(); Chris@0: Chris@0: // Delete all table entries older than the nth row, if nth row was found. Chris@0: if ($min_row) { Chris@18: $connection->delete('watchdog') Chris@0: ->condition('wid', $min_row, '<') Chris@0: ->execute(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gathers a list of uniquely defined database log message types. Chris@0: * Chris@0: * @return array Chris@0: * List of uniquely defined database log message types. Chris@0: */ Chris@0: function _dblog_get_message_types() { Chris@0: return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type') Chris@0: ->fetchAllKeyed(0, 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_form_FORM_ID_alter() for system_logging_settings(). Chris@0: */ Chris@0: function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) { Chris@0: $row_limits = [100, 1000, 10000, 100000, 1000000]; Chris@0: $form['dblog_row_limit'] = [ Chris@0: '#type' => 'select', Chris@0: '#title' => t('Database log messages to keep'), Chris@0: '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'), Chris@0: '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits), Chris@18: '#description' => t('The maximum number of messages to keep in the database log. Requires a cron maintenance task.', [':cron' => Url::fromRoute('system.status')->toString()]), Chris@0: ]; Chris@0: Chris@0: $form['#submit'][] = 'dblog_logging_settings_submit'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form submission handler for system_logging_settings(). Chris@0: * Chris@0: * @see dblog_form_system_logging_settings_alter() Chris@0: */ Chris@0: function dblog_logging_settings_submit($form, FormStateInterface $form_state) { Chris@0: \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_ENTITY_TYPE_presave() for views entities. Chris@0: * Chris@0: * This hook ensures there are no views based that are using a wrong plugin for Chris@0: * wid and uid fields on views that use watchdog as base table. Chris@0: * Chris@0: * @deprecated in Drupal 8.4.x and will be removed before 9.0.0. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2876378 Chris@0: */ Chris@0: function dblog_view_presave(ViewEntityInterface $view) { Chris@0: // Only interested in watchdog based views. Chris@0: if ($view->get('base_table') != 'watchdog') { Chris@0: return; Chris@0: } Chris@0: Chris@0: $displays = $view->get('display'); Chris@0: $update = FALSE; Chris@0: foreach ($displays as $display_name => $display) { Chris@0: Chris@0: // Iterate through all the fields of watchdog views based tables. Chris@0: if (isset($display['display_options']['fields'])) { Chris@0: foreach ($display['display_options']['fields'] as $field_name => $field) { Chris@0: // We are only interested in wid and uid fields from the watchdog table Chris@0: // that still use the numeric id. Chris@0: if (isset($field['table']) && Chris@0: $field['table'] === 'watchdog' && Chris@0: $field['plugin_id'] == 'numeric' && Chris@0: in_array($field['field'], ['wid', 'uid'], TRUE)) { Chris@0: Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['plugin_id'] = 'standard'; Chris@0: Chris@0: // Delete all the attributes related to numeric fields. Chris@0: unset( Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['set_precision'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['precision'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['decimal'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['separator'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['format_plural'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['format_plural_string'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['prefix'], Chris@0: $displays[$display_name]['display_options']['fields'][$field_name]['suffix'] Chris@0: ); Chris@0: Chris@0: $update = TRUE; Chris@0: @trigger_error("The numeric plugin for watchdog.$field_name field is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use standard plugin instead. See https://www.drupal.org/node/2876378.", E_USER_DEPRECATED); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Iterate all filters looking for type filters to update. Chris@0: if (isset($display['display_options']['filters'])) { Chris@0: foreach ($display['display_options']['filters'] as $filter_name => $filter) { Chris@0: if (isset($filter['table']) && Chris@0: $filter['table'] === 'watchdog' && Chris@0: $filter['plugin_id'] == 'in_operator' && Chris@0: $filter['field'] == 'type') { Chris@0: Chris@0: $displays[$display_name]['display_options']['filters'][$filter_name]['plugin_id'] = 'dblog_types'; Chris@0: $update = TRUE; Chris@0: @trigger_error("The in_operator plugin for watchdog.type filter is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use dblog_types plugin instead. See https://www.drupal.org/node/2876378.", E_USER_DEPRECATED); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if ($update) { Chris@0: $view->set('display', $displays); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_views_pre_render(). Chris@0: */ Chris@0: function dblog_views_pre_render(ViewExecutable $view) { Chris@0: if (isset($view) && ($view->storage->get('base_table') == 'watchdog')) { Chris@0: $view->element['#attached']['library'][] = 'dblog/drupal.dblog'; Chris@0: } Chris@0: }