Commit e94b01e5 authored by Dries's avatar Dries

Issue #1996714 by amateescu: Convert FileItem and ImageItem to extend EntityReferenceItem.

parent da1234fb
......@@ -119,7 +119,7 @@ function testImageFieldSync() {
// Generate the item for the current image file entity and attach it to
// the entity.
$item = array(
'fid' => $fid,
'target_id' => $fid,
'alt' => $default_langcode . '_' . $fid . '_' . $this->randomName(),
'title' => $default_langcode . '_' . $fid . '_' . $this->randomName(),
);
......@@ -144,7 +144,7 @@ function testImageFieldSync() {
// the entity.
$fid = $this->files[$index]->fid;
$item = array(
'fid' => $fid,
'target_id' => $fid,
'alt' => $langcode . '_' . $fid . '_' . $this->randomName(),
'title' => $langcode . '_' . $fid . '_' . $this->randomName(),
);
......@@ -166,11 +166,11 @@ function testImageFieldSync() {
// have been retained.
$fids = array();
foreach ($entity->{$this->fieldName} as $delta => $item) {
$value = $values[$default_langcode][$item->fid];
$value = $values[$default_langcode][$item->target_id];
$source_item = $entity->getTranslation($langcode)->{$this->fieldName}->offsetGet($delta);
$assert = $item->fid == $source_item->fid && $item->alt == $value['alt'] && $item->title == $value['title'];
$this->assertTrue($assert, format_string('Field item @fid has been successfully synchronized.', array('@fid' => $item->fid)));
$fids[$item->fid] = TRUE;
$assert = $item->target_id == $source_item->target_id && $item->alt == $value['alt'] && $item->title == $value['title'];
$this->assertTrue($assert, format_string('Field item @fid has been successfully synchronized.', array('@fid' => $item->target_id)));
$fids[$item->target_id] = TRUE;
}
// Check that the dropped value is the right one.
......@@ -179,7 +179,7 @@ function testImageFieldSync() {
// Add back an item for the dropped value and perform synchronization again.
$values[$langcode][$removed_fid] = array(
'fid' => $removed_fid,
'target_id' => $removed_fid,
'alt' => $langcode . '_' . $removed_fid . '_' . $this->randomName(),
'title' => $langcode . '_' . $removed_fid . '_' . $this->randomName(),
);
......@@ -196,11 +196,11 @@ function testImageFieldSync() {
// When adding an item its value is copied over all the target languages,
// thus in this case the source language needs to be used to check the
// values instead of the target one.
$fid_langcode = $item->fid != $removed_fid ? $default_langcode : $langcode;
$value = $values[$fid_langcode][$item->fid];
$fid_langcode = $item->target_id != $removed_fid ? $default_langcode : $langcode;
$value = $values[$fid_langcode][$item->target_id];
$source_item = $entity->getTranslation($langcode)->{$this->fieldName}->offsetGet($delta);
$assert = $item->fid == $source_item->fid && $item->alt == $value['alt'] && $item->title == $value['title'];
$this->assertTrue($assert, format_string('Field item @fid has been successfully synchronized.', array('@fid' => $item->fid)));
$assert = $item->target_id == $source_item->target_id && $item->alt == $value['alt'] && $item->title == $value['title'];
$this->assertTrue($assert, format_string('Field item @fid has been successfully synchronized.', array('@fid' => $item->target_id)));
}
}
......
......@@ -93,7 +93,7 @@ public function getPropertyDefinitions() {
* since we cannot extend it.
*/
public static function schema(Field $field) {
$definition = \Drupal::typedData()->getDefinition('field_item:' . $field->type);
$definition = \Drupal::service('plugin.manager.entity.field.field_type')->getDefinition($field->type);
$module = $definition['module'];
module_load_install($module);
$callback = "{$module}_field_schema";
......
......@@ -38,7 +38,7 @@ function hook_file_load($files) {
$result = db_query('SELECT * FROM {upload} u WHERE u.fid IN (:fids)', array(':fids' => array_keys($files)))->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $record) {
foreach ($record as $key => $value) {
$files[$record['fid']]->$key = $value;
$files[$record['target_id']]->$key = $value;
}
}
}
......
......@@ -189,7 +189,7 @@ function _file_generic_settings_file_directory_validate($element, &$form_state)
function file_field_insert(EntityInterface $entity, $field, $instance, $langcode, &$items) {
// Add a new usage of each uploaded file.
foreach ($items as $item) {
file_usage()->add(file_load($item['fid']), 'file', $entity->entityType(), $entity->id());
file_usage()->add(file_load($item['target_id']), 'file', $entity->entityType(), $entity->id());
}
}
......@@ -203,7 +203,7 @@ function file_field_update(EntityInterface $entity, $field, $instance, $langcode
// deletion of previous file usages are necessary.
if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) {
foreach ($items as $item) {
file_usage()->add(file_load($item['fid']), 'file', $entity->entityType(), $entity->id());
file_usage()->add(file_load($item['target_id']), 'file', $entity->entityType(), $entity->id());
}
return;
}
......@@ -211,7 +211,7 @@ function file_field_update(EntityInterface $entity, $field, $instance, $langcode
// Build a display of the current FIDs.
$current_fids = array();
foreach ($items as $item) {
$current_fids[] = $item['fid'];
$current_fids[] = $item['target_id'];
}
// Compare the original field values with the ones that are being saved.
......@@ -219,18 +219,18 @@ function file_field_update(EntityInterface $entity, $field, $instance, $langcode
$original_fids = array();
if (!empty($original->{$field['field_name']}[$langcode])) {
foreach ($original->{$field['field_name']}[$langcode] as $original_item) {
$original_fids[] = $original_item['fid'];
if (isset($original_item['fid']) && !in_array($original_item['fid'], $current_fids)) {
$original_fids[] = $original_item['target_id'];
if (isset($original_item['target_id']) && !in_array($original_item['target_id'], $current_fids)) {
// Decrement the file usage count by 1.
file_usage()->delete(file_load($original_item['fid']), 'file', $entity->entityType(), $entity->id());
file_usage()->delete(file_load($original_item['target_id']), 'file', $entity->entityType(), $entity->id());
}
}
}
// Add new usage entries for newly added files.
foreach ($items as $item) {
if (!in_array($item['fid'], $original_fids)) {
file_usage()->add(file_load($item['fid']), 'file', $entity->entityType(), $entity->id());
if (!in_array($item['target_id'], $original_fids)) {
file_usage()->add(file_load($item['target_id']), 'file', $entity->entityType(), $entity->id());
}
}
}
......@@ -241,7 +241,7 @@ function file_field_update(EntityInterface $entity, $field, $instance, $langcode
function file_field_delete(EntityInterface $entity, $field, $instance, $langcode, &$items) {
// Delete all file usages within this entity.
foreach ($items as $delta => $item) {
file_usage()->delete(file_load($item['fid']), 'file', $entity->entityType(), $entity->id(), 0);
file_usage()->delete(file_load($item['target_id']), 'file', $entity->entityType(), $entity->id(), 0);
}
}
......@@ -251,7 +251,7 @@ function file_field_delete(EntityInterface $entity, $field, $instance, $langcode
function file_field_delete_revision(EntityInterface $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $item) {
// Decrement the file usage count by 1.
file_usage()->delete(file_load($item['fid']), 'file', $entity->entityType(), $entity->id());
file_usage()->delete(file_load($item['target_id']), 'file', $entity->entityType(), $entity->id());
}
}
......@@ -259,7 +259,7 @@ function file_field_delete_revision(EntityInterface $entity, $field, $instance,
* Implements hook_field_is_empty().
*/
function file_field_is_empty($item, $field_type) {
return empty($item['fid']);
return empty($item['target_id']);
}
/**
......
......@@ -5,6 +5,8 @@
* Install, update and uninstall functions for File module.
*/
use Drupal\field\Plugin\Core\Entity\Field;
/**
* Implements hook_schema().
*/
......@@ -154,10 +156,10 @@ function file_schema() {
function file_field_schema($field) {
return array(
'columns' => array(
'fid' => array(
'description' => 'The {file_managed}.fid being referenced in this field.',
'target_id' => array(
'description' => 'The ID of the target entity.',
'type' => 'int',
'not null' => FALSE,
'not null' => TRUE,
'unsigned' => TRUE,
),
'display' => array(
......@@ -175,12 +177,12 @@ function file_field_schema($field) {
),
),
'indexes' => array(
'fid' => array('fid'),
'target_id' => array('target_id'),
),
'foreign keys' => array(
'fid' => array(
'target_id' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
'columns' => array('target_id' => 'fid'),
),
),
);
......@@ -243,6 +245,12 @@ function file_update_dependencies() {
$dependencies['field'][8003] = array(
'file' => 8001,
);
// Convert the 'fid' column of file fields to 'target_id' after fields and
// instances have been moved to the config system.
$dependencies['file'][8003] = array(
'field' => 8003,
);
return $dependencies;
}
......@@ -288,3 +296,46 @@ function file_update_8002() {
db_change_field('file_usage', 'module', 'module', $spec);
}
}
/**
* Update file field tables to use target_id instead of fid.
*/
function file_update_8003() {
foreach (config_get_storage_names_with_prefix('field.field.') as $config_name) {
$field_config = config($config_name);
// Only update file fields that use the default SQL storage.
if (in_array($field_config->get('type'), array('file', 'image')) && $field_config->get('storage.type') == 'field_sql_storage') {
$field = new Field($field_config->get());
$tables = array(
_field_sql_storage_tablename($field),
_field_sql_storage_revision_tablename($field),
);
foreach ($tables as $table_name) {
// Skip fields which were created during the upgrade process.
if (!db_field_exists($table_name, $field->id() . '_fid')) {
continue 2;
}
db_change_field($table_name, $field->id() . '_fid', $field->id() . '_target_id', array(
'description' => 'The ID of the target entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
));
// Change the index.
db_drop_index($table_name, $field->id() . '_fid');
db_add_index($table_name, $field->id() . '_target_id', array($field->id() . '_target_id'));
}
// Update the indexes in field config as well.
$indexes = $field_config->get('indexes');
unset($indexes['fid']);
$indexes['target_id'] = array('target_id');
$field_config->set('indexes', $indexes);
$field_config->save();
}
}
}
......@@ -455,11 +455,11 @@ function file_field_views_data($field) {
$data = field_views_field_default_views_data($field);
foreach ($data as $table_name => $table_data) {
// Add the relationship only on the fid field.
$data[$table_name][$field['field_name'] . '_fid']['relationship'] = array(
$data[$table_name][$field['field_name'] . '_target_id']['relationship'] = array(
'id' => 'standard',
'base' => 'file_managed',
'entity type' => 'file',
'base field' => 'fid',
'base field' => 'target_id',
'label' => t('file from !field_name', array('!field_name' => $field['field_name'])),
);
}
......@@ -489,7 +489,7 @@ function file_field_views_data_views_data_alter(&$data, $field) {
'id' => 'entity_reverse',
'field_name' => $field['field_name'],
'field table' => _field_sql_storage_tablename($field),
'field field' => $field['field_name'] . '_fid',
'field field' => $field['field_name'] . '_target_id',
'base' => $entity_info['base_table'],
'base field' => $entity_info['entity_keys']['id'],
'label' => t('!field_name', array('!field_name' => $field['field_name'])),
......
......@@ -22,9 +22,9 @@ public function prepareView(array $entities, $langcode, array &$items) {
$fids = array();
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if ($this->isDisplayed($item) && !empty($item['fid'])) {
if ($this->isDisplayed($item) && !empty($item['target_id'])) {
// Load the files from the files table.
$fids[] = $item['fid'];
$fids[] = $item['target_id'];
}
}
}
......@@ -35,8 +35,8 @@ public function prepareView(array $entities, $langcode, array &$items) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// If the file does not exist, mark the entire item as empty.
if (!empty($item['fid'])) {
$items[$id][$delta]['entity'] = isset($files[$item['fid']]) ? $files[$item['fid']] : NULL;
if (!empty($item['target_id'])) {
$items[$id][$delta]['entity'] = isset($files[$item['target_id']]) ? $files[$item['target_id']] : NULL;
}
}
}
......
......@@ -209,8 +209,8 @@ public function formElement(array $items, $delta, array $element, $langcode, arr
// Field stores FID value in a single mode, so we need to transform it for
// form element to recognize it correctly.
if (!isset($items[$delta]['fids']) && isset($items[$delta]['fid'])) {
$items[$delta]['fids'][0] = $items[$delta]['fid'];
if (!isset($items[$delta]['fids']) && isset($items[$delta]['target_id'])) {
$items[$delta]['fids'][0] = $items[$delta]['target_id'];
}
$element['#default_value'] = !empty($items[$delta]) ? $items[$delta] : $defaults;
......@@ -237,7 +237,7 @@ public function massageFormValues(array $values, array $form, array &$form_state
foreach ($values as &$value) {
foreach ($value['fids'] as $fid) {
$new_value = $value;
$new_value['fid'] = $fid;
$new_value['target_id'] = $fid;
unset($new_value['fids']);
$new_values[] = $new_value;
}
......
......@@ -60,7 +60,7 @@ function testNodeDisplay() {
// Check that the default formatter is displaying with the file name.
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$default_output = theme('file_link', array('file' => $node_file));
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
......
......@@ -35,7 +35,7 @@ function testUploadPath() {
// Check that the file was uploaded to the file root.
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertPathMatch('public://' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri())));
// Change the path to contain multiple subdirectories.
......@@ -46,7 +46,7 @@ function testUploadPath() {
// Check that the file was uploaded into the subdirectory.
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid'], TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id'], TRUE);
$this->assertPathMatch('public://foo/bar/baz/' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri())));
// Check the path when used with tokens.
......@@ -58,7 +58,7 @@ function testUploadPath() {
// Check that the file was uploaded into the subdirectory.
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
// Do token replacement using the same user which uploaded the file, not
// the user running the test case.
$data = array('user' => $this->admin_user);
......
......@@ -67,7 +67,7 @@ function testFileFieldRSSContent() {
// Get the uploaded file from the node.
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
// Check that the RSS enclosure appears in the RSS feed.
$this->drupalGet('rss.xml');
......
......@@ -47,7 +47,7 @@ function testRevisions() {
// Check that the file exists on disk and in the database.
$node = node_load($nid, TRUE);
$node_file_r1 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file_r1 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$node_vid_r1 = $node->vid;
$this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
$this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
......@@ -56,7 +56,7 @@ function testRevisions() {
// Upload another file to the same node in a new revision.
$this->replaceNodeFile($test_file, $field_name, $nid);
$node = node_load($nid, TRUE);
$node_file_r2 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file_r2 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$node_vid_r2 = $node->vid;
$this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
$this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
......@@ -64,7 +64,7 @@ function testRevisions() {
// Check that the original file is still in place on the first revision.
$node = node_revision_load($node_vid_r1);
$current_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$current_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.');
$this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
$this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
......@@ -74,7 +74,7 @@ function testRevisions() {
// Check that the file is still the same as the previous revision.
$this->drupalPost('node/' . $nid . '/edit', array('revision' => '1'), t('Save and keep published'));
$node = node_load($nid, TRUE);
$node_file_r3 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file_r3 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$node_vid_r3 = $node->vid;
$this->assertEqual($node_file_r2->id(), $node_file_r3->id(), 'Previous revision file still in place after creating a new revision without a new file.');
$this->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
......@@ -82,7 +82,7 @@ function testRevisions() {
// Revert to the first revision and check that the original file is active.
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
$node = node_load($nid, TRUE);
$node_file_r4 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file_r4 = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$node_vid_r4 = $node->vid;
$this->assertEqual($node_file_r1->id(), $node_file_r4->id(), 'Original revision file still in place after reverting to the original revision.');
$this->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
......@@ -96,7 +96,7 @@ function testRevisions() {
// Attach the second file to a user.
$user = $this->drupalCreateUser();
$user->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid'] = $node_file_r3->id();
$user->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id'] = $node_file_r3->id();
$user->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['display'] = 1;
$user->save();
$this->drupalGet('user/' . $user->uid . '/edit');
......
......@@ -47,7 +47,7 @@ function testRequired() {
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, 'File exists after uploading to the required field.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
......@@ -63,7 +63,7 @@ function testRequired() {
// Create a new node with the uploaded file into the multivalue field.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
}
......@@ -93,7 +93,7 @@ function testFileMaxSize() {
// Create a new node with the small file, which should pass.
$nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize)));
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize)));
......@@ -109,7 +109,7 @@ function testFileMaxSize() {
// Upload the big file successfully.
$nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->getSize()))));
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->getSize()))));
}
......@@ -131,7 +131,7 @@ function testFileExtension() {
// Check that the file can be uploaded with no extension checking.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
......@@ -149,7 +149,7 @@ function testFileExtension() {
// Check that the file can be uploaded with extension checking.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
}
......
......@@ -46,7 +46,7 @@ function testSingleValuedWidget() {
// does not yet support file uploads.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
// Ensure the file can be downloaded.
......@@ -79,7 +79,7 @@ function testSingleValuedWidget() {
// Save the node and ensure it does not have the file.
$this->drupalPost(NULL, array(), t('Save and keep published'));
$node = node_load($nid, TRUE);
$this->assertTrue(empty($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']), 'File was successfully removed from the node.');
$this->assertTrue(empty($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']), 'File was successfully removed from the node.');
}
}
......@@ -196,7 +196,7 @@ function testMultiValuedWidget() {
preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
$nid = $matches[1];
$node = node_load($nid, TRUE);
$this->assertTrue(empty($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']), 'Node was successfully saved without any files.');
$this->assertTrue(empty($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']), 'Node was successfully saved without any files.');
}
}
......@@ -216,7 +216,7 @@ function testPrivateFileSetting() {
$this->drupalPost("admin/structure/types/manage/$type_name/fields/$instance->id/field", $edit, t('Save field settings'));
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
// Ensure the private file is available to the user who uploaded it.
......
......@@ -67,7 +67,7 @@ public function setUp() {
public function testFileItem() {
// Create a test entity with the
$entity = entity_create('entity_test', array());
$entity->file_test->fid = $this->file->id();
$entity->file_test->target_id = $this->file->id();
$entity->file_test->display = 1;
$entity->file_test->description = $description = $this->randomName();
$entity->name->value = $this->randomName();
......@@ -76,7 +76,7 @@ public function testFileItem() {
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->file_test instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->file_test->fid, $this->file->id());
$this->assertEqual($entity->file_test->target_id, $this->file->id());
$this->assertEqual($entity->file_test->display, 1);
$this->assertEqual($entity->file_test->description, $description);
$this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
......@@ -90,7 +90,7 @@ public function testFileItem() {
));
$file2->save();
$entity->file_test->fid = $file2->id();
$entity->file_test->target_id = $file2->id();
$this->assertEqual($entity->file_test->entity->id(), $file2->id());
$this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
}
......
......@@ -50,7 +50,7 @@ function testPrivateFile() {
$test_file = $this->getTestFile('text');
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, array('private' => TRUE));
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
// Ensure the file can be downloaded.
$this->drupalGet(file_create_url($node_file->getFileUri()));
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
......@@ -62,7 +62,7 @@ function testPrivateFile() {
$this->drupalLogin($this->admin_user);
$nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, array('private' => TRUE));
$node = node_load($nid, TRUE);
$node_file = file_load($node->{$no_access_field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$node_file = file_load($node->{$no_access_field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
// Ensure the file cannot be downloaded.
$this->drupalGet(file_create_url($node_file->getFileUri()));
$this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission.');
......
......@@ -47,7 +47,7 @@ function testFileTokenReplacement() {
// Load the node and the file.
$node = node_load($nid, TRUE);
$file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
$file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['target_id']);
// Generate and test sanitized tokens.
$tests = array();
......
......@@ -7,12 +7,12 @@
namespace Drupal\file\Type;
use Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem;
use Drupal\field\Plugin\Type\FieldType\ConfigEntityReferenceItemBase;
/**
* Defines the 'file_field' entity field item.
*/
class FileItem extends LegacyConfigFieldItem {
class FileItem extends ConfigEntityReferenceItemBase {
/**
* Property definitions of the contained properties.
......@@ -24,14 +24,14 @@ class FileItem extends LegacyConfigFieldItem {
static $propertyDefinitions;
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
* {@inheritdoc}
*/
public function getPropertyDefinitions() {
$this->definition['settings']['target_type'] = 'file';
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['fid'] = array(
'type' => 'integer',
'label' => t('Referenced file id.'),
);
static::$propertyDefinitions = parent::getPropertyDefinitions();
static::$propertyDefinitions['display'] = array(
'type' => 'boolean',
'label' => t('Flag to control whether this file should be displayed when viewing content.'),
......@@ -40,39 +40,8 @@ public function getPropertyDefinitions() {
'type' => 'string',
'label' => t('A description of the file.'),
);
static::$propertyDefinitions['entity'] = array(
'type' => 'entity',
'constraints' => array(
'EntityType' => 'file',
),
'label' => t('File'),
'description' => t('The referenced file'),
// The entity object is computed out of the fid.
'computed' => TRUE,
'read-only' => FALSE,
'settings' => array('id source' => 'fid'),
);
}
return static::$propertyDefinitions;
}
/**
* Overrides \Drupal\Core\Entity\Field\FieldItemBase::get().
*/
public function setValue($values, $notify = TRUE) {
// Treat the values as value of the entity property, if no array is
// given as this handles entity IDs and objects.
if (isset($values) && !is_array($values)) {
// Directly update the property instead of invoking the parent, so that
// the entity property can take care of updating the ID property.
$this->properties['entity']->setValue($values, $notify);
}
else {
// Make sure that the 'entity' property gets set as 'target_id'.
if (isset($values['fid']) && !isset($values['entity'])) {
$values['entity'] = $values['fid'];
}
parent::setValue($values, $notify);
}
}
}
......@@ -22,7 +22,7 @@ function image_field_info() {
'column_groups' => array(
'file' => array(
'label' => t('File'),
'columns' => array('fid', 'width', 'height'),
'columns' => array('target_id', 'width', 'height'),
),
'alt' => array(
'label' => t('Alt'),
......@@ -230,7 +230,7 @@ function image_field_presave(EntityInterface $entity, $field, $instance, $langco
// Determine the dimensions if necessary.
foreach ($items as &$item) {
if (!isset($item['width']) || !isset($item['height'])) {
$info = image_get_info(file_load($item['fid'])->getFileUri());
$info = image_get_info(file_load($item['target_id'])->getFileUri());
if (is_array($info)) {
$item['width'] = $info['width'];
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Component\Uuid\Uuid;
use Drupal\field\Plugin\Core\Entity\Field;
/**
* Implements hook_install().
......@@ -30,10 +31,10 @@ function image_uninstall() {
function image_field_schema($field) {
return array(
'columns' => array(
'fid' => array(
'description' => 'The {file_managed}.fid being referenced in this field.',
'target_id' => array(