You are here

public static function BlazyAlter::thirdPartyPreprocessField in Blazy 8.2

Overrides variables for field.html.twig templates.

1 call to BlazyAlter::thirdPartyPreprocessField()
Blazy::preprocessField in src/Blazy.php
Overrides variables for field.html.twig templates.

File

src/BlazyAlter.php, line 144

Class

BlazyAlter
Provides hook_alter() methods for Blazy.

Namespace

Drupal\blazy

Code

public static function thirdPartyPreprocessField(array &$variables) {
  $element = $variables['element'];
  $settings = empty($element['#blazy']) ? [] : $element['#blazy'];
  $settings['third_party'] = $element['#third_party_settings'];
  $is_preview = Blazy::isPreview();
  foreach ($variables['items'] as &$item) {
    if (empty($item['content'])) {
      continue;
    }
    $item_attributes =& $item['content'][isset($item['content']['#attributes']) ? '#attributes' : '#item_attributes'];
    $item_attributes['data-b-lazy'] = TRUE;
    if ($is_preview) {
      $item_attributes['data-b-preview'] = TRUE;
    }
  }

  // Attaches Blazy libraries here since Blazy is not the formatter.
  $attachments = blazy()
    ->attach($settings);
  $variables['#attached'] = empty($variables['#attached']) ? $attachments : NestedArray::mergeDeep($variables['#attached'], $attachments);
}