You are here

function galleria_prepare_file_images in Galleria 7

Util function to parse file $items and return images in format expected by galleria formatter.

Parameters

array $items: Array of field items.

array $settings: Array of settings

string $langcode: Language code of the field

1 call to galleria_prepare_file_images()
galleria_field_formatter_view in ./galleria.module
Implements hook_field_formatter_view().

File

./galleria.module, line 628
A light-weight, customizable image gallery plugin for Drupal based on jQuery

Code

function galleria_prepare_file_images($items, $settings, $langcode) {
  $image_fids = array();
  $alt_field = empty($settings['alt_field']) ? FALSE : $settings['alt_field'];
  $title_field = empty($settings['title_field']) ? FALSE : $settings['title_field'];

  // First pass, fetch the fids. We pass twice so we can minimize calls to
  // entity_load which is expensive to call multiple times.
  foreach ($items as $delta => $item) {
    if ($item['type'] == 'image') {
      $image_fids[] = $item['fid'];

      // Set a sensible default.
      $items[$delta]['alt'] = $items[$delta]['title'] = '';
    }
    else {

      // Galleria only handles images.
      unset($items['key']);
    }
  }
  $file_entities = entity_load('file', $image_fids);

  // Second pass, set the alt/title tag.
  foreach ($items as $delta => $item) {
    if ($item['type'] == 'image') {
      $file_entity = $file_entities[$item['fid']];
      if ($alt_field) {
        $alt_field_items = field_get_items('file', $file_entity, $alt_field, $langcode);
        if (!empty($alt_field_items[0]['safe_value'])) {
          $items[$delta]['alt'] = $alt_field_items[0]['safe_value'];
        }
      }
      if ($title_field) {
        $title_field_items = field_get_items('file', $file_entity, $title_field, $langcode);
        if (!empty($title_field_items[0]['safe_value'])) {
          $items[$delta]['title'] = $title_field_items[0]['safe_value'];
        }
      }
    }
  }
  return $items;
}