Skip to content

Commit

Permalink
API Add image lazy load config
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jun 24, 2021
1 parent c0e90b8 commit 46e11eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Image.php
Expand Up @@ -25,6 +25,15 @@ class Image extends File
*/
private static $plural_name = "Images";

/**
* Globally control whether Images added via the WYSIWYG editor or inserted as Image objects in Silverstripe
* templates have the loading="lazy" HTML added by default
*
* @config
* @var bool
*/
private static $lazy_loading_enabled = true;

public function __construct($record = null, $isSingleton = false, $queryParams = [])
{
parent::__construct($record, $isSingleton, $queryParams);
Expand Down Expand Up @@ -60,4 +69,12 @@ public function PreviewLink($action = null)
$this->extend('updatePreviewLink', $link, $action);
return $link;
}

/**
* @return bool
*/
public static function getLazyLoadingEnabled(): bool
{
return self::config()->get('lazy_loading_enabled');
}
}
9 changes: 9 additions & 0 deletions tests/php/ImageTest.php
Expand Up @@ -547,4 +547,13 @@ protected function getMockAssetBackend($filename, $hash)
$builder->expects($this->any())->method('getIsImage')->will($this->returnValue(true));
return $builder;
}

public function testGetLazyLoadingEnabled()
{
$this->assertTrue(Image::getLazyLoadingEnabled());
Config::withConfig(function () {
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
$this->assertFalse(Image::getLazyLoadingEnabled());
});
}
}

0 comments on commit 46e11eb

Please sign in to comment.