Ejemplo n.º 1
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['link_url'] = $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);
 }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
0
 /**
  * Utility to extract Juicebox options from the common Drupal display
  * settings, and add them to the gallery.
  *
  * Some common Juicebox configuration options are set via a GUI and others
  * are set as manual strings. This method fetches all of these values from
  * drupal settings data and merges them into the gallery. Note that this only
  * accounts for common settings.
  *
  * @param Drupal\juicebox\JuiceboxGalleryInterface $gallery
  *   An initialized Juicebox gallery object.
  * @param $settings
  *   An associative array of gallery-specific settings.
  * @param mixed $data
  */
 protected function setGalleryOptions(JuiceboxGalleryInterface $gallery, $settings)
 {
     // Get the string options set via the GUI.
     foreach (array('jlib_galleryWidth', 'jlib_galleryHeight', 'jlib_backgroundColor', 'jlib_textColor', 'jlib_thumbFrameColor') as $name) {
         if (isset($settings[$name])) {
             $name_real = str_replace('jlib_', '', $name);
             $gallery->addOption(Unicode::strtolower($name_real), trim(Html::escape($settings[$name])));
         }
     }
     // Get the bool options set via the GUI.
     foreach (array('jlib_showOpenButton', 'jlib_showExpandButton', 'jlib_showThumbsButton', 'jlib_useThumbDots', 'jlib_useFullscreenExpand') as $name) {
         if (isset($settings[$name])) {
             $name_real = str_replace('jlib_', '', $name);
             $gallery->addOption(Unicode::strtolower($name_real), !empty($settings[$name]) ? 'TRUE' : 'FALSE');
         }
     }
     // Merge-in the manually assigned options making sure they take priority
     // over any conflicting GUI options.
     if (!empty($settings['manual_config'])) {
         $manual_options = explode("\n", $settings['manual_config']);
         foreach ($manual_options as $option) {
             $option = trim($option);
             if (!empty($option)) {
                 // Each manual option has only been validated (on input) to be in the
                 // form optionName="optionValue". Now we need split and sanitize the
                 // values.
                 $matches = array();
                 preg_match('/^([A-Za-z0-9]+?)="([^"]+?)"$/u', $option, $matches);
                 list($full_match, $name, $value) = $matches;
                 $gallery->addOption(Unicode::strtolower($name), Html::escape($value));
             }
         }
     }
 }