You are here

function tft_preprocess_field in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x tft.module \tft_preprocess_field()

Implements hook_theme_preprocess_field().

File

./tft.module, line 689
Contains tft.module.

Code

function tft_preprocess_field(&$variables) {
  if ('tft_file' == $variables['field_name']) {
    $fids = $variables['element']['#object']
      ->get('tft_file')
      ->getValue();
    if (empty($fids)) {
      return;
    }
    $fid = reset($fids)['target_id'];
    $file = File::load($fid);
    if (strpos($file
      ->getMimeType(), 'image') !== FALSE) {
      $image_url = ImageStyle::load('large')
        ->buildUrl($file
        ->getFileUri());
    }
    if (!isset($image_url)) {
      return;
    }

    // Clean jquery ui attributes & add data src.
    foreach ($variables['items'] as &$items) {
      if (isset($items['content'])) {
        foreach ($items['content'] as &$item) {
          foreach ($item as &$value) {
            if (isset($value['#options'])) {
              $value['#options']['attributes'] = [];
              $value['#options']['attributes']['data-src'] = $image_url;
            }
          }
        }
      }
    }
  }
}