예제 #1
0
 /**
  * Utility to build a Juicebox gallery based on field formatter data.
  *
  * @param Drupal\juicebox\JuiceboxGalleryInterface $gallery
  *   An initialized Juicebox gallery object.
  * @param Drupal\Core\Field\FieldItemListInterface $items
  *   A list of field items that contain file data for the gallery.
  */
 protected function buildGallery(JuiceboxGalleryInterface $gallery, FieldItemListInterface $items)
 {
     // Get settings.
     $settings = $this->getSettings();
     // Iterate over items and extract image data.
     foreach ($items as $delta => $item) {
         if ($item->isDisplayed() && !empty($item->target_id)) {
             // Calculate the source data that Juicebox requires.
             $src_data = $this->juicebox->styleImageSrcData($item->entity, $settings['image_style'], $item->entity, $settings['thumb_style'], $settings);
             // Short-circut this iteration if skipping an incompatible file.
             if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'skip') {
                 continue;
             }
             // Set the image title. If we have an incompatible file and are
             // configured to show a link, set the title text as the link.
             if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'show_icon_and_link') {
                 $anchor = !empty($item->description) ? $item->description : $item->entity->get('filename')->value;
                 $title = $this->linkGenerator->generate($anchor, Url::fromUri($src_data['linkURL']));
             } else {
                 $title = $this->getFieldText($item, $settings['title_source']);
             }
             // Set the image caption.
             $caption = $this->getFieldText($item, $settings['caption_source']);
             // Add this image to the gallery.
             $gallery->addImage($src_data, $title, $caption);
         }
     }
     // Run common build tasks. This is also where the general settings are
     // applied.
     $this->juicebox->runCommonBuild($gallery, $settings, $items);
 }
예제 #2
0
 /**
  * Build the gallery based on loaded Drupal views data.
  *
  * @param Drupal\juicebox\JuiceboxGalleryInterface $gallery
  *   An initialized Juicebox gallery object.
  */
 protected function buildGallery(JuiceboxGalleryInterface $gallery)
 {
     $view = $this->view;
     $settings = $this->options;
     // Populate $this->rendered_fields
     $this->renderFields($view->result);
     // Get all row image data in the format of Drupal file field items.
     $image_items = $thumb_items = $this->getItems($settings['image_field']);
     if ($settings['image_field'] != $settings['thumb_field']) {
         $thumb_items = $this->getItems($settings['thumb_field']);
     }
     // Iterate through each view row and calculate the gallery-specific details.
     foreach ($image_items as $row_index => $image_item) {
         // Make sure each main image has a thumb item.
         $thumb_item = !empty($thumb_items[$row_index]) ? $thumb_items[$row_index] : $image_item;
         // Calculate the source data that Juicebox requires.
         $src_data = $this->juicebox->styleImageSrcData($image_item, $settings['image_field_style'], $thumb_item, $settings['thumb_field_style'], $settings);
         // Short-circut this iteration if skipping an incompatible file.
         if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'skip') {
             continue;
         }
         // Check if the linkURL should be customized based on view settings.
         if (!empty($settings['linkurl_source']) && !empty($this->rendered_fields[$row_index][$settings['linkurl_source']])) {
             $src_data['linkURL'] = (string) $this->rendered_fields[$row_index][$settings['linkurl_source']];
         }
         // Set the image title.
         $title = '';
         // If we have an incompatible file the title may need special handeling.
         if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'show_icon_and_link') {
             $anchor = !empty($image_item->description) ? $image_item->description : $image_item->filename;
             $title = $this->linkGenerator->generate($anchor, Url::fromUri($src_data['linkURL']));
         } elseif (!empty($settings['title_field']) && !empty($this->rendered_fields[$row_index][$settings['title_field']])) {
             $title = (string) $this->rendered_fields[$row_index][$settings['title_field']];
         }
         // Set the image caption.
         $caption = '';
         if (!empty($settings['caption_field']) && !empty($this->rendered_fields[$row_index][$settings['caption_field']])) {
             $caption = (string) $this->rendered_fields[$row_index][$settings['caption_field']];
         }
         // Add this image to the gallery.
         $gallery->addImage($src_data, $title, $caption);
     }
     if ($settings['show_title']) {
         $gallery->addOption('gallerytitle', Html::escape($view->getTitle()));
     }
     // Run common build tasks.
     $this->juicebox->runCommonBuild($gallery, $settings, $view);
 }