You are here

function file_entity_update_8004 in File Entity (fieldable files) 8.2

Fix entity field mismatches on the file type field.

File

./file_entity.install, line 162
Install, update and uninstall functions for the file_entity module.

Code

function file_entity_update_8004() {
  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $original = $entity_definition_update_manager
    ->getFieldStorageDefinition('type', 'file');
  $field_storage = \Drupal::service('entity_field.manager')
    ->getFieldStorageDefinitions('file')['type'];

  /** @var \Drupal\Core\Entity\Schema\DynamicallyFieldableEntityStorageSchemaInterface $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('file');

  // Do nothing if there is no field storage schema change reported.
  if ($original && !$storage
    ->requiresFieldStorageSchemaChanges($field_storage, $original)) {
    return;
  }

  // Ensure that the type field is not null, update the actual schema and the
  // stored schema.
  $key_value = \Drupal::keyValue('entity.storage_schema.sql');
  $key_name = 'file.field_schema_data.type';
  $storage_schema = $key_value
    ->get($key_name);
  $storage_schema['file_managed']['fields']['type']['not null'] = TRUE;
  \Drupal::database()
    ->schema()
    ->changeField('file_managed', 'type', 'type', $storage_schema['file_managed']['fields']['type']);
  $key_value
    ->set($key_name, $storage_schema);
}