diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 472cad4..56a6d1e 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -390,9 +390,11 @@ function image_entity_presave(EntityInterface $entity) { $entity_type_id = $entity->getEntityTypeId(); if ($entity_type_id == 'field_instance_config') { $field = $entity->getField(); + $default_settings = \Drupal::service('plugin.manager.field.field_type')->getDefaultInstanceSettings('image'); } elseif ($entity_type_id == 'field_config') { $field = $entity; + $default_settings = \Drupal::service('plugin.manager.field.field_type')->getDefaultSettings('image'); } // Exit, if not saving an image field or image field instance entity. if (!$field || $field->type != 'image') { @@ -403,20 +405,19 @@ function image_entity_presave(EntityInterface $entity) { if ($fid) { $original_fid = isset($entity->original) ? $entity->original->settings['default_image']['fid'] : NULL; if ($fid != $original_fid) { - $image = \Drupal::service('image.factory')->get(file_load($fid)->getFileUri()); - $entity->settings['default_image']['width'] = $image->getWidth(); - $entity->settings['default_image']['height'] = $image->getHeight(); + $file = file_load($fid); + if ($file) { + $image = \Drupal::service('image.factory')->get($file->getFileUri()); + $entity->settings['default_image']['width'] = $image->getWidth(); + $entity->settings['default_image']['height'] = $image->getHeight(); + } + else { + $entity->settings['default_image']['fid'] = NULL; + } } } - else { - $entity->settings['default_image'] = array( - 'fid' => NULL, - 'alt' => '', - 'title' => '', - 'width' => NULL, - 'height' => NULL, - ); - } + + $entity->settings['default_image'] += $default_settings['default_image']; } /** diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php index d162cd3..7489011 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php @@ -289,4 +289,43 @@ public function testDefaultImages() { ); } + /** + * Tests image field and instance having an invalid default image. + */ + public function testInvalidDefaultImage() { + $field = array( + 'name' => drupal_strtolower($this->randomName()), + 'entity_type' => 'node', + 'type' => 'image', + 'settings' => array( + 'default_image' => array( + 'fid' => mt_rand(100, 999999), + ) + ), + ); + $instance = array( + 'field_name' => $field['name'], + 'label' => $this->randomName(), + 'entity_type' => 'node', + 'bundle' => 'page', + 'settings' => array( + 'default_image' => array( + 'fid' => mt_rand(100, 999999), + ) + ), + ); + $field_config = entity_create('field_config', $field); + $field_config->save(); + $settings = $field_config->getSettings(); + // The non-existent default image should not be saved. + $this->assertNull($settings['default_image']['fid']); + + $field_instance_config = entity_create('field_instance_config', $instance); + $field_instance_config->save(); + $settings = $field_instance_config->getSettings(); + // The non-existent default image should not be saved. + $this->assertNull($settings['default_image']['fid']); + + } + } diff --git a/core/modules/user/lib/Drupal/user/Tests/UserCreateTest.php b/core/modules/user/lib/Drupal/user/Tests/UserCreateTest.php index cc6d2ad..0acb310 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserCreateTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserCreateTest.php @@ -37,6 +37,14 @@ protected function testUserAdd() { $user = $this->drupalCreateUser(array('administer users')); $this->drupalLogin($user); + $default_image = array( + 'fid' => NULL, + 'alt' => '', + 'title' => '', + 'width' => NULL, + 'height' => NULL + ); + // Create a field and an instance. $field_name = 'test_field'; $field = array( @@ -49,7 +57,7 @@ protected function testUserAdd() { 'indexes' => array('target_id' => array('target_id')), 'settings' => array( 'uri_scheme' => 'public', - 'default_image' => FALSE, + 'default_image' => $default_image, ), ); entity_create('field_config', $field)->save(); @@ -69,7 +77,7 @@ protected function testUserAdd() { 'title_field' => 0, 'max_resolution' => '85x85', 'min_resolution' => '', - 'default_image' => 0, + 'default_image' => $default_image, ), ); entity_create('field_instance_config', $instance)->save();