diff --git a/core/modules/dblog/dblog.module b/core/modules/dblog/dblog.module index ed92d6c..7295b4a 100644 --- a/core/modules/dblog/dblog.module +++ b/core/modules/dblog/dblog.module @@ -12,6 +12,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\views\Entity\ViewEntityInterface; /** * Implements hook_help(). @@ -115,3 +116,66 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo function dblog_logging_settings_submit($form, FormStateInterface $form_state) { \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save(); } + +/** + * Implements hook_ENTITY_TYPE_presave() for views entities. + * + * This hook ensures there are no views based that are using a wrong plugin for + * wid and uid fields on views that use watchdog as base table. + */ +function dblog_view_presave(ViewEntityInterface $view) { + // Only interested in watchdog based views. + if ($view->get('base_table') != 'watchdog') { + return; + } + + foreach ($view->get('display') as $display_name => $display) { + // Iterate through all the fields of watchdog views based tables. + if (isset($display['display_options']['fields'])) { + foreach ($display['display_options']['fields'] as $field_name => $field) { + // We are only interested in wid and uid fields from the watchdog table + // that still use the numeric id. + if (isset($field['table']) && + $field['table'] === 'watchdog' && + $field['plugin_id'] == 'numeric' && + in_array($field['field'], ['wid', 'uid'])) { + + $new_value = $field; + $new_value['plugin_id'] = 'standard'; + + // Delete all the attributes related to numeric fields. + unset( + $new_value['set_precision'], + $new_value['precision'], + $new_value['decimal'], + $new_value['separator'], + $new_value['format_plural'], + $new_value['format_plural_string'], + $new_value['prefix'], + $new_value['suffix'] + ); + foreach ($numeric_attributes_to_delete as $key) { + if (isset($new_value[$key])) { + unset($new_value[$key]); + } + } + $view->set("display.$display_name.display_options.fields.$field_name", $new_value); + } + } + } + + // Iterate all filters looking for type filters to update. + if (isset($display['display_options']['filters'])) { + foreach ($display['display_options']['filters'] as $filter_name => $filter) { + if (isset($filter['table']) && + $filter['table'] === 'watchdog' && + $filter['plugin_id'] == 'in_operator' && + $filter['field'] == 'type') { + + $filter['plugin_id'] = 'dblog_types'; + $view->set("display.$display_name.display_options.filters.$filter_name", $filter); + } + } + } + } +} diff --git a/core/modules/dblog/dblog.post_update.php b/core/modules/dblog/dblog.post_update.php index d565a44..caf86de 100644 --- a/core/modules/dblog/dblog.post_update.php +++ b/core/modules/dblog/dblog.post_update.php @@ -5,72 +5,18 @@ * Post update functions for Dblog. */ +use Drupal\views\Entity\View; + /** * Use standard plugin for wid and uid fields. Use dblog_types for type filter. */ function dblog_post_update_wid_uid_from_numeric_to_standard_fields_and_type_filter() { - $config_factory = \Drupal::configFactory(); - $numeric_attributes_to_delete = [ - 'set_precision', - 'precision', - 'decimal', - 'separator', - 'format_plural', - 'format_plural_string', - 'prefix', - 'suffix', - ]; - foreach ($config_factory->listAll('views.view.') as $view_config_name) { - $view = $config_factory->getEditable($view_config_name); - if ($view->get('base_table') != 'watchdog') { - continue; - } - - $save = FALSE; - foreach ($view->get('display') as $display_name => $display) { - // Iterate through all the fields of watchdog views based tables. - if (isset($display['display_options']['fields'])) { - foreach ($display['display_options']['fields'] as $field_name => $field) { - // We are only interested in wid and uid fields from the watchdog - // table that still use the numeric id. - if (isset($field['table']) && - $field['table'] === 'watchdog' && - $field['plugin_id'] == 'numeric' && - in_array($field['field'], ['wid', 'uid'])) { - - $save = TRUE; - $new_value = $field; - $new_value['plugin_id'] = 'standard'; - - // Delete all the attributes related to numeric fields. - foreach ($numeric_attributes_to_delete as $key) { - if (isset($new_value[$key])) { - unset($new_value[$key]); - } - } - $view->set("display.$display_name.display_options.fields.$field_name", $new_value); - } - } - } - - // Iterate all filters looking for type filters to update. - if (isset($display['display_options']['filters'])) { - foreach ($display['display_options']['filters'] as $filter_name => $filter) { - if (isset($filter['table']) && - $filter['table'] === 'watchdog' && - $filter['plugin_id'] == 'in_operator' && - $filter['field'] == 'type') { - - $save = TRUE; - $filter['plugin_id'] = 'dblog_types'; - $view->set("display.$display_name.display_options.filters.$filter_name", $filter); - } - } - } - } - - if ($save) { + // The fields and filters are fixed automatically in dblog_view_presave() + // so we just need to re-save all views that use watchdog as base table. + $views = View::loadMultiple(); + array_walk($views, function(View $view) { + if ($view->get('base_table') == 'watchdog') { $view->save(); } - } + }); }