You are here

public static function ConfigImporterIgnore::postImport in Config Ignore 8

Replace the overridden values with the original ones.

Parameters

array $context: Context of the config import.

ConfigImporter $config_importer: Config importer object.

File

src/ConfigImporterIgnore.php, line 61

Class

ConfigImporterIgnore
Class ConfigImporterIgnore.

Namespace

Drupal\config_ignore

Code

public static function postImport(array &$context, ConfigImporter $config_importer) {

  /** @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 <ul> list in the browser, and
    // renders the lists HTML as clear text.
    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');
  }
}