Last active
November 23, 2016 23:25
-
-
Save jmolivas/2417d42ed3a2d1e2a8fe6f06ac1ab2de to your computer and use it in GitHub Desktop.
Update key value form a key|value list field
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 | |
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