From 52c990836cf0e94df24ac2a9c89d6ca3ddc4c6b0 Mon Sep 17 00:00:00 2001 From: tlyngej Date: Fri, 17 Feb 2017 12:54:47 +0000 Subject: [PATCH] Issue #2845286 by tlyngej, jzavrl: Should the module suppress configuration override notices? --- config_ignore.drush.inc | 72 ++++++++++++++++++ config_ignore.module | 31 ++++++++ src/ConfigImporterIgnore.php | 93 ++++++++++++++++++----- src/Tests/ConfigIgnoreTest.php | 7 ++ tests/src/Functional/ConfigIgnoreTest.php | 61 +++++++++++++++ 5 files changed, 243 insertions(+), 21 deletions(-) create mode 100644 config_ignore.drush.inc create mode 100644 tests/src/Functional/ConfigIgnoreTest.php diff --git a/config_ignore.drush.inc b/config_ignore.drush.inc new file mode 100644 index 0000000..0b1c278 --- /dev/null +++ b/config_ignore.drush.inc @@ -0,0 +1,72 @@ +createChangelist()->hasChanges(); + + $change_list = array(); + foreach ($storage_comparer->getAllCollectionNames() as $collection) { + $change_list[$collection] = $storage_comparer->getChangelist(NULL, $collection); + } + + $update_changes = $change_list['']['update']; + + if (!empty($update_changes)) { + + $red = "\033[31;40m\033[1m%s\033[0m"; + $yellow = "\033[1;33;40m\033[1m%s\033[0m"; + $green = "\033[1;32;40m\033[1m%s\033[0m"; + + drush_print(); + drush_print(sprintf($green, 'Message from Config Ignore'), 1); + drush_print('The following list of config will be ignore if you chose to import', 1); + foreach ($update_changes as $config_name) { + if (ConfigImporterIgnore::matchConfigName($config_name)) { + drush_print(sprintf($red, $config_name), 3); + } + } + drush_print(); + } +} diff --git a/config_ignore.module b/config_ignore.module index b5339a4..5387b37 100644 --- a/config_ignore.module +++ b/config_ignore.module @@ -6,6 +6,9 @@ */ use Drupal\Core\Config\ConfigImporter; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Config\StorageComparer; +use Drupal\config_ignore\ConfigImporterIgnore; /** * Implements hook_config_import_steps_alter(). @@ -19,3 +22,31 @@ function config_ignore_config_import_steps_alter(&$sync_steps, ConfigImporter $c // workflow. array_push($sync_steps, ['Drupal\config_ignore\ConfigImporterIgnore', 'postImport']); } + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function config_ignore_form_config_admin_import_form_alter(&$form, FormStateInterface $form_state, $form_id) { + // Load the needed services. + $storage_sync = \Drupal::service('config.storage.sync'); + $storage = \Drupal::service('config.storage'); + $config_manager = \Drupal::service('config.manager'); + + $storage_compare = new StorageComparer($storage_sync, $storage, $config_manager); + foreach ($storage_compare->getAllCollectionNames() as $collection) { + // Add a new header. + $form[$collection]['update']['list']['#header'][] = t('Ignored'); + + // Now check if the rows match any of the ignored entities. + if (isset($form[$collection]['update']['list']['#rows']) && !empty($form[$collection]['update']['list']['#rows'])) { + foreach ($form[$collection]['update']['list']['#rows'] as $key => $row) { + if (ConfigImporterIgnore::matchConfigName($row['name'])) { + $form[$collection]['update']['list']['#rows'][$key]['ignored'] = t('✔'); + } + else { + $form[$collection]['update']['list']['#rows'][$key]['ignored'] = t('✖'); + } + } + } + } +} diff --git a/src/ConfigImporterIgnore.php b/src/ConfigImporterIgnore.php index fdc20a5..3c47ebd 100644 --- a/src/ConfigImporterIgnore.php +++ b/src/ConfigImporterIgnore.php @@ -26,31 +26,13 @@ class ConfigImporterIgnore { */ public static function preImport(array &$context, ConfigImporter $config_importer) { $config_to_ignore = []; - $config_ignore_settings = \Drupal::config('config_ignore.settings')->get('ignored_config_entities'); - \Drupal::moduleHandler()->invokeAll('config_ignore_settings_alter', [&$config_ignore_settings]); + foreach (['delete', 'create', 'rename', 'update'] as $op) { // For now, we only support updates. if ($op == 'update') { foreach ($config_importer->getUnprocessedConfiguration($op) as $config) { - foreach ($config_ignore_settings as $config_ignore_setting) { - // Check if the last character in the string is an asterisk. - // If so, it means that it is a wildcard. - if (Unicode::substr($config_ignore_setting, -1) == '*') { - // Remove the asterisk character from the end of the string. - $config_ignore_setting = rtrim($config_ignore_setting, '*'); - // Test if the start of the config, we are checking, are matching - // the $config_ignore_setting string. If it is a match, mark - // that config name to be ignored. - if (Unicode::substr($config, 0, strlen($config_ignore_setting)) == $config_ignore_setting) { - $config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData(); - } - } - // If string does not contain an asterisk in the end, just compare - // the two strings, and if they match, mark that config name to be - // ignored. - elseif ($config == $config_ignore_setting) { - $config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData(); - } + if (self::matchConfigName($config)) { + $config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData(); } } } @@ -77,15 +59,84 @@ class ConfigImporterIgnore { /** @var SharedTempStore $temp_store */ $temp_store = \Drupal::service('user.shared_tempstore')->get('config_ignore'); $config_to_ignore = $temp_store->get('config_to_ignore'); + $config_names_ignored = []; foreach ($config_to_ignore as $op) { foreach ($op as $config_name => $config) { /** @var \Drupal\Core\Config\Config $config_to_restore */ $config_to_restore = \Drupal::service('config.factory')->getEditable($config_name); $config_to_restore->setData($config)->save(); + $config_names_ignored[] = $config_name; } } $context['finished'] = 1; $temp_store->delete('config_to_ignore'); + + // Inform about the config entities ignored. + // We have two formats, one for browser output and one for terminal. + if (!empty($config_names_ignored)) { + + // The list of names looks different depending on output medium. + // If terminal (CLI), then no markup. + if (php_sapi_name() == 'cli' || isset($_SERVER['argc']) && is_numeric($_SERVER['argc'] && $_SERVER['argc'] > 0)) { + $names_list = "\n\r " . implode("\n\r ", $config_names_ignored); + } + else { + $output = [ + '#theme' => 'item_list', + '#list_type' => 'ul', + '#items' => $config_names_ignored, + ]; + $names_list = render($output); + } + + // `PluralTranslatableMarkup` does not seem to handle HTML as well as + // plain t() does. It will not allow the