Created
November 24, 2017 18:41
-
-
Save jmolivas/e3189c3af2659f358e187829476e5b76 to your computer and use it in GitHub Desktop.
Update field data table and configuration storage object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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