Commit dfd537fe authored by catch's avatar catch

Issue #2095489 by tayzlor, beejeebus: Separate out module install config code from import code.

parent 05fbc52f
......@@ -3,11 +3,10 @@
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigException;
use Drupal\Core\Config\ConfigInstaller;
use Drupal\Core\Config\ExtensionInstallStorage;
use Drupal\Core\Config\Context\FreeConfigContext;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\ExtensionInstallStorageComparer;
use Symfony\Component\Yaml\Dumper;
/**
......@@ -37,8 +36,6 @@
* The name of the module or theme to install default configuration for.
*
* @see \Drupal\Core\Config\ExtensionInstallStorage
* @see \Drupal\Core\Config\ExtensionInstallStorageComparer
* @see \Drupal\Core\Config\ConfigInstaller
*/
function config_install_default_config($type, $name) {
// Get all default configuration owned by this extension.
......@@ -66,20 +63,36 @@ function ($value) use ($name) {
$config_to_install = array_merge($config_to_install, $other_module_config);
}
if (!empty($config_to_install)) {
$storage_comparer = new ExtensionInstallStorageComparer($source_storage, \Drupal::service('config.storage'));
$storage_comparer->setSourceNames($config_to_install);
// Only import new config. Changed config is from previous enables and
// should not be overwritten.
$storage_comparer->addChangelistCreate();
$installer = new ConfigInstaller(
$storage_comparer,
\Drupal::service('event_dispatcher'),
\Drupal::service('config.factory'),
\Drupal::entityManager(),
\Drupal::lock(),
\Drupal::service('uuid')
);
$installer->import();
$entity_manager = Drupal::service('entity.manager');
$config_factory = Drupal::service('config.factory');
$context = new FreeConfigContext(Drupal::service('event_dispatcher'), Drupal::service('uuid'));
$target_storage = Drupal::service('config.storage');
$config_factory->enterContext($context);
foreach ($config_to_install as $name) {
// Only import new config.
if ($target_storage->exists($name)) {
continue;
}
$new_config = new Config($name, $target_storage, $context);
$data = $source_storage->read($name);
if ($data !== FALSE) {
$new_config->setData($data);
}
if ($entity_type = config_get_entity_type_by_name($name)) {
$entity_manager
->getStorageController($entity_type)
->create($new_config->get())
->save();
}
else {
$new_config->save();
}
// Reset static cache on the config factory.
$config_factory->reset($name);
}
$config_factory->leaveContext();
}
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Config\ConfigInstaller.
*/
namespace Drupal\Core\Config;
/**
* Defines a configuration installer.
*
* A config installer imports the changes into the configuration system during
* module installs.
*
* The ConfigInstaller has a identifier which is used to construct event names.
* The events fired during an import are:
* - 'config.installer.validate': Events listening can throw a
* \Drupal\Core\Config\ConfigImporterException to prevent an import from
* occurring.
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
* - 'config.installer.import': Events listening can react to a successful import.
*
* @see \Drupal\Core\Config\ConfigImporter
*/
class ConfigInstaller extends ConfigImporter {
/**
* The name used to identify events and the lock.
*/
const ID = 'config.installer';
}
......@@ -30,32 +30,5 @@ protected function getAllFolders() {
}
return $this->folders;
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Config\StorageException
*/
public function write($name, array $data) {
throw new StorageException('Write operation is not allowed for config extension install storage.');
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Config\StorageException
*/
public function delete($name) {
throw new StorageException('Delete operation is not allowed for config extension install storage.');
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Config\StorageException
*/
public function rename($name, $new_name) {
throw new StorageException('Rename operation is not allowed for config extension install storage.');
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Config\ExtensionInstallStorageComparer.
*/
namespace Drupal\Core\Config;
/**
* Defines a config storage comparer.
*/
class ExtensionInstallStorageComparer extends StorageComparer {
/**
* Sets the configuration names in the source storage.
*
* @param array $source_names
* List of all the configuration names in the source storage.
*/
public function setSourceNames(array $source_names) {
$this->sourceNames = $source_names;
return $this;
}
/**
* Gets all the configuration names in the source storage.
*
* @return array
* List of all the configuration names in the source storage.
*
* @see self::setSourceNames()
*/
protected function getSourceNames() {
return $this->sourceNames;
}
}
......@@ -52,10 +52,6 @@ function testModuleInstallation() {
$config = \Drupal::config($default_configuration_entity);
$this->assertIdentical($config->isNew(), FALSE);
// Verify that configuration import callback was invoked for the dynamic
// configuration entity.
$this->assertTrue($GLOBALS['hook_config_import']);
// Verify that config_test API hooks were invoked for the dynamic default
// configuration entity.
$this->assertFalse(isset($GLOBALS['hook_config_test']['load']));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment