Skip to content

Instantly share code, notes, and snippets.

@mglaman
Last active March 1, 2016 21:56
Show Gist options
  • Save mglaman/5f2716f895cc736f5c39 to your computer and use it in GitHub Desktop.
Save mglaman/5f2716f895cc736f5c39 to your computer and use it in GitHub Desktop.
Example using config_update to help automate hook_update_N for new config in config/install, or non-overridden config already active.
<?php
function example_helper_to_import_config_in_hook_update_N() {
/** @var \Drupal\config_update\ConfigReverter $updater */
$updater = \Drupal::service('config_update.config_update');
try {
$updater->import('commerce_product_type', 'dvds');
}
catch (\Drupal\Core\Entity\EntityStorageException $e) {
// What to do if already exists.
}
// We compared old known hash to active hash. We will always have a diff
// from the config_update.config_diff service.
$old_hash = 's-_5XolJXcCRi-R-_0Jgg-XrC85VXZWoOi0fBKRHLD4';
// Returns raw input, not EntityInterface::toArray because it's reading
// straight from storage.
$active = $updater->getFromActive('commerce_product_type', 't_shirt');
unset($active['uuid']);
unset($active['_core']);
// Get hash
$active_hash = \Drupal\Component\Utility\Crypt::hashBase64(serialize($active));
if ($active_hash === $old_hash) {
$updater->revert('commerce_product_type', 't_shirt');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment