ConfigImporterIgnore.php in Config Ignore 8
File
src/ConfigImporterIgnore.php
View source
<?php
namespace Drupal\config_ignore;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigImporter;
use Drupal\user\SharedTempStore;
class ConfigImporterIgnore {
const FORCE_EXCLUSION_PREFIX = '~';
const INCLUDE_SUFFIX = '*';
public static function preImport(array &$context, ConfigImporter $config_importer) {
$config_to_ignore = [];
foreach ([
'delete',
'create',
'rename',
'update',
] as $op) {
if ($op == 'update') {
foreach ($config_importer
->getUnprocessedConfiguration($op) as $config) {
if (self::matchConfigName($config)) {
$config_to_ignore[$op][$config] = \Drupal::config($config)
->getRawData();
}
}
}
unset($config_to_ignore[$op]['core.extension']);
}
$temp_store = \Drupal::service('user.shared_tempstore')
->get('config_ignore');
$temp_store
->set('config_to_ignore', $config_to_ignore);
$context['finished'] = 1;
}
public static function postImport(array &$context, ConfigImporter $config_importer) {
$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) {
$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');
if (!empty($config_names_ignored)) {
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);
}
if (count($config_names_ignored) == 1) {
$message = t('The following config entity was ignored: @list', [
'@list' => $names_list,
]);
}
else {
$message = t('The following @count config entities was ignored: @list', [
'@count' => count($config_names_ignored),
'@list' => $names_list,
]);
}
drupal_set_message($message, 'warning');
}
}
public static function matchConfigName($config_name) {
$config_ignore_settings = \Drupal::config('config_ignore.settings')
->get('ignored_config_entities');
\Drupal::moduleHandler()
->invokeAll('config_ignore_settings_alter', [
&$config_ignore_settings,
]);
if (in_array(static::FORCE_EXCLUSION_PREFIX . $config_name, $config_ignore_settings, TRUE)) {
return FALSE;
}
foreach ($config_ignore_settings as $config_ignore_setting) {
if (fnmatch($config_ignore_setting, $config_name)) {
return TRUE;
}
}
return FALSE;
}
}