diff --git a/config_ignore.module b/config_ignore.module index a8b6d75..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(). @@ -24,26 +27,25 @@ function config_ignore_config_import_steps_alter(&$sync_steps, ConfigImporter $c * 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 + // Load the needed services. $storage_sync = \Drupal::service('config.storage.sync'); $storage = \Drupal::service('config.storage'); $config_manager = \Drupal::service('config.manager'); - // Get the ignored entities - $config_ignore_settings = \Drupal::config('config_ignore.settings')->get('ignored_config_entities'); - - $storage_comparer = new StorageComparer($storage_sync, $storage, $config_manager); - foreach ($storage_comparer->getAllCollectionNames() as $collection) { - // Add a new header + $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 - foreach ($form[$collection]['update']['list']['#rows'] as $key => $row) { - if (in_array($row['name'], $config_ignore_settings)) { - $form[$collection]['update']['list']['#rows'][$key]['ignored'] = t('✔'); - } - else { - $form[$collection]['update']['list']['#rows'][$key]['ignored'] = t('✖'); + // 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 43e0b4d..7fe1566 100644 --- a/src/ConfigImporterIgnore.php +++ b/src/ConfigImporterIgnore.php @@ -26,30 +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'); + 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(); } } } @@ -89,7 +72,7 @@ class ConfigImporterIgnore { $temp_store->delete('config_to_ignore'); // Inform about the config entities ignored. - // We have two formats, on for browser output and one for terminal. + // 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. @@ -120,4 +103,39 @@ class ConfigImporterIgnore { } } + /** + * Match a config entity name against the list of ignored config entities. + * + * @param string $config_name + * The name of the config entity to match against all ignored entities. + * + * @return bool + * True, if the config entity is to be ignored, false otherwise. + */ + public static function matchConfigName($config_name) { + $config_ignore_settings = \Drupal::config('config_ignore.settings')->get('ignored_config_entities'); + 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_name, 0, strlen($config_ignore_setting)) == $config_ignore_setting) { + return TRUE; + } + } + // 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_name == $config_ignore_setting) { + return TRUE; + } + } + + return FALSE; + } + } diff --git a/src/Tests/ConfigIgnoreTest.php b/src/Tests/ConfigIgnoreTest.php index 1d450f1..e353250 100644 --- a/src/Tests/ConfigIgnoreTest.php +++ b/src/Tests/ConfigIgnoreTest.php @@ -98,6 +98,7 @@ class ConfigIgnoreTest extends WebTestBase { // Validate that the user gets a message about what has been ignored. $this->assertText('The following config entity was ignored'); + } } diff --git a/tests/src/Functional/ConfigIgnoreTest.php b/tests/src/Functional/ConfigIgnoreTest.php new file mode 100644 index 0000000..fe4783e --- /dev/null +++ b/tests/src/Functional/ConfigIgnoreTest.php @@ -0,0 +1,61 @@ +config('system.site')->set('name', 'Test import')->save(); + $this->config('system.date')->set('first_day', '0')->save(); + $this->config('config_ignore.settings')->set('ignored_config_entities', ['system.site'])->save(); + $destination = CONFIG_SYNC_DIRECTORY; + $destination_dir = config_get_config_directory($destination); + /** @var CachedStorage $source_storage */ + $source_storage = \Drupal::service('config.storage'); + $destination_storage = new FileStorage($destination_dir); + foreach ($source_storage->listAll() as $name) { + $destination_storage->write($name, $source_storage->read($name)); + } + + // Login with a user that has permission to sync. config. + $this->drupalLogin($this->drupalCreateUser(['synchronize configuration'])); + + // Change the site name, which is supposed to look as an ignored change + // in on the sync. page. + $this->config('system.site')->set('name', 'Test import with changed title')->save(); + $this->config('system.date')->set('first_day', '1')->save(); + + // Validate that the sync. table informs the user that the config will be + // ignored. + $this->drupalGet('admin/config/development/configuration'); + $this->assertSession()->responseContains('✔'); + $this->assertSession()->responseContains('✖'); + } + +}