LazyImageFormatter.php in Lazy-load 8.3
File
src/Plugin/Field/FieldFormatter/LazyImageFormatter.php
View source
<?php
namespace Drupal\lazy\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
class LazyImageFormatter extends ImageFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
foreach ($elements as $delta => $element) {
$elements[$delta]['#item_attributes']['data-lazy'] = TRUE;
}
return $elements;
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$dependencies['module'][] = 'image';
$dependencies['config'][] = 'lazy.settings';
return $dependencies;
}
}