You are here

function encrypt_update_8002 in Encrypt 8.3

Update existing encryption profiles to depend on their keys.

File

./encrypt.install, line 35
Install and hook_update_nn functions for the encrypt module.

Code

function encrypt_update_8002() {
  $config_factory = \Drupal::configFactory();
  $updated = FALSE;

  // Get all config profiles.
  foreach ($config_factory
    ->listAll('encrypt.profile.') as $profile_config_name) {
    $profile_config = $config_factory
      ->getEditable($profile_config_name);

    // Check if the profile has a key as dependency. It may have other config
    // dependencies so let's check each.
    $has_dependency = FALSE;
    $config_names = $profile_config
      ->get('dependencies.config');
    if (is_array($config_names)) {
      foreach ($config_names as $config_name) {
        if (strpos($config_name, 'key.key') === 0) {
          $has_dependency = TRUE;
        }
      }
    }

    // Add the dependency.
    if (!$has_dependency) {
      $entity_manager = \Drupal::entityTypeManager();
      $encryption_profile = $entity_manager
        ->getStorage('encryption_profile')
        ->load($profile_config
        ->get('id'));
      if ($encryption_profile) {
        $config_names = is_array($config_names) ? $config_names : [];
        $config_names[] = 'key.key.' . $encryption_profile
          ->getEncryptionKeyId();
        $profile_config
          ->set('dependencies.config', $config_names);
        $profile_config
          ->save(TRUE);
        $updated = TRUE;
      }
    }
  }
  if ($updated) {
    return t('Added configuration dependencies on keys for existing profiles.');
  }
  else {
    return t('No changes were made because there were no profiles without a configuration dependency on their key.');
  }
}