function webform_encrypt_update_8101 in Webform Encrypt 8
Migrate webform encrypt enabed elements to using third party settings.
File
- ./
webform_encrypt.install, line 50 - Contains install and update related functions for the Webform Encrypt module.
Code
function webform_encrypt_update_8101() {
// Upgrade path for https://www.drupal.org/node/2921824
// Get a list of all encrypt enabled webform elements.
$old_config = \Drupal::service('config.factory')
->get('webform.encrypt')
->get('element.settings');
if (!empty($old_config)) {
foreach ($old_config as $element_name => $config) {
if ($config['encrypt'] !== '1') {
unset($old_config[$element_name]);
}
}
}
if (!empty($old_config)) {
// Loop through all webform on the site.
$webforms = Webform::loadMultiple();
foreach ($webforms as $webform) {
$webform_elements = $webform
->getElementsDecodedAndFlattened();
$webform_elements_names = array_keys($webform_elements);
$new_config = [];
foreach ($webform_elements_names as $webform_element_name) {
// If the webform is using an encrypt enabled element.
if (isset($old_config[$webform_element_name])) {
// Add the new settings to be saved.
$values = $old_config[$webform_element_name];
$new_config[$webform_element_name] = [
'encrypt' => $values['encrypt'] == '1',
'encrypt_profile' => $values['encrypt_profile'],
];
}
}
if (!empty($new_config)) {
$webform
->setThirdPartySetting('webform_encrypt', 'element', $new_config);
\Drupal::messenger()
->addMessage(t('Set webform_encrypt settings for %elements on the webform %webform.', [
'%elements' => implode(', ', array_keys($new_config)),
'%webform' => $webform
->get('title'),
]));
$webform
->save();
\Drupal::messenger()
->addMessage(t('Saved the webform %webform with the new webform_encrypt settings.'), [
'%webform' => $webform
->get('title'),
]);
}
}
}
// Delete the old config.
\Drupal::configFactory()
->getEditable('webform.encrypt')
->delete();
\Drupal::messenger()
->addMessage(t('webform.encrypt configuration object deleted. If you had exported it you should remove it from your filesystem now.'));
drupal_flush_all_caches();
}