function entity_test_update_8002
Same name in this branch
- 9 core/modules/system/tests/modules/entity_test/update/status_report_8002.inc \entity_test_update_8002()
Same name in other branches
- 8.9.x core/modules/system/tests/modules/entity_test/update/status_report_8002.inc \entity_test_update_8002()
- 8.9.x core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8002.inc \entity_test_update_8002()
- 10 core/modules/system/tests/modules/entity_test/update/status_report_8002.inc \entity_test_update_8002()
- 10 core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8002.inc \entity_test_update_8002()
- 11.x core/modules/system/tests/modules/entity_test/update/status_report_8002.inc \entity_test_update_8002()
- 11.x core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8002.inc \entity_test_update_8002()
Makes the 'user_id' field single and migrate its data.
File
-
core/
modules/ system/ tests/ modules/ entity_test/ update/ entity_definition_updates_8002.inc, line 13
Code
function entity_test_update_8002() {
// To update the field schema we need to have no field data in the storage,
// thus we retrieve it, delete it from storage, and write it back to the
// storage after updating the schema.
$database = \Drupal::database();
// Retrieve existing entity data.
$query = $database->select('entity_test__user_id', 'et')
->fields('et', [
'entity_id',
'user_id_target_id',
]);
$query->condition('et.delta', 0);
$user_ids = $query->execute()
->fetchAllKeyed();
// Remove data from the storage.
$database->truncate('entity_test__user_id')
->execute();
// Update definitions and schema.
$manager = \Drupal::entityDefinitionUpdateManager();
$storage_definition = $manager->getFieldStorageDefinition('user_id', 'entity_test');
$storage_definition->setCardinality(1);
$manager->updateFieldStorageDefinition($storage_definition);
// Restore entity data in the new schema.
foreach ($user_ids as $id => $user_id) {
$database->update('entity_test')
->fields([
'user_id' => $user_id,
])
->condition('id', $id)
->execute();
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.