diff --git a/core/modules/field/field.install b/core/modules/field/field.install index dfd61a7..f7949d4 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -583,15 +583,22 @@ function field_update_8006(&$sandbox) { * Convert image field and instance setting 'default_image'. */ function field_update_8007(&$sandbox) { + $image_factory = \Drupal::service('image.factory'); foreach (array('field', 'instance') as $type) { $prefix = "field.$type"; foreach (config_get_storage_names_with_prefix($prefix) as $config_id) { $config = \Drupal::config($config_id); if (($type == 'field' && $config->get('type') == 'image') || ($type == 'instance' && $config->get('field_type') == 'image')) { + if ($fid = $config->get('settings.default_image')) { + $file = file_load($fid); + $image = $image_factory->get($file->getFileUri()); + } $default_image = array( - 'fid' => $config->get('settings.default_image'), + 'fid' => (int) $fid, 'alt' => '', 'title' => '', + 'width' => $fid ? $image->getWidth() : 0, + 'height' => $fid ? $image->getHeight() : 0, ); $config ->set('settings.default_image', $default_image) diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 0254cbf..d465aba 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -419,10 +419,22 @@ function image_entity_presave(EntityInterface $entity, $type) { } if ($field && $field->type == 'image' && is_array($entity->settings['default_image']['fid'])) { if (!empty($entity->settings['default_image']['fid'][0])) { - $entity->settings['default_image']['fid'] = $entity->settings['default_image']['fid'][0]; + $fid = $entity->settings['default_image']['fid'][0]; + $entity->settings['default_image']['fid'] = $fid; + + // If there's a new default image get save the width and height in the + // field/instance settings. + $original_fid = isset($entity->original) ? $entity->original->settings['default_image']['fid'] : 0; + 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(); + } } else { $entity->settings['default_image']['fid'] = 0; + $entity->settings['default_image']['width'] = 0; + $entity->settings['default_image']['height'] = 0; } } } diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php b/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php index d67644c..6d7f9e6 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/field_type/ImageItem.php @@ -22,7 +22,9 @@ * "default_image" = { * "fid" = 0, * "alt" = "", - * "title" = "" + * "title" = "", + * "width" = 0, + * "height" = 0 * }, * "column_groups" = { * "file" = { @@ -52,7 +54,9 @@ * "default_image" = { * "fid" = 0, * "alt" = "", - * "title" = "" + * "title" = "", + * "width" = 0, + * "height" = 0 * } * }, * default_widget = "image_image", @@ -355,6 +359,16 @@ protected function defaultImageForm(array &$element, array $settings) { '#default_value' => $settings['default_image']['title'], '#maxlength' => 1024, ); + + $element['default_image']['width'] = array( + '#type' => 'value', + '#value' => $settings['default_image']['width'], + ); + + $element['default_image']['height'] = array( + '#type' => 'value', + '#value' => $settings['default_image']['height'], + ); } /** diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php index 9481e7e..dd40e12 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatterBase.php @@ -87,8 +87,8 @@ public function prepareView(array $entities_items) { 'is_default' => TRUE, 'alt' => $default_image['alt'], 'title' => $default_image['title'], - 'width' => $image->getWidth(), - 'height' => $image->getHeight(), + 'width' => $default_image['width'], + 'height' => $default_image['height'], 'entity' => $file, 'target_id' => $file->id(), ))); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php index f39076e..a616a4d 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldDefaultImagesTest.php @@ -46,11 +46,15 @@ public function testDefaultImages() { 'fid' => $default_images['field']->id(), 'alt' => '', 'title' => '', + 'width' => 0, + 'height' => 0, ); $instance_settings['default_image'] = array( 'fid' => $default_images['instance']->id(), 'alt' => '', 'title' => '', + 'width' => 0, + 'height' => 0, ); $widget_settings = array( 'preview_image_style' => 'medium', @@ -87,6 +91,8 @@ public function testDefaultImages() { 'fid' => $default_images['instance2']->id(), 'alt' => '', 'title' => '', + 'width' => 0, + 'height' => 0, ), ), )); diff --git a/core/profiles/standard/config/field.field.node.field_image.yml b/core/profiles/standard/config/field.field.node.field_image.yml index 7613e52..6277cba 100644 --- a/core/profiles/standard/config/field.field.node.field_image.yml +++ b/core/profiles/standard/config/field.field.node.field_image.yml @@ -11,6 +11,8 @@ settings: fid: 0 alt: '' title: '' + width: 0 + height: 0 column_groups: file: label: File diff --git a/core/profiles/standard/config/field.instance.node.article.field_image.yml b/core/profiles/standard/config/field.instance.node.article.field_image.yml index 0ecd418..488cabe 100644 --- a/core/profiles/standard/config/field.instance.node.article.field_image.yml +++ b/core/profiles/standard/config/field.instance.node.article.field_image.yml @@ -23,5 +23,7 @@ settings: fid: 0 alt: '' title: '' + width: 0 + height: 0 status: 1 langcode: und