Skip to content

Instantly share code, notes, and snippets.

@alexpott
Last active October 11, 2016 10:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpott/f0650fbb7954df8497e4 to your computer and use it in GitHub Desktop.
Save alexpott/f0650fbb7954df8497e4 to your computer and use it in GitHub Desktop.
<?php
use Drupal\Core\Extension\ExtensionDiscovery;
// IMPORTANT!
// Uncomment $settings['extension_discovery_scan_tests'] = TRUE; in settings.php
// to include test modules.
//$config_entity_type_id = drush_prompt('Entity type ID?');
//$config_entity_provider = drush_prompt('Entity type provider? (leave blank if core)');
$config_entity_type_id = 'view';
$config_entity_provider = 'views';
// Ensure provider is installed.
if ($config_entity_provider) {
\Drupal::moduleHandler()->install(array($config_entity_provider));
}
$config_entity_type = \Drupal::entityManager()->getDefinition($config_entity_type_id);
$entity_storage = \Drupal::entityManager()->getStorage($config_entity_type_id);
$listing = new ExtensionDiscovery(\Drupal::root());
$modules = $listing->scan('module');
foreach ($modules as $module) {
$extension_path = $module->getPath();
// If the extension provides configuration schema clear the definitions.
if (is_dir($extension_path . '/' . \Drupal\Core\Config\InstallStorage::CONFIG_INSTALL_DIRECTORY)) {
$install_storage = new \Drupal\Core\Config\FileStorage($extension_path . '/' . \Drupal\Core\Config\InstallStorage::CONFIG_INSTALL_DIRECTORY);
$entities = $install_storage->listAll($config_entity_type->getConfigPrefix());
if (count($entities)) {
// Ensure module is installed.
\Drupal::moduleHandler()->install(array($module->getName()));
}
foreach ($entities as $name) {
$id = substr($name, strlen($config_entity_type->getConfigPrefix()) + 1);
$loaded_entity = $entity_storage->load($id);
$data = $loaded_entity->toArray();
unset($data['uuid']);
$config = new \Drupal\Core\Config\Config($name, $install_storage, \Drupal::service('event_dispatcher'), \Drupal::service('config.typed'));
$config->setData($data)->save();
}
}
if (is_dir($extension_path . '/test_views')) {
// Ensure module is installed.
\Drupal::moduleHandler()->install(array($module->getName()));
$install_storage = new \Drupal\Core\Config\FileStorage($extension_path . '/test_views');
$entities = $install_storage->listAll($config_entity_type->getConfigPrefix());
foreach ($entities as $name) {
if (\Drupal::service('config.storage')->exists($name)) {
$id = substr($name, strlen($config_entity_type->getConfigPrefix()) + 1);
$entity_storage->load($id)->delete();
}
$entity = $entity_storage->createFromStorageRecord($install_storage->read($name));
try {
$entity->save();
}
catch (\Exception $e) {
print "Saving $name has generated the following exception: " . $e->getMessage() . "\n";
continue;
}
$data = $entity->toArray();
unset($data['uuid']);
$config = new \Drupal\Core\Config\Config($name, $install_storage, \Drupal::service('event_dispatcher'), \Drupal::service('config.typed'));
$config->setData($data)->save();
}
}
}
@jibran
Copy link

jibran commented Nov 19, 2014

Would this work for contrib modules as well?

$listing = new ExtensionDiscovery(\Drupal::root());
$modules = $listing->scan('module');

According to this yes!

@vijaycs85
Copy link

Needs \Drupal::moduleHandler()->install => \Drupal::service('module_installer')->install() update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment