Loading core/modules/field/tests/src/Kernel/Migrate/d6/MigrateFieldFormatterSettingsTest.php +7 −1 Original line number Diff line number Diff line Loading @@ -147,7 +147,13 @@ public function testEntityDisplaySettings() { // Test the image field formatter settings. $expected['weight'] = 9; $expected['type'] = 'image'; $expected['settings'] = ['image_link' => '', 'image_style' => '']; $expected['settings'] = [ 'image_link' => '', 'image_style' => '', 'image_loading' => [ 'attribute' => 'lazy', ], ]; $component = $display->getComponent('field_test_imagefield'); $this->assertSame($expected, $component); $display = EntityViewDisplay::load('node.story.teaser'); Loading core/modules/image/config/schema/image.schema.yml +7 −0 Original line number Diff line number Diff line Loading @@ -142,6 +142,13 @@ field.formatter.settings.image: image_style: type: string label: 'Image style' image_loading: type: mapping label: 'Image loading settings' mapping: attribute: type: string label: 'Loading attribute' field.formatter.settings.image_url: type: mapping Loading core/modules/image/image.module +11 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ * Exposes global functionality for creating image styles. */ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Routing\RouteMatchInterface; Loading @@ -14,6 +15,7 @@ use Drupal\field\FieldStorageConfigInterface; use Drupal\file\FileInterface; use Drupal\image\Entity\ImageStyle; use Drupal\image\ImageConfigUpdater; /** * The name of the query parameter for image derivative tokens. Loading Loading @@ -368,6 +370,15 @@ function image_entity_presave(EntityInterface $entity) { $entity->setSetting('default_image', $default_image); } /** * Implements hook_ENTITY_TYPE_presave() for entity_view_display. */ function image_entity_view_display_presave(EntityViewDisplayInterface $view_display): void { $config_updater = \Drupal::classResolver(ImageConfigUpdater::class); assert($config_updater instanceof ImageConfigUpdater); $config_updater->processImageLazyLoad($view_display); } /** * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'. */ Loading core/modules/image/image.post_update.php +14 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,10 @@ * Post-update functions for Image. */ use Drupal\Core\Config\Entity\ConfigEntityUpdater; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\image\ImageConfigUpdater; /** * Implements hook_removed_post_updates(). */ Loading @@ -14,3 +18,13 @@ function image_removed_post_updates() { 'image_post_update_scale_and_crop_effect_add_anchor' => '9.0.0', ]; } /** * Add the image loading attribute setting to image field formatter instances. */ function image_post_update_image_loading_attribute(array &$sandbox = NULL): void { $image_config_updater = \Drupal::classResolver(ImageConfigUpdater::class); \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function (EntityViewDisplayInterface $view_display) use ($image_config_updater): bool { return $image_config_updater->processImageLazyLoad($view_display); }); } core/modules/image/src/ImageConfigUpdater.php 0 → 100644 +43 −0 Original line number Diff line number Diff line <?php namespace Drupal\image; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; /** * Provides a BC layer for modules providing old configurations. * * @internal * This class is only meant to fix outdated image configuration and its * methods should not be invoked directly. It will be removed once all the * deprecated methods have been removed. */ final class ImageConfigUpdater { /** * Re-order mappings by breakpoint ID and descending numeric multiplier order. * * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display * The view display. * * @return bool * Whether the display was updated. */ public function processImageLazyLoad(EntityViewDisplayInterface $view_display): bool { $changed = FALSE; foreach ($view_display->getComponents() as $field => $component) { if (isset($component['type']) && ($component['type'] === 'image') && !array_key_exists('image_loading', $component['settings']) ) { $component['settings']['image_loading']['attribute'] = 'lazy'; $view_display->setComponent($field, $component); $changed = TRUE; } } return $changed; } } Loading
core/modules/field/tests/src/Kernel/Migrate/d6/MigrateFieldFormatterSettingsTest.php +7 −1 Original line number Diff line number Diff line Loading @@ -147,7 +147,13 @@ public function testEntityDisplaySettings() { // Test the image field formatter settings. $expected['weight'] = 9; $expected['type'] = 'image'; $expected['settings'] = ['image_link' => '', 'image_style' => '']; $expected['settings'] = [ 'image_link' => '', 'image_style' => '', 'image_loading' => [ 'attribute' => 'lazy', ], ]; $component = $display->getComponent('field_test_imagefield'); $this->assertSame($expected, $component); $display = EntityViewDisplay::load('node.story.teaser'); Loading
core/modules/image/config/schema/image.schema.yml +7 −0 Original line number Diff line number Diff line Loading @@ -142,6 +142,13 @@ field.formatter.settings.image: image_style: type: string label: 'Image style' image_loading: type: mapping label: 'Image loading settings' mapping: attribute: type: string label: 'Loading attribute' field.formatter.settings.image_url: type: mapping Loading
core/modules/image/image.module +11 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ * Exposes global functionality for creating image styles. */ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Routing\RouteMatchInterface; Loading @@ -14,6 +15,7 @@ use Drupal\field\FieldStorageConfigInterface; use Drupal\file\FileInterface; use Drupal\image\Entity\ImageStyle; use Drupal\image\ImageConfigUpdater; /** * The name of the query parameter for image derivative tokens. Loading Loading @@ -368,6 +370,15 @@ function image_entity_presave(EntityInterface $entity) { $entity->setSetting('default_image', $default_image); } /** * Implements hook_ENTITY_TYPE_presave() for entity_view_display. */ function image_entity_view_display_presave(EntityViewDisplayInterface $view_display): void { $config_updater = \Drupal::classResolver(ImageConfigUpdater::class); assert($config_updater instanceof ImageConfigUpdater); $config_updater->processImageLazyLoad($view_display); } /** * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'. */ Loading
core/modules/image/image.post_update.php +14 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,10 @@ * Post-update functions for Image. */ use Drupal\Core\Config\Entity\ConfigEntityUpdater; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\image\ImageConfigUpdater; /** * Implements hook_removed_post_updates(). */ Loading @@ -14,3 +18,13 @@ function image_removed_post_updates() { 'image_post_update_scale_and_crop_effect_add_anchor' => '9.0.0', ]; } /** * Add the image loading attribute setting to image field formatter instances. */ function image_post_update_image_loading_attribute(array &$sandbox = NULL): void { $image_config_updater = \Drupal::classResolver(ImageConfigUpdater::class); \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display', function (EntityViewDisplayInterface $view_display) use ($image_config_updater): bool { return $image_config_updater->processImageLazyLoad($view_display); }); }
core/modules/image/src/ImageConfigUpdater.php 0 → 100644 +43 −0 Original line number Diff line number Diff line <?php namespace Drupal\image; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; /** * Provides a BC layer for modules providing old configurations. * * @internal * This class is only meant to fix outdated image configuration and its * methods should not be invoked directly. It will be removed once all the * deprecated methods have been removed. */ final class ImageConfigUpdater { /** * Re-order mappings by breakpoint ID and descending numeric multiplier order. * * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display * The view display. * * @return bool * Whether the display was updated. */ public function processImageLazyLoad(EntityViewDisplayInterface $view_display): bool { $changed = FALSE; foreach ($view_display->getComponents() as $field => $component) { if (isset($component['type']) && ($component['type'] === 'image') && !array_key_exists('image_loading', $component['settings']) ) { $component['settings']['image_loading']['attribute'] = 'lazy'; $view_display->setComponent($field, $component); $changed = TRUE; } } return $changed; } }