You are here

function loqate_update_8003 in Loqate 8

Same name and namespace in other branches
  1. 2.x loqate.install \loqate_update_8003()

Convert old key string value into a key config entity.

File

./loqate.install, line 32
Contains loqate.install.

Code

function loqate_update_8003() {

  // Move the string value into a key config entity if any.
  $loqate_config = \Drupal::configFactory()
    ->getEditable('loqate.loqateapikeyconfig');
  $old_key_value = $loqate_config
    ->get(LoqateApiKeyConfigForm::DEFAULT_API_KEY);
  if (!empty($old_key_value)) {

    // Create a new entity key with the config key provider.
    $key_entity = new Key([
      'id' => LoqateApiKeyConfigForm::DEFAULT_API_KEY,
      'label' => 'Loqate API key',
      'key_provider' => 'config',
      'key_input' => 'text_field',
    ], 'key');
    $key_entity
      ->setKeyValue($old_key_value);
    $key_entity
      ->save();

    // Set the config id now as the key value in config.
    $loqate_config
      ->set(LoqateApiKeyConfigForm::DEFAULT_API_KEY, LoqateApiKeyConfigForm::DEFAULT_API_KEY)
      ->save();
    return t('Key config entity successfully created.');
  }
  return t('No old key value found.');
}