diff --git a/core/modules/field/field.install b/core/modules/field/field.install index f7949d4..884e457 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -579,34 +579,7 @@ function field_update_8006(&$sandbox) { $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['index'] / $sandbox['max']); } -/** - * 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' => (int) $fid, - 'alt' => '', - 'title' => '', - 'width' => $fid ? $image->getWidth() : 0, - 'height' => $fid ? $image->getHeight() : 0, - ); - $config - ->set('settings.default_image', $default_image) - ->save(); - } - } - } -} + /** * @} End of "addtogroup updates-7.x-to-8.x". diff --git a/core/modules/image/image.install b/core/modules/image/image.install index 9a14749..2cf4b2c 100644 --- a/core/modules/image/image.install +++ b/core/modules/image/image.install @@ -59,6 +59,17 @@ function image_requirements($phase) { } /** + * Implements hook_update_dependencies(). + */ +function image_update_dependencies() { + // Convert image field and instance setting 'default_image' from integer to + // array only after fields and instances were converted to config. + $dependencies['image'][8003] = array('field' => 8006); + + return $dependencies; +} + +/** * Loads all effects for an image style from backend. * * @param array $style @@ -237,3 +248,35 @@ function image_update_8002() { 'image_style_preview_image' => 'preview_image', )); } + +/** + * Convert image field and instance setting 'default_image' from integer to + * array by adding alt, title, width, and height options. + */ +function image_update_8003() { + $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); + $is_image = ($type == 'field' && $config->get('type') == 'image') || ($type == 'instance' && $config->get('field_type') == 'image'); + if (!$is_image) { + continue; + } + if ($fid = (int) $config->get('settings.default_image')) { + $uri = db_query('SELECT fid FROM {file_managed} WHERE fid = :fid', array(':fid' => $fid))->fetchField(); + $image = $image_factory->get($fid); + } + $default_image = array( + 'fid' => $fid ?: NULL, + 'alt' => '', + 'title' => '', + 'width' => $fid ? $image->getWidth() : NULL, + 'height' => $fid ? $image->getHeight() : NULL, + ); + $config + ->set('settings.default_image', $default_image) + ->save(); + } + } +} diff --git a/core/modules/image/image.module b/core/modules/image/image.module index d465aba..9a33112 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -417,26 +417,31 @@ function image_entity_presave(EntityInterface $entity, $type) { elseif ($entity instanceof Field) { $field = $entity; } - if ($field && $field->type == 'image' && is_array($entity->settings['default_image']['fid'])) { - if (!empty($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; + // Exit, if not saving an image field or image field instance entity. + if (!$field || $field->type != 'image') { + return; + } + + if (!empty($entity->settings['default_image']['fid'][0])) { + $entity->settings['default_image']['fid'] = $entity->settings['default_image']['fid'][0]; + } + if ($fid = $entity->settings['default_image']['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(); } } + else { + $entity->settings['default_image'] = array( + 'fid' => NULL, + 'alt' => '', + 'title' => '', + 'width' => NULL, + 'height' => NULL, + ); + } } /** 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 6d7f9e6..f8f4b7c 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 @@ -20,11 +20,11 @@ * settings = { * "uri_scheme" = "", * "default_image" = { - * "fid" = 0, + * "fid" = NULL, * "alt" = "", * "title" = "", - * "width" = 0, - * "height" = 0 + * "width" = NULL, + * "height" = NULL * }, * "column_groups" = { * "file" = { @@ -52,11 +52,11 @@ * "max_resolution" = "", * "min_resolution" = "", * "default_image" = { - * "fid" = 0, + * "fid" = NULL, * "alt" = "", * "title" = "", - * "width" = 0, - * "height" = 0 + * "width" = NULL, + * "height" = NULL * } * }, * default_widget = "image_image", @@ -322,8 +322,6 @@ public static function validateResolution($element, &$form_state) { /** * Builds the default_image details element. * - * Code reusing in both, settingsForm() and instanceSettingsForm() methods. - * * @param array $element * The form associative array passed by reference. * @param array $settings @@ -335,7 +333,6 @@ protected function defaultImageForm(array &$element, array $settings) { '#title' => t('Default image'), '#open' => TRUE, ); - $element['default_image']['fid'] = array( '#type' => 'managed_file', '#title' => t('Image'), @@ -343,28 +340,24 @@ protected function defaultImageForm(array &$element, array $settings) { '#default_value' => empty($settings['default_image']['fid']) ? array() : array($settings['default_image']['fid']), '#upload_location' => $settings['uri_scheme'] . '://default_images/', ); - $element['default_image']['alt'] = array( '#type' => 'textfield', '#title' => t('Alternate text'), - '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), + '#description' => t('This text will be used by screen readers, search engines, and when the image cannot be loaded.'), '#default_value' => $settings['default_image']['alt'], '#maxlength' => 512, ); - $element['default_image']['title'] = array( '#type' => 'textfield', '#title' => t('Title'), - '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'), + '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'), '#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/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php index 9bc0235..7d742a7 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php @@ -145,4 +145,41 @@ public function sortByKey(array $data) { } return $data; } + + /** + * Tests if the field and instance setting 'default_image' has been + * successfully converted from an integer to an associative array. + */ + public function testImageFieldDefaultImageUpgrade() { + // Perform upgrade. + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + // While this test is testing the upgrade path against a Drupal 7 'standard' + // profile installation we assume that node bundle 'article', having the + // 'field_image' image field, is installed too. + $cases = array('field.field.node.field_image', 'field.instance.node.article.field_image'); + foreach ($cases as $case) { + $default_image = \Drupal::config($case)->get('settings.default_image'); + + // Check is was converted to an array. + $this->assertTrue(is_array($default_image)); + + // Check if 'default_image' contains a key named 'fid'. It might be an + // integer or NULL. + $this->assertTrue(array_key_exists('fid', $default_image)); + + // Check if 'alt' key exists and is a string. + $this->assertTrue(isset($default_image['alt']) && is_string($default_image['alt'])); + + // Check if 'title' key exists and is a string. + $this->assertTrue(isset($default_image['title']) && is_string($default_image['title'])); + + // Check if 'width' key exists. It might be an integer or NULL. + $this->assertTrue(array_key_exists('width', $default_image)); + + // Check if 'height' key exists. It might be an integer or NULL. + $this->assertTrue(array_key_exists('height', $default_image)); + } + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php index ecd82e8..85822dc 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php @@ -44,7 +44,7 @@ public function testUserPictureUpgrade() { $instance = field_info_instance('user', 'user_picture', 'user'); // We explicitly avoid using the getFieldSetting() method here, since it // merges field and instance settings. - $file = entity_load('file', $instance->settings['default_image']['fid'][0]); + $file = entity_load('file', $instance->settings['default_image']['fid']); $this->assertTrue($file, 'Default user picture has been migrated.'); $this->assertEqual($file->getFileUri(), 'public://user_pictures_dir/druplicon.png', 'File id matches the uri expected.'); $this->assertEqual($file->getFilename(), 'druplicon.png'); diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 7620ee5..ef916d4 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -272,7 +272,13 @@ function user_install_picture_field() { 'indexes' => array('target_id' => array('target_id')), 'settings' => array( 'uri_scheme' => 'public', - 'default_image' => FALSE, + 'default_image' => array( + 'fid' => NULL, + 'alt' => '', + 'title' => '', + 'width' => NULL, + 'height' => NULL, + ), ), ); entity_create('field_entity', $field)->save(); @@ -292,7 +298,13 @@ function user_install_picture_field() { 'title_field' => 0, 'max_resolution' => '85x85', 'min_resolution' => '', - 'default_image' => 0, + 'default_image' => array( + 'fid' => NULL, + 'alt' => '', + 'title' => '', + 'width' => NULL, + 'height' => NULL, + ), ), ); entity_create('field_instance', $instance)->save(); @@ -660,7 +672,13 @@ function user_update_8011() { 'indexes' => array('target_id' => array('target_id')), 'settings' => array( 'uri_scheme' => 'public', - 'default_image' => FALSE, + 'default_image' => array( + 'fid' => NULL, + 'alt' => '', + 'title' => '', + 'width' => NULL, + 'height' => NULL, + ), ), 'schema' => array( 'columns' => array( @@ -706,6 +724,13 @@ function user_update_8011() { ); _update_8003_field_create_field($field); + // Get the default image object, if exists. We want to obtain later the image + // dimensions. + $has_default_image = !empty($default_image_fid); + if ($has_default_image) { + $image = \Drupal::service('image.factory')->get($destination); + } + $instance = array( 'entity_type' => 'user', 'bundle' => 'user', @@ -720,7 +745,13 @@ function user_update_8011() { 'title_field' => 0, 'max_resolution' => update_variable_get('user_picture_dimensions', '85x85'), 'min_resolution' => '', - 'default_image' => !empty($default_image_fid) ? array($default_image_fid) : array(), + 'default_image' => array( + 'fid' => $has_default_image ? $default_image_fid : NULL, + 'alt' => '', + 'title' => '', + 'width' => $has_default_image ? $image->getWidth() : NULL, + 'height' => $has_default_image ? $image->getHeight() : NULL, + ), ), ); _update_8003_field_create_instance($field, $instance); @@ -778,7 +809,7 @@ function user_update_8011() { $display->save(); // Add file usage for the default field. - if (!empty($default_image_fid)) { + if ($has_default_image) { db_insert('file_usage') ->fields(array( 'fid' => $default_image_fid, 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 6277cba..458baaf 100644 --- a/core/profiles/standard/config/field.field.node.field_image.yml +++ b/core/profiles/standard/config/field.field.node.field_image.yml @@ -8,11 +8,11 @@ active: '1' settings: uri_scheme: public default_image: - fid: 0 + fid: null alt: '' title: '' - width: 0 - height: 0 + width: null + height: null 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 488cabe..23db673 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 @@ -20,10 +20,10 @@ settings: alt_field_required: 0 title_field_required: 0 default_image: - fid: 0 + fid: null alt: '' title: '' - width: 0 - height: 0 + width: null + height: null status: 1 langcode: und