Commit 0485e555 authored by catch's avatar catch

Issue #2357801 by dawehner, amitgoyal: File field default values are not...

Issue #2357801 by dawehner, amitgoyal: File field default values are not deployable -- use UUIDs for the default file
parent 503e46bb
...@@ -13,8 +13,8 @@ image_size: ...@@ -13,8 +13,8 @@ image_size:
field_default_image: field_default_image:
type: mapping type: mapping
mapping: mapping:
fid: uuid:
type: integer type: string
label: 'Image' label: 'Image'
alt: alt:
type: label type: label
......
...@@ -340,18 +340,18 @@ function image_entity_presave(EntityInterface $entity) { ...@@ -340,18 +340,18 @@ function image_entity_presave(EntityInterface $entity) {
return; return;
} }
$fid = $entity->settings['default_image']['fid']; $uuid = $entity->settings['default_image']['uuid'];
if ($fid) { if ($uuid) {
$original_fid = isset($entity->original) ? $entity->original->settings['default_image']['fid'] : NULL; $original_uuid = isset($entity->original) ? $entity->original->settings['default_image']['uuid'] : NULL;
if ($fid != $original_fid) { if ($uuid != $original_uuid) {
$file = file_load($fid); $file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid);
if ($file) { if ($file) {
$image = \Drupal::service('image.factory')->get($file->getFileUri()); $image = \Drupal::service('image.factory')->get($file->getFileUri());
$entity->settings['default_image']['width'] = $image->getWidth(); $entity->settings['default_image']['width'] = $image->getWidth();
$entity->settings['default_image']['height'] = $image->getHeight(); $entity->settings['default_image']['height'] = $image->getHeight();
} }
else { else {
$entity->settings['default_image']['fid'] = NULL; $entity->settings['default_image']['uuid'] = NULL;
} }
} }
} }
...@@ -371,12 +371,12 @@ function image_field_storage_config_update(FieldStorageConfigInterface $field_st ...@@ -371,12 +371,12 @@ function image_field_storage_config_update(FieldStorageConfigInterface $field_st
$prior_field_storage = $field_storage->original; $prior_field_storage = $field_storage->original;
// The value of a managed_file element can be an array if #extended == TRUE. // The value of a managed_file element can be an array if #extended == TRUE.
$fid_new = $field_storage->settings['default_image']['fid']; $uuid_new = $field_storage->settings['default_image']['uuid'];
$fid_old = $prior_field_storage->settings['default_image']['fid']; $uuid_old = $prior_field_storage->settings['default_image']['uuid'];
$file_new = $fid_new ? file_load($fid_new) : FALSE; $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE;
if ($fid_new != $fid_old) { if ($uuid_new != $uuid_old) {
// Is there a new file? // Is there a new file?
if ($file_new) { if ($file_new) {
...@@ -386,7 +386,7 @@ function image_field_storage_config_update(FieldStorageConfigInterface $field_st ...@@ -386,7 +386,7 @@ function image_field_storage_config_update(FieldStorageConfigInterface $field_st
} }
// Is there an old file? // Is there an old file?
if ($fid_old && ($file_old = file_load($fid_old))) { if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) {
\Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid()); \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid());
} }
} }
...@@ -411,12 +411,12 @@ function image_field_config_update(FieldConfigInterface $field) { ...@@ -411,12 +411,12 @@ function image_field_config_update(FieldConfigInterface $field) {
$prior_instance = $field->original; $prior_instance = $field->original;
$fid_new = $field->settings['default_image']['fid']; $uuid_new = $field->settings['default_image']['uuid'];
$fid_old = $prior_instance->settings['default_image']['fid']; $uuid_old = $prior_instance->settings['default_image']['uuid'];
// If the old and new files do not match, update the default accordingly. // If the old and new files do not match, update the default accordingly.
$file_new = $fid_new ? file_load($fid_new) : FALSE; $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE;
if ($fid_new != $fid_old) { if ($uuid_new != $uuid_old) {
// Save the new file, if present. // Save the new file, if present.
if ($file_new) { if ($file_new) {
$file_new->status = FILE_STATUS_PERMANENT; $file_new->status = FILE_STATUS_PERMANENT;
...@@ -424,7 +424,7 @@ function image_field_config_update(FieldConfigInterface $field) { ...@@ -424,7 +424,7 @@ function image_field_config_update(FieldConfigInterface $field) {
\Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid()); \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid());
} }
// Delete the old file, if present. // Delete the old file, if present.
if ($fid_old && ($file_old = file_load($fid_old))) { if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) {
\Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid()); \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid());
} }
} }
...@@ -447,8 +447,8 @@ function image_field_storage_config_delete(FieldStorageConfigInterface $field) { ...@@ -447,8 +447,8 @@ function image_field_storage_config_delete(FieldStorageConfigInterface $field) {
} }
// The value of a managed_file element can be an array if #extended == TRUE. // The value of a managed_file element can be an array if #extended == TRUE.
$fid = $field->settings['default_image']['fid']; $uuid = $field->settings['default_image']['uuid'];
if ($fid && ($file = file_load($fid))) { if ($uuid && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid))) {
\Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid()); \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid());
} }
} }
...@@ -464,10 +464,10 @@ function image_field_config_delete(FieldConfigInterface $field) { ...@@ -464,10 +464,10 @@ function image_field_config_delete(FieldConfigInterface $field) {
} }
// The value of a managed_file element can be an array if #extended == TRUE. // The value of a managed_file element can be an array if #extended == TRUE.
$fid = $field->settings['default_image']['fid']; $uuid = $field->settings['default_image']['uuid'];
// Remove the default image when the instance is deleted. // Remove the default image when the instance is deleted.
if ($fid && ($file = file_load($fid))) { if ($uuid && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $uuid))) {
\Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid()); \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid());
} }
} }
...@@ -28,11 +28,11 @@ public function prepareView(array $entities_items) { ...@@ -28,11 +28,11 @@ public function prepareView(array $entities_items) {
$default_image = $this->getFieldSetting('default_image'); $default_image = $this->getFieldSetting('default_image');
// If we are dealing with a configurable field, look in both // If we are dealing with a configurable field, look in both
// instance-level and field-level settings. // instance-level and field-level settings.
if (empty($default_image['fid']) && $this->fieldDefinition instanceof FieldConfigInterface) { if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
$default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image'); $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
} }
if (!empty($default_image['fid']) && ($file = file_load($default_image['fid']))) { if (!empty($default_image['uuid']) && ($file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']))) {
$items->setValue(array(array( $items->setValue(array(array(
'is_default' => TRUE, 'is_default' => TRUE,
'alt' => $default_image['alt'], 'alt' => $default_image['alt'],
......
...@@ -47,13 +47,20 @@ ...@@ -47,13 +47,20 @@
*/ */
class ImageItem extends FileItem { class ImageItem extends FileItem {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function defaultStorageSettings() { public static function defaultStorageSettings() {
return array( return array(
'default_image' => array( 'default_image' => array(
'fid' => NULL, 'uuid' => NULL,
'alt' => '', 'alt' => '',
'title' => '', 'title' => '',
'width' => NULL, 'width' => NULL,
...@@ -75,7 +82,7 @@ public static function defaultFieldSettings() { ...@@ -75,7 +82,7 @@ public static function defaultFieldSettings() {
'max_resolution' => '', 'max_resolution' => '',
'min_resolution' => '', 'min_resolution' => '',
'default_image' => array( 'default_image' => array(
'fid' => NULL, 'uuid' => NULL,
'alt' => '', 'alt' => '',
'title' => '', 'title' => '',
'width' => NULL, 'width' => NULL,
...@@ -397,11 +404,16 @@ protected function defaultImageForm(array &$element, array $settings) { ...@@ -397,11 +404,16 @@ protected function defaultImageForm(array &$element, array $settings) {
'#title' => t('Default image'), '#title' => t('Default image'),
'#open' => TRUE, '#open' => TRUE,
); );
$element['default_image']['fid'] = array( // Convert the stored UUID to a FID.
$fids = [];
if ($file = $this->getEntityManager()->loadEntityByUuid('file', $settings['default_image']['uuid'])) {
$fids[0] = $file->id();
}
$element['default_image']['uuid'] = array(
'#type' => 'managed_file', '#type' => 'managed_file',
'#title' => t('Image'), '#title' => t('Image'),
'#description' => t('Image to be shown if no image is uploaded.'), '#description' => t('Image to be shown if no image is uploaded.'),
'#default_value' => empty($settings['default_image']['fid']) ? array() : array($settings['default_image']['fid']), '#default_value' => $fids,
'#upload_location' => $settings['uri_scheme'] . '://default_images/', '#upload_location' => $settings['uri_scheme'] . '://default_images/',
'#element_validate' => array( '#element_validate' => array(
'\Drupal\file\Element\ManagedFile::validateManagedFile', '\Drupal\file\Element\ManagedFile::validateManagedFile',
...@@ -450,9 +462,13 @@ public static function validateDefaultImageForm(array &$element, FormStateInterf ...@@ -450,9 +462,13 @@ public static function validateDefaultImageForm(array &$element, FormStateInterf
// for default image is not TRUE and this is a single value. // for default image is not TRUE and this is a single value.
if (isset($element['fids']['#value'][0])) { if (isset($element['fids']['#value'][0])) {
$value = $element['fids']['#value'][0]; $value = $element['fids']['#value'][0];
// Convert the file ID to a uuid.
if ($file = \Drupal::entityManager()->getStorage('file')->load($value)) {
$value = $file->uuid();
}
} }
else { else {
$value = 0; $value = '';
} }
$form_state->setValueForElement($element, $value); $form_state->setValueForElement($element, $value);
} }
...@@ -465,4 +481,16 @@ public function isDisplayed() { ...@@ -465,4 +481,16 @@ public function isDisplayed() {
return TRUE; return TRUE;
} }
/**
* Gets the entity manager.
*
* @return \Drupal\Core\Entity\EntityManagerInterface.
*/
protected function getEntityManager() {
if (!isset($this->entityManager)) {
$this->entityManager = \Drupal::entityManager();
}
return $this->entityManager;
}
} }
...@@ -138,9 +138,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ...@@ -138,9 +138,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Default image. // Default image.
$default_image = $field_settings['default_image']; $default_image = $field_settings['default_image'];
if (empty($default_image['fid'])) { if (empty($default_image['uuid'])) {
$default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image'); $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
} }
// Convert the stored UUID into a file ID.
if (!empty($default_image['uuid']) && $entity = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid'])) {
$default_image['fid'] = $entity->id();
}
$element['#default_image'] = !empty($default_image['fid']) ? $default_image : array(); $element['#default_image'] = !empty($default_image['fid']) ? $default_image : array();
return $element; return $element;
......
...@@ -48,14 +48,14 @@ public function testDefaultImages() { ...@@ -48,14 +48,14 @@ public function testDefaultImages() {
// Create an image field and add an field to the article content type. // Create an image field and add an field to the article content type.
$field_name = strtolower($this->randomMachineName()); $field_name = strtolower($this->randomMachineName());
$storage_settings['default_image'] = array( $storage_settings['default_image'] = array(
'fid' => $default_images['field']->id(), 'uuid' => $default_images['field']->uuid(),
'alt' => '', 'alt' => '',
'title' => '', 'title' => '',
'width' => 0, 'width' => 0,
'height' => 0, 'height' => 0,
); );
$field_settings['default_image'] = array( $field_settings['default_image'] = array(
'fid' => $default_images['field']->id(), 'uuid' => $default_images['field']->uuid(),
'alt' => '', 'alt' => '',
'title' => '', 'title' => '',
'width' => 0, 'width' => 0,
...@@ -67,18 +67,18 @@ public function testDefaultImages() { ...@@ -67,18 +67,18 @@ public function testDefaultImages() {
$field = $this->createImageField($field_name, 'article', $storage_settings, $field_settings, $widget_settings); $field = $this->createImageField($field_name, 'article', $storage_settings, $field_settings, $widget_settings);
// The field default image id should be 2. // The field default image id should be 2.
$this->assertEqual($field->getSetting('default_image')['fid'], $default_images['field']->id()); $this->assertEqual($field->getSetting('default_image')['uuid'], $default_images['field']->uuid());
// Also test \Drupal\field\Entity\FieldConfig::getSetting(). // Also test \Drupal\field\Entity\FieldConfig::getSetting().
$this->assertEqual($field->getSettings()['default_image']['fid'], $default_images['field']->id()); $this->assertEqual($field->getSettings()['default_image']['uuid'], $default_images['field']->uuid());
$field_storage = $field->getFieldStorageDefinition(); $field_storage = $field->getFieldStorageDefinition();
// The field default image id should be 1. // The field default image id should be 1.
$this->assertEqual($field_storage->getSetting('default_image')['fid'], $default_images['field']->id()); $this->assertEqual($field_storage->getSetting('default_image')['uuid'], $default_images['field']->uuid());
// Also test \Drupal\field\Entity\FieldStorageConfig::getSettings(). // Also test \Drupal\field\Entity\FieldStorageConfig::getSettings().
$this->assertEqual($field_storage->getSettings()['default_image']['fid'], $default_images['field']->id()); $this->assertEqual($field_storage->getSettings()['default_image']['uuid'], $default_images['field']->uuid());
// Add another field with another default image to the page content type. // Add another field with another default image to the page content type.
$field2 = entity_create('field_config', array( $field2 = entity_create('field_config', array(
...@@ -88,7 +88,7 @@ public function testDefaultImages() { ...@@ -88,7 +88,7 @@ public function testDefaultImages() {
'required' => $field->required, 'required' => $field->required,
'settings' => array( 'settings' => array(
'default_image' => array( 'default_image' => array(
'fid' => $default_images['field2']->id(), 'uuid' => $default_images['field2']->uuid(),
'alt' => '', 'alt' => '',
'title' => '', 'title' => '',
'width' => 0, 'width' => 0,
...@@ -110,7 +110,7 @@ public function testDefaultImages() { ...@@ -110,7 +110,7 @@ public function testDefaultImages() {
$field_id = $field->id(); $field_id = $field->id();
$this->drupalGet("admin/structure/types/manage/article/fields/$field_id/storage"); $this->drupalGet("admin/structure/types/manage/article/fields/$field_id/storage");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field_storage[settings][default_image][fid][fids]"]', '//input[@name="field_storage[settings][default_image][uuid][fids]"]',
$default_images['field']->id(), $default_images['field']->id(),
format_string( format_string(
'Article image field default equals expected file ID of @fid.', 'Article image field default equals expected file ID of @fid.',
...@@ -120,7 +120,7 @@ public function testDefaultImages() { ...@@ -120,7 +120,7 @@ public function testDefaultImages() {
// Confirm the defaults are present on the article field edit form. // Confirm the defaults are present on the article field edit form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_id"); $this->drupalGet("admin/structure/types/manage/article/fields/$field_id");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field[settings][default_image][fid][fids]"]', '//input[@name="field[settings][default_image][uuid][fids]"]',
$default_images['field']->id(), $default_images['field']->id(),
format_string( format_string(
'Article image field field default equals expected file ID of @fid.', 'Article image field field default equals expected file ID of @fid.',
...@@ -131,7 +131,7 @@ public function testDefaultImages() { ...@@ -131,7 +131,7 @@ public function testDefaultImages() {
// Confirm the defaults are present on the page field settings form. // Confirm the defaults are present on the page field settings form.
$this->drupalGet("admin/structure/types/manage/page/fields/$field_id/storage"); $this->drupalGet("admin/structure/types/manage/page/fields/$field_id/storage");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field_storage[settings][default_image][fid][fids]"]', '//input[@name="field_storage[settings][default_image][uuid][fids]"]',
$default_images['field']->id(), $default_images['field']->id(),
format_string( format_string(
'Page image field default equals expected file ID of @fid.', 'Page image field default equals expected file ID of @fid.',
...@@ -142,7 +142,7 @@ public function testDefaultImages() { ...@@ -142,7 +142,7 @@ public function testDefaultImages() {
$field2_id = $field2->id(); $field2_id = $field2->id();
$this->drupalGet("admin/structure/types/manage/page/fields/$field2_id"); $this->drupalGet("admin/structure/types/manage/page/fields/$field2_id");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field[settings][default_image][fid][fids]"]', '//input[@name="field[settings][default_image][uuid][fids]"]',
$default_images['field2']->id(), $default_images['field2']->id(),
format_string( format_string(
'Page image field field default equals expected file ID of @fid.', 'Page image field field default equals expected file ID of @fid.',
...@@ -175,13 +175,13 @@ public function testDefaultImages() { ...@@ -175,13 +175,13 @@ public function testDefaultImages() {
); );
// Upload a new default for the field storage. // Upload a new default for the field storage.
$field_storage->settings['default_image']['fid'] = $default_images['field_new']->id(); $field_storage->settings['default_image']['uuid'] = $default_images['field_new']->uuid();
$field_storage->save(); $field_storage->save();
// Confirm that the new default is used on the article field settings form. // Confirm that the new default is used on the article field settings form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_id/storage"); $this->drupalGet("admin/structure/types/manage/article/fields/$field_id/storage");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field_storage[settings][default_image][fid][fids]"]', '//input[@name="field_storage[settings][default_image][uuid][fids]"]',
$default_images['field_new']->id(), $default_images['field_new']->id(),
format_string( format_string(
'Updated image field default equals expected file ID of @fid.', 'Updated image field default equals expected file ID of @fid.',
...@@ -211,14 +211,14 @@ public function testDefaultImages() { ...@@ -211,14 +211,14 @@ public function testDefaultImages() {
); );
// Upload a new default for the article's field field. // Upload a new default for the article's field field.
$field->settings['default_image']['fid'] = $default_images['field_new']->id(); $field->settings['default_image']['uuid'] = $default_images['field_new']->uuid();
$field->save(); $field->save();
// Confirm the new field field default is used on the article field // Confirm the new field field default is used on the article field
// admin form. // admin form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_id"); $this->drupalGet("admin/structure/types/manage/article/fields/$field_id");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field[settings][default_image][fid][fids]"]', '//input[@name="field[settings][default_image][uuid][fids]"]',
$default_images['field_new']->id(), $default_images['field_new']->id(),
format_string( format_string(
'Updated article image field field default equals expected file ID of @fid.', 'Updated article image field field default equals expected file ID of @fid.',
...@@ -256,13 +256,13 @@ public function testDefaultImages() { ...@@ -256,13 +256,13 @@ public function testDefaultImages() {
$this->assertRaw($file->getFilename()); $this->assertRaw($file->getFilename());
// Remove the instance default from articles. // Remove the instance default from articles.
$field->settings['default_image']['fid'] = 0; $field->settings['default_image']['uuid'] = 0;
$field->save(); $field->save();
// Confirm the article field field default has been removed. // Confirm the article field field default has been removed.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_id"); $this->drupalGet("admin/structure/types/manage/article/fields/$field_id");
$this->assertFieldByXpath( $this->assertFieldByXpath(
'//input[@name="field[settings][default_image][fid][fids]"]', '//input[@name="field[settings][default_image][uuid][fids]"]',
'', '',
'Updated article image field field default has been successfully removed.' 'Updated article image field field default has been successfully removed.'
); );
...@@ -291,7 +291,7 @@ public function testDefaultImages() { ...@@ -291,7 +291,7 @@ public function testDefaultImages() {
); );
$non_image = $this->drupalGetTestFiles('text'); $non_image = $this->drupalGetTestFiles('text');
$this->drupalPostForm(NULL, array('files[field_settings_default_image_fid]' => drupal_realpath($non_image[0]->uri)), t("Upload")); $this->drupalPostForm(NULL, array('files[field_settings_default_image_uuid]' => drupal_realpath($non_image[0]->uri)), t("Upload"));
$this->assertText(t('The specified file text-0.txt could not be uploaded. Only files with the following extensions are allowed: png gif jpg jpeg.'), 'Non-image file cannot be used as default image.'); $this->assertText(t('The specified file text-0.txt could not be uploaded. Only files with the following extensions are allowed: png gif jpg jpeg.'), 'Non-image file cannot be used as default image.');
// Confirm the default image is shown on the node form. // Confirm the default image is shown on the node form.
...@@ -310,14 +310,14 @@ public function testInvalidDefaultImage() { ...@@ -310,14 +310,14 @@ public function testInvalidDefaultImage() {
'type' => 'image', 'type' => 'image',
'settings' => array( 'settings' => array(
'default_image' => array( 'default_image' => array(
'fid' => 100000, 'uuid' => 100000,
) )
), ),
)); ));
$field_storage->save(); $field_storage->save();
$settings = $field_storage->getSettings(); $settings = $field_storage->getSettings();
// The non-existent default image should not be saved. // The non-existent default image should not be saved.
$this->assertNull($settings['default_image']['fid']); $this->assertNull($settings['default_image']['uuid']);
$field = entity_create('field_config', array( $field = entity_create('field_config', array(
'field_storage' => $field_storage, 'field_storage' => $field_storage,
...@@ -325,14 +325,14 @@ public function testInvalidDefaultImage() { ...@@ -325,14 +325,14 @@ public function testInvalidDefaultImage() {
'label' => $this->randomMachineName(), 'label' => $this->randomMachineName(),
'settings' => array( 'settings' => array(
'default_image' => array( 'default_image' => array(
'fid' => 100000, 'uuid' => 100000,
) )
), ),
)); ));
$field->save(); $field->save();
$settings = $field->getSettings(); $settings = $field->getSettings();
// The non-existent default image should not be saved. // The non-existent default image should not be saved.
$this->assertNull($settings['default_image']['fid']); $this->assertNull($settings['default_image']['uuid']);
} }
} }
...@@ -318,7 +318,7 @@ function testImageFieldDefaultImage() { ...@@ -318,7 +318,7 @@ function testImageFieldDefaultImage() {
$alt = $this->randomString(512); $alt = $this->randomString(512);
$title = $this->randomString(1024); $title = $this->randomString(1024);
$edit = array( $edit = array(
'files[field_storage_settings_default_image_fid]' => drupal_realpath($images[0]->uri), 'files[field_storage_settings_default_image_uuid]' => drupal_realpath($images[0]->uri),
'field_storage[settings][default_image][alt]' => $alt, 'field_storage[settings][default_image][alt]' => $alt,
'field_storage[settings][default_image][title]' => $title, 'field_storage[settings][default_image][title]' => $title,
); );
...@@ -327,7 +327,7 @@ function testImageFieldDefaultImage() { ...@@ -327,7 +327,7 @@ function testImageFieldDefaultImage() {
\Drupal::entityManager()->clearCachedFieldDefinitions(); \Drupal::entityManager()->clearCachedFieldDefinitions();
$field_storage = FieldStorageConfig::loadByName('node', $field_name); $field_storage = FieldStorageConfig::loadByName('node', $field_name);
$default_image = $field_storage->getSetting('default_image'); $default_image = $field_storage->getSetting('default_image');
$file = file_load($default_image['fid']); $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']);
$this->assertTrue($file->isPermanent(), 'The default image status is permanent.'); $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
$image = array( $image = array(
'#theme' => 'image', '#theme' => 'image',
...@@ -363,21 +363,21 @@ function testImageFieldDefaultImage() { ...@@ -363,21 +363,21 @@ function testImageFieldDefaultImage() {
// Remove default image from the field and make sure it is no longer used. // Remove default image from the field and make sure it is no longer used.
$edit = array( $edit = array(
'field_storage[settings][default_image][fid][fids]' => 0, 'field_storage[settings][default_image][uuid][fids]' => 0,
); );
$this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings')); $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings'));
// Clear field definition cache so the new default image is detected. // Clear field definition cache so the new default image is detected.
\Drupal::entityManager()->clearCachedFieldDefinitions(); \Drupal::entityManager()->clearCachedFieldDefinitions();
$field_storage = FieldStorageConfig::loadByName('node', $field_name); $field_storage = FieldStorageConfig::loadByName('node', $field_name);
$default_image = $field_storage->getSetting('default_image'); $default_image = $field_storage->getSetting('default_image');
$this->assertFalse($default_image['fid'], 'Default image removed from field.'); $this->assertFalse($default_image['uuid'], 'Default image removed from field.');
// Create an image field that uses the private:// scheme and test that the // Create an image field that uses the private:// scheme and test that the
// default image works as expected. // default image works as expected.
$private_field_name = strtolower($this->randomMachineName()); $private_field_name = strtolower($this->randomMachineName());
$this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private')); $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
// Add a default image to the new field. // Add a default image to the new field.
$edit = array( $edit = array(
'files[field_storage_settings_default_image_fid]' => drupal_realpath($images[1]->uri), 'files[field_storage_settings_default_image_uuid]' => drupal_realpath($images[1]->uri),
'field_storage[settings][default_image][alt]' => $alt, 'field_storage[settings][default_image][alt]' => $alt,
'field_storage[settings][default_image][title]' => $title, 'field_storage[settings][default_image][title]' => $title,
); );
...@@ -387,7 +387,7 @@ function testImageFieldDefaultImage() { ...@@ -387,7 +387,7 @@ function testImageFieldDefaultImage() {
$private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name); $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name);
$default_image = $private_field_storage->getSetting('default_image'); $default_image = $private_field_storage->getSetting('default_image');
$file = file_load($default_image['fid']); $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']);
$this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.'); $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.');
$this->assertTrue($file->isPermanent(), 'The default image status is permanent.'); $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
// Create a new node with no image attached and ensure that default private // Create a new node with no image attached and ensure that default private
......
...@@ -72,7 +72,7 @@ public function testFields() { ...@@ -72,7 +72,7 @@ public function testFields() {
$settings = $field_storage->getSettings(); $settings = $field_storage->getSettings();
$this->assertEqual($settings['target_type'], 'file'); $this->assertEqual($settings['target_type'], 'file');
$this->assertEqual($settings['uri_scheme'], 'public'); $this->assertEqual($settings['uri_scheme'], 'public');
$this->assertEqual($settings['default_image']['fid'], ''); $this->assertEqual($settings['default_image']['uuid'], '');
$this->assertEqual(array_filter($settings['default_image']), array()); $this->assertEqual(array_filter($settings['default_image']), array());
// Phone field. // Phone field.
......
<