public function BlazyVideoFormatter::viewElements in Blazy 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/BlazyVideoFormatter.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyVideoFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ BlazyVideoFormatter.php, line 48
Class
- BlazyVideoFormatter
- Plugin implementation of the 'Blazy Video' to get VEF videos.
Namespace
Drupal\blazy\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$build = [];
// Early opt-out if the field is empty.
if ($items
->isEmpty()) {
return $build;
}
// Collects specific settings to this formatter.
$settings = $this
->buildSettings();
$settings['blazy'] = TRUE;
$settings['namespace'] = $settings['item_id'] = $settings['lazy'] = 'blazy';
// Build the settings.
$build = [
'settings' => $settings,
];
// Modifies settings.
$this->blazyManager
->buildSettings($build, $items);
// Fecthes URI from the first item to build dimensions once.
$this
->buildVideo($build['settings'], $items[0]->value);
// Build the elements.
$this
->buildElements($build, $items);
// Updates settings.
$settings = $build['settings'];
unset($build['settings']);
// Supports Blazy multi-breakpoint images if provided.
if (!empty($settings['uri'])) {
$this->blazyManager
->isBlazy($settings, $build[0]['#build']);
}
$build['#blazy'] = $settings;
$build['#attached'] = $this->blazyManager
->attach($settings);
return $build;
}