BlazyFormatterBlazy.php in Blazy 8.2
File
src/Plugin/Field/FieldFormatter/BlazyFormatterBlazy.php
View source
<?php
namespace Drupal\blazy\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Component\Utility\Xss;
class BlazyFormatterBlazy extends BlazyFileFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$files = $this
->getEntitiesToView($items, $langcode);
if (empty($files)) {
return [];
}
return $this
->commonViewElements($items, $langcode, $files);
}
public function buildElements(array &$build, $files) {
$settings = $build['settings'];
foreach ($files as $delta => $file) {
$item = $file->_referringItem;
$box = [];
$settings['delta'] = $delta;
$settings['file_tags'] = $file
->getCacheTags();
$settings['type'] = 'image';
$settings['uri'] = $file
->getFileUri();
$box['item'] = $item;
$box['settings'] = $settings;
$this
->buildElement($box, $file);
if (!empty($settings['caption'])) {
foreach ($settings['caption'] as $caption) {
if ($caption_content = $box['item']->{$caption}) {
$box['captions'][$caption] = [
'#markup' => Xss::filterAdmin($caption_content),
];
}
}
}
$build[$delta] = $this->formatter
->getBlazy($box);
unset($box);
}
}
}