You are here

function lazy_preprocess_field in Lazy-load 8.2

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

Implements template_preprocess_field().

File

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

Code

function lazy_preprocess_field(&$variables) {
  $element = $variables['element'];
  if ($element['#field_type'] === 'image') {
    $config = \Drupal::config('lazy.settings')
      ->get();
    $pages = $config['disabled_paths'];
    $path_matches = lazy_disabled_by_path($pages);
    if (!$path_matches) {
      $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);
    }
  }
}