function entity_test_update_8001

Same name in this branch
  1. 11.x core/modules/system/tests/modules/entity_test/update/status_report_8001.inc \entity_test_update_8001()
Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8001.inc \entity_test_update_8001()
  2. 9 core/modules/system/tests/modules/entity_test/update/status_report_8001.inc \entity_test_update_8001()
  3. 8.9.x core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8001.inc \entity_test_update_8001()
  4. 8.9.x core/modules/system/tests/modules/entity_test/update/status_report_8001.inc \entity_test_update_8001()
  5. 10 core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8001.inc \entity_test_update_8001()
  6. 10 core/modules/system/tests/modules/entity_test/update/status_report_8001.inc \entity_test_update_8001()

Makes the 'user_id' field multiple and migrate its data.

File

core/modules/system/tests/modules/entity_test/update/entity_definition_updates_8001.inc, line 13

Code

function entity_test_update_8001() {
    // 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 field data.
    $user_ids = $database->select('entity_test', 'et')
        ->fields('et', [
        'id',
        'user_id',
    ])
        ->execute()
        ->fetchAllKeyed();
    // Remove data from the storage.
    $database->update('entity_test')
        ->fields([
        'user_id' => NULL,
    ])
        ->execute();
    // Update definitions and schema.
    $manager = \Drupal::entityDefinitionUpdateManager();
    $storage_definition = $manager->getFieldStorageDefinition('user_id', 'entity_test');
    $storage_definition->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $manager->updateFieldStorageDefinition($storage_definition);
    // Restore entity data in the new schema.
    $insert_query = $database->insert('entity_test__user_id')
        ->fields([
        'bundle',
        'deleted',
        'entity_id',
        'revision_id',
        'langcode',
        'delta',
        'user_id_target_id',
    ]);
    foreach ($user_ids as $id => $user_id) {
        $insert_query->values([
            'entity_test',
            0,
            $id,
            $id,
            'en',
            0,
            $user_id,
        ]);
    }
    $insert_query->execute();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.