Created
February 25, 2016 14:45
-
-
Save mglaman/1f1e75630ee7cbdc69ae to your computer and use it in GitHub Desktop.
Loops through your custom module and ensures the config on hook_rebuild. Features like behavior, sans-Features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Implements hook_rebuild(). | |
| */ | |
| function mymodule_core_rebuild() { | |
| $collection = \Drupal\Core\Config\StorageInterface::DEFAULT_COLLECTION; | |
| $config_manager = \Drupal::service('config.manager'); | |
| $modules = ['custom_module1', 'custom_module2', 'custom_module3']; | |
| foreach ($modules as $module) { | |
| $default_install_path = drupal_get_path('module', $module) . '/' . \Drupal\Core\Config\InstallStorage::CONFIG_INSTALL_DIRECTORY; | |
| $storage = new \Drupal\Core\Config\FileStorage($default_install_path, $collection); | |
| $prefix = ''; | |
| $data = $storage->readMultiple($storage->listAll($prefix)); | |
| if (!empty($data)) { | |
| $dependency_manager = new \Drupal\Core\Config\Entity\ConfigDependencyManager(); | |
| $config_names = $dependency_manager | |
| ->setData($data) | |
| ->sortAll(); | |
| $overrider = $config_manager->getConfigCollectionInfo()->getOverrideService($collection); | |
| foreach ($config_names as $name) { | |
| if ($overrider) { | |
| $new_config = $overrider->createConfigObject($name, $collection); | |
| } | |
| else { | |
| $new_config = new Drupal\Core\Config\Config( | |
| $name, | |
| $storage, | |
| \Drupal::service('event_dispatcher'), | |
| \Drupal::service('config.typed')); | |
| } | |
| if ($data[$name] !== FALSE) { | |
| $new_config->setData($data[$name]); | |
| $new_config->set('_core.default_config_hash', \Drupal\Component\Utility\Crypt::hashBase64(serialize($data[$name]))); | |
| } | |
| if ($entity_type = $config_manager->getEntityTypeIdByName($name)) { | |
| $entity_storage = \Drupal::entityTypeManager() | |
| ->getStorage($entity_type); | |
| if ($storage->exists($name)) { | |
| $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); | |
| $entity = $entity_storage->load($id); | |
| $entity = $entity_storage->updateFromStorageRecord($entity, $new_config->get()); | |
| } | |
| else { | |
| $entity = $entity_storage->createFromStorageRecord($new_config->get()); | |
| } | |
| if ($entity->isInstallable()) { | |
| $entity->trustData()->save(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment