Created
August 3, 2021 15:52
-
-
Save rudolfbyker/c8b6350ee85f798d63e227a2f786e91b to your computer and use it in GitHub Desktop.
Utilities for managing Drupal module configuration during module updates.
This file contains 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 | |
namespace Drupal\my_module; | |
use Drupal; | |
use Drupal\Core\Config\FileStorage; | |
/** | |
* Utilities for managing module configuration during module updates. | |
* | |
* @package Drupal\my_module | |
*/ | |
class ConfigUtil { | |
/** | |
* Add or replace config. | |
* | |
* @param string $entity_type_id | |
* The ID of the entity type to replace, e.g. 'entity_form_display'. | |
* @param string $entity_id | |
* The ID of the entity to replace, e.g. 'node.content_type.default'. | |
* @param array $new_values | |
* An array of new values from which to create the new entity. | |
* | |
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | |
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
public static function addOrReplace(string $entity_type_id, string $entity_id, array $new_values): void { | |
$storage = Drupal::entityTypeManager()->getStorage($entity_type_id); | |
$original = $storage->load($entity_id); | |
if ($original) { | |
self::replace($entity_type_id, $entity_id, $new_values); | |
} | |
else { | |
self::add($entity_type_id, $new_values); | |
} | |
} | |
/** | |
* Replace existing config. | |
* | |
* @param string $entity_type_id | |
* The ID of the entity type to replace, e.g. 'entity_form_display'. | |
* @param string $entity_id | |
* The ID of the entity to replace, e.g. 'node.content_type.default'. | |
* @param array $new_values | |
* An array of new values from which to create the new entity. | |
* | |
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | |
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
public static function replace(string $entity_type_id, string $entity_id, array $new_values): void { | |
$storage = Drupal::entityTypeManager()->getStorage($entity_type_id); | |
$original = $storage->load($entity_id); | |
$new_values['uuid'] = $original->uuid(); | |
$updated = $storage->create($new_values); | |
$updated->original = $original; | |
$updated->enforceIsNew(FALSE); | |
$updated->save(); | |
} | |
/** | |
* Add new config. | |
* | |
* @param string $entity_type_id | |
* The ID of the entity type to add, e.g. 'entity_form_display'. | |
* @param array $values | |
* An array of new values from which to create the new entity. | |
* | |
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | |
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
public static function add(string $entity_type_id, array $values): void { | |
Drupal::entityTypeManager() | |
->getStorage($entity_type_id) | |
->create($values) | |
->save(); | |
} | |
/** | |
* Delete existing config if it exists. | |
* | |
* @param string $entity_type_id | |
* The ID of the entity type to delete, e.g. 'entity_form_display'. | |
* @param string $entity_id | |
* The ID of the entity to delete, e.g. 'node.content_type.default'. | |
* | |
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | |
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
public static function deleteIfExists(string $entity_type_id, string $entity_id): void { | |
$storage = Drupal::entityTypeManager()->getStorage($entity_type_id); | |
$entity = $storage->load($entity_id); | |
if ($entity) { | |
$entity->delete(); | |
} | |
} | |
/** | |
* Get the stored config yml files from `module_dir/config/install`. | |
* | |
* @param string $module_name | |
* The name of the module whose config files to load. | |
* | |
* @return \Drupal\Core\Config\FileStorage | |
* The config that ships with the specified module. | |
*/ | |
public static function getFileStorage(string $module_name): FileStorage { | |
return new FileStorage(drupal_get_path('module', $module_name) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'install'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment