You are here

function lazy_preprocess_field in Lazy-load 8

Same name and namespace in other branches
  1. 8.3 lazy.module \lazy_preprocess_field()
  2. 8.2 lazy.module \lazy_preprocess_field()

Implements template_preprocess_field().

File

./lazy.module, line 38
Module file for Lazy-load.

Code

function lazy_preprocess_field(&$variables) {
  $config = \Drupal::config('lazy.settings')
    ->get();
  $pages = $config['disabled_paths'];
  $path_matches = lazy_disabled_by_path($pages);
  $element = $variables['element'];
  if (!$path_matches && $element['#field_type'] === 'image') {
    $entity = $element['#object'];
    $entity_type = $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();
    $view_mode = $element['#view_mode'];
    $field_name = $element['#field_name'];
    $render_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    $field_display = $render_display
      ->getComponent($field_name);
    $image_fields = [];
    if (isset($field_display['third_party_settings']['lazy']) && $field_display['third_party_settings']['lazy']['lazy_image']) {
      foreach ($variables['items'] as $key => $item) {
        $variables['items'][$key]['content']['#item_attributes']['data-lazy-load-fields'] = 'image';
        $view_mode = $view_mode === 'full' ? 'default' : $view_mode;
        $image_fields["{$entity_type}--{$bundle}--{$view_mode}--{$field_name}"] = TRUE;
      }
    }
    lazy_settings_update($image_fields);
  }
}