Skip to content

Instantly share code, notes, and snippets.

@edutrul
Forked from jmolivas/module_name.install
Created November 24, 2017 19:30
Show Gist options
  • Save edutrul/70f86efe225a7f7a2df683f8b7256443 to your computer and use it in GitHub Desktop.
Save edutrul/70f86efe225a7f7a2df683f8b7256443 to your computer and use it in GitHub Desktop.
Update field data table and configuration storage object.
<?php
/**
* Update field data table and configuration storage
*/
function _update_field_data(
$entityType,
$field,
$valueMapping,
$configOverride
) {
$tables = [
$entityType.'__' . $field,
$entityType.'_revision__' . $field,
];
foreach ($tables as $table) {
foreach ($valueMapping as $currentValue => $newValue) {
Database::getConnection()
->update($table)
->fields([$field . '_value' => $newValue])
->condition($field . '_value', $currentValue)
->execute();
}
}
\Drupal::configFactory()
->getEditable('field.storage.'.$entityType.'.'.$field )
->set(
$configOverride['key'],
$configOverride['value']
)
->save();
}
function module_name_update_N() {
$field = 'field_overlay_color';
$entityType = 'paragraph';
$valueMapping = [
'None' => 'purple',
'Pink' => 'purple',
'Black' => 'purple',
'Purple' => 'purple',
'Magenta' => 'magenta'
];
$configOverride = [
'key' => 'settings.allowed_values',
'value' =>[
[
'value' => 'purple',
'label' => 'Purple',
],
[
'value' => 'magenta',
'label' => 'Magenta'
],
[
'value' => 'maroon',
'label' => 'Maroon'
]
]
];
_update_field_data(
$entityType,
$field,
$valueMapping,
$configOverride
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment