ConfigImporterIgnore.php 2.29 KB
Newer Older
tlyngej's avatar
tlyngej committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php

namespace Drupal\config_ignore;

use Drupal\Core\Config\ConfigImporter;
use Drupal\user\SharedTempStore;

/**
 * Class ConfigImporterIgnore.
 *
 * @package Drupal\config_ignore
 */
class ConfigImporterIgnore {

  /**
   * Gather config that we want to keep.
   *
   * Saves the values, that are to be ignored, so that we can put them back in
   * later on.
   *
   * @param array $context
   *   Context of the config import.
   * @param ConfigImporter $config_importer
   *   Config importer object.
   */
tlyngej's avatar
tlyngej committed
26
  public static function preImport(array &$context, ConfigImporter $config_importer) {
tlyngej's avatar
tlyngej committed
27 28 29 30
    $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.
31 32 33
      if ($op == 'update') {
        foreach ($config_importer->getUnprocessedConfiguration($op) as $config) {
          if (in_array($config, $config_ignore_settings)) {
tlyngej's avatar
tlyngej committed
34
            $config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData();
35
          }
tlyngej's avatar
tlyngej committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        }
      }
      // We do not support core.extension.
      unset($config_to_ignore[$op]['core.extension']);
    }

    /** @var SharedTempStore $temp_store */
    $temp_store = \Drupal::service('user.shared_tempstore')->get('config_ignore');
    $temp_store->set('config_to_ignore', $config_to_ignore);

    $context['finished'] = 1;
  }

  /**
   * Replace the overridden values with the original ones.
   *
   * @param array $context
   *   Context of the config import.
   * @param ConfigImporter $config_importer
   *   Config importer object.
   */
tlyngej's avatar
tlyngej committed
57
  public static function postImport(array &$context, ConfigImporter $config_importer) {
tlyngej's avatar
tlyngej committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    /** @var SharedTempStore $temp_store */
    $temp_store = \Drupal::service('user.shared_tempstore')->get('config_ignore');
    $config_to_ignore = $temp_store->get('config_to_ignore');
    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();
      }
    }
    $context['finished'] = 1;
    $temp_store->delete('config_to_ignore');
  }

}