You are here

function juicebox_build_gallery_data_from_view in Juicebox HTML5 Responsive Image Galleries 7

Generate data for a Juicebox gallery from a view object.

Parameters

object $view: A fully built/executed view object.

string $xml_path: The path to this gallery's XML (can be used as a unique gallery ID).

Return value

array An associative array containing all the content and configuration data needed to build a Juicebox gallery and XML.

2 calls to juicebox_build_gallery_data_from_view()
juicebox_page_xml in ./juicebox.module
Menu callback: generate Juicebox XML.
juicebox_style_plugin::render in plugins/juicebox_style_plugin.inc
Render the view page display.

File

./juicebox.module, line 207
Provides Drupal integration with the Juicebox library.

Code

function juicebox_build_gallery_data_from_view($view, $xml_path) {
  $images = array();
  $settings = $view->style_plugin->options;

  // Get rendered field data that's not available in $view->result
  $rendered_fields = $view->style_plugin
    ->render_fields($view->result);
  foreach ($view->result as $row_index => $row) {

    // First make sure that we have image field data to work with. This prevents
    // php errors from coming up if the user has not yet configured their
    // view page display or if the view lists items that don't contain an image.
    $field_image_name = 'field_' . $settings['image_field'];
    $field_thumb_name = 'field_' . $settings['thumb_field'];
    if (!empty($row->{$field_image_name}[0]['raw']['uri']) && !empty($row->{$field_thumb_name}[0]['raw']['uri'])) {
      $field_image_uri = $row->{$field_image_name}[0]['raw']['uri'];
      $unstyled_image_src = file_create_url($field_image_uri);

      // Get the main image source.
      $image_src = $unstyled_image_src;
      if (!empty($settings['image_field_style'])) {
        $image_src = image_style_url($settings['image_field_style'], $field_image_uri);
      }

      // Get the thumbnail source.
      $thumb_src = $unstyled_image_src;
      $field_thumb_uri = $row->{$field_thumb_name}[0]['raw']['uri'];
      if (!empty($settings['thumb_field_style'])) {
        $thumb_src = image_style_url($settings['thumb_field_style'], $field_thumb_uri);
      }

      // Get the image title.
      $title = '';
      if (!empty($settings['title_field']) && !empty($rendered_fields[$row_index][$settings['title_field']])) {
        $title = $rendered_fields[$row_index][$settings['title_field']];
      }

      // Get the image caption.
      $caption = '';
      if (!empty($settings['caption_field']) && !empty($rendered_fields[$row_index][$settings['caption_field']])) {
        $caption = $rendered_fields[$row_index][$settings['caption_field']];
      }

      // Filter the image title and caption markup.
      if (!empty($settings['apply_markup_filter'])) {
        $title = _juicebox_filter_markup($title);
        $caption = _juicebox_filter_markup($caption);
      }

      // Get the linkURL.
      $image_link_src = $unstyled_image_src;
      if (!empty($settings['linkurl_source'])) {
        if ($settings['linkurl_source'] == 'image_styled') {
          $image_link_src = $image_src;
        }
        elseif (!empty($rendered_fields[$row_index][$settings['linkurl_source']])) {
          $image_link_src = $rendered_fields[$row_index][$settings['linkurl_source']];
        }
      }

      // Add each image to the data.
      $images[$row_index]['image_src'] = $image_src;
      $images[$row_index]['image_link_src'] = $image_link_src;
      $images[$row_index]['thumb_src'] = $thumb_src;
      $images[$row_index]['title'] = $title;
      $images[$row_index]['caption'] = $caption;
      $images[$row_index]['linkurl_target'] = $settings['linkurl_target'];
    }
  }

  // Get the Juicebox library-specific options.
  $jlib_options = array();
  if ($settings['show_title']) {
    $jlib_options['gallerytitle'] = check_plain($view
      ->get_title());
  }
  $jlib_options = array_merge($jlib_options, _juicebox_get_lib_options($settings));
  $data = array(
    'jlib_options' => $jlib_options,
    'images' => $images,
  );

  // Allow other modules to alter the data we are using to build the gallery.
  $source_info = array(
    'xml_path' => $xml_path,
    'source' => $view,
  );
  drupal_alter('juicebox_gallery_data', $data, $settings, $source_info);

  // Make sure all Juicebox library configuration keys are lowercase to make
  // future key lookups easier.
  $data['jlib_options'] = array_change_key_case($data['jlib_options']);
  return $data;
}