function media_update_8700

Set the 'owner' entity key and update the field.

File

core/modules/media/media.install, line 233

Code

function media_update_8700() {
    $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $entity_type = $definition_update_manager->getEntityType('media');
    $database = \Drupal::database();
    if (\Drupal::entityTypeManager()->getStorage('media') instanceof SqlEntityStorageInterface) {
        if ($database->schema()
            ->tableExists($entity_type->getDataTable())) {
            $database->update($entity_type->getDataTable())
                ->fields([
                'uid' => 0,
            ])
                ->isNull('uid')
                ->execute();
        }
        if ($database->schema()
            ->tableExists($entity_type->getRevisionDataTable())) {
            $database->update($entity_type->getRevisionDataTable())
                ->fields([
                'uid' => 0,
            ])
                ->isNull('uid')
                ->execute();
        }
    }
    $keys = $entity_type->getKeys();
    $keys['owner'] = 'uid';
    $entity_type->set('entity_keys', $keys);
    $definition_update_manager->updateEntityType($entity_type);
    $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'media'));
}

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