You are here

protected function JuiceboxFormatterViewsStyle::getItems in Juicebox HTML5 Responsive Image Galleries 7.2

Utility to get the item arrays that contain image data from view rows.

The items that this method returns are array representations of Drupal file objects. This type of array is often used by Drupal to represent file fields, so this a common format to work with.

Parameters

string $source_field: The view field source that will contain a file identifer. The exact part of the row data to get the file identifer from will depend on the field type, and this method will resolve that based on the view's field handler details.

Return value

array An indexed array, keyed by row id, of file field items that were extracted based on row data.

See also

JuiceboxFormatterViewStyle::confGetFieldSources()

1 call to JuiceboxFormatterViewsStyle::getItems()
JuiceboxFormatterViewsStyle::buildGallery in plugins/JuiceboxFormatterViewsStyle.inc
Build the gallery based on loaded Drupal views data.

File

plugins/JuiceboxFormatterViewsStyle.inc, line 277
Contains the Juicebox views style plugin.

Class

JuiceboxFormatterViewsStyle
Style plugin to render each item in a views list.

Code

protected function getItems($source_field) {
  $view = $this->view;

  // Get the field source options and make sure the passed-source is valid.
  $source_options = $this
    ->confGetFieldSources();
  if (empty($source_options['field_options_images_type'][$source_field])) {
    throw new Exception(t('Empty or invalid field source @source detected for Juicebox view-based gallery.', array(
      '@source' => $source_field,
    )));
  }
  else {
    $source_type = $source_options['field_options_images_type'][$source_field];
  }
  $fids = array();
  $items = array();

  // Pass 1 - get the fids based on the source type.
  foreach ($view->result as $row_index => $row) {
    switch ($source_type) {
      case 'file_base':

        // This is a file-based view so the fid is already in the row data.
        if (!empty($row->fid)) {
          $fids[$row_index] = $row->fid;
        }
        continue;
      case 'file_id_field':

        // The source is a file ID field so we can fetch the fid from row
        // data.
        if (!empty($row->{$view->field[$source_field]->field_alias})) {
          $fids[$row_index] = $row->{$view->field[$source_field]->field_alias};
        }
        continue;
      case 'file_field':

        // The source is an image or file field so views should have already
        // loaded the related file entities which will include the fid. In
        // this case we also already have our file field item array.
        if (!empty($row->{'field_' . $source_field}[0]['raw'])) {
          $fids[$row_index] = $row->{'field_' . $source_field}[0]['raw']['fid'];
          $items[$row_index] = $row->{'field_' . $source_field}[0]['raw'];
        }
    }
  }
  if (empty($items)) {

    // Bulk load all file entities.
    $file_entities = file_load_multiple($fids);

    // Pass 2 - Get field items from the file entities.
    foreach ($fids as $row_index => $fid) {
      $items[$row_index] = (array) $file_entities[$fid];
    }
  }
  return $items;
}