Created
February 12, 2019 21:51
-
-
Save kbrinner/78a5da1b11717ea1504963ec043065dd to your computer and use it in GitHub Desktop.
Programmatically import configs
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
| /** | |
| * Import configs that may be needed for any further updates. | |
| * These files should be placed in config/install/[update_id] in this module for initial | |
| * installation. The configs should also be placed in /config/default for all future | |
| * modifications. | |
| * | |
| * Based on https://stackoverflow.com/a/52277490/1650120. | |
| * | |
| * @param string $update_id | |
| * The name of the folder in containing the yml files. | |
| * Folder should be placed in this helper module in config/install. | |
| * | |
| * @return array | |
| * Array of updated and created entity ids. | |
| */ | |
| function _import_configs(string $update_id) { | |
| // Get list of all files in $update_id directory. | |
| $path = drupal_get_path('DEMO', 'DEMO_helper') . '/config/install/' . $update_id; | |
| $files = array(); | |
| $directory = new DirectoryIterator($path); | |
| foreach ($directory as $fileinfo) { | |
| if (!$fileinfo->isDot()) { | |
| $files[] = $path . '/' . $fileinfo->getFilename(); | |
| } | |
| } | |
| // The ConfigManager class provides helper functions for the configuration system. | |
| $config_manager = \Drupal::service('config.manager'); | |
| // Create empty arrays for returning update information. | |
| $updated = array(); | |
| $created = array(); | |
| // Import the configs from each file. | |
| foreach ($files as $file) { | |
| // Get configuration information from yml file. | |
| $raw = file_get_contents($file); | |
| $value = \Drupal\Component\Serialization\Yaml::decode($raw); | |
| if(!is_array($value)) { | |
| throw new \RuntimeException(sprintf('Invalid YAML file %s'), $file); | |
| } | |
| // Gets EntityManagerInterface - the entity manager. | |
| $entity_manager = $config_manager->getEntityManager(); | |
| // Get the entity type of a configuration object - ie | |
| // block_content.type.homepage_hero.yml will get block_content_type. | |
| $entity_type = $config_manager->getEntityTypeIdByName(basename($file)); | |
| // Gets EntityTypeInterface of entity type in current config. | |
| $definition = $entity_manager->getDefinition($entity_type); | |
| // Gets entity id. | |
| $id_key = $definition->getKey('id'); | |
| // Yml config file has been serialized into an array. Get the id. | |
| // For example from field.storage.block_content.field_paragraph.yml it is | |
| // 'homepage_hero' | |
| $id = $value[$id_key]; | |
| // Gets EntityStorageInterface - a storage instance. | |
| $entity_storage = $entity_manager->getStorage($entity_type); | |
| // Load an entity based on its entity id. | |
| $entity = $entity_storage->load($id); | |
| // If entity already exists, update. Else create a new one. | |
| if ($entity) { | |
| $entity = $entity_storage->updateFromStorageRecord($entity, $value); | |
| $entity->save(); | |
| $updated[] = $id; | |
| } | |
| else { | |
| $entity = $entity_storage->createFromStorageRecord($value); | |
| $entity->save(); | |
| $created[] = $id; | |
| } | |
| } | |
| // Return results of update/create entities (if needed). | |
| return [ | |
| 'updated' => $updated, | |
| 'created' => $created, | |
| ]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment