You are here

protected function JuiceboxDisplayStyle::getItems in Juicebox HTML5 Responsive Image Galleries 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/style/JuiceboxDisplayStyle.php \Drupal\juicebox\Plugin\views\style\JuiceboxDisplayStyle::getItems()

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

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 entities that were extracted based on row data.

See also

JuiceboxDiplayStyle::confGetFieldSources()

1 call to JuiceboxDisplayStyle::getItems()
JuiceboxDisplayStyle::buildGallery in src/Plugin/views/style/JuiceboxDisplayStyle.php
Build the gallery based on loaded Drupal views data.

File

src/Plugin/views/style/JuiceboxDisplayStyle.php, line 363

Class

JuiceboxDisplayStyle
Plugin implementation of the 'juicebox' display style.

Namespace

Drupal\juicebox\Plugin\views\style

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('Empty or invalid field source @source detected for Juicebox view-based gallery.', [
      '@source' => $source_field,
    ]);
  }
  else {
    $source_type = $source_options['field_options_images_type'][$source_field];
  }
  $fids = [];
  $items = [];

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

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

        // The source is a file field so we fetch the fid through the
        // target_id property if the field item.
        $target_ids = $view->field[$source_field]
          ->getValue($row, 'target_id');

        // The target IDs value comes in a mixed format depending on
        // cardinality. We can only use one ID as each view row can only
        // reference one image (to ensure appropriate matching with the
        // thumb/title/caption data already specified on the row).
        $target_id = is_array($target_ids) ? reset($target_ids) : $target_ids;
        if (!empty($target_id) && is_numeric($target_id)) {
          $fids[$row_index] = $target_id;
        }
    }
  }
  if (empty($items)) {

    // Bulk load all file entities.
    $file_entities = $this->entityTypeManager
      ->getStorage('file')
      ->loadMultiple($fids);

    // Pass 2 - Ensure the file entities are keyed by row.
    foreach ($fids as $row_index => $fid) {
      $items[$row_index] = $file_entities[$fid];
    }
  }
  return $items;
}