Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Last active November 23, 2016 23:25
Show Gist options
  • Save jmolivas/2417d42ed3a2d1e2a8fe6f06ac1ab2de to your computer and use it in GitHub Desktop.
Save jmolivas/2417d42ed3a2d1e2a8fe6f06ac1ab2de to your computer and use it in GitHub Desktop.
Update key value form a key|value list field
<?php
use Drupal\Core\Database\Database;
function module_update_80NN() {
$field = 'field_name';
$tables = [
'node__'.$field,
'node_revision__' .$field
];
$fieldMapping = [
'old-1' => 'new-1',
'old-2' => 'new-2',
'old-3' => 'new-3',
'old-4' => 'new-4',
'old-5' => 'new-5',
];
foreach($tables as $table) {
foreach ($fieldMapping as $oldValue => $newValue) {
Database::getConnection()
->update($table)
->fields([$field.'_value' => $newValue])
->condition($field.'_value', $oldValue)
->execute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment