You are here

function juicebox_field_formatter_view in Juicebox HTML5 Responsive Image Galleries 7

Same name and namespace in other branches
  1. 7.2 includes/juicebox.field.inc \juicebox_field_formatter_view()

Implements hook_field_formatter_view().

This is where the Juicebox embed code is built for the field formatter.

File

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

Code

function juicebox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // If there are no images, don't do anything else.
  if (empty($items)) {
    return;
  }

  // The gallery shown in preview view will only display field data from the
  // previously saved version (that is the only version the XML generation
  // methods will have access to). Display a warning because of this.
  if (!empty($entity->in_preview)) {
    drupal_set_message(t('Juicebox galleries may not display correctly in preview mode. Any edits made to gallery data will only be visible after all changes are saved.'), 'warning', FALSE);
  }
  $field_name = $instance['field_name'];
  $entity_type_info = entity_get_info($entity_type);
  $entity_id = $entity->{$entity_type_info['entity keys']['id']};

  // Initialize the "settings" values before working with them. This is
  // required for legacy support.
  $settings = _juicebox_init_display_settings($display['settings']);

  // Load the juicebox javascript.
  libraries_load('juicebox');

  // We need to get the display name to pass as part of our XML path. Though
  // we have access to the actaul $display array, it does not look like we
  // have access to the actaul display NAME in this scope. We do have access to
  // a list of ALL displays in $instanace though, so iterate though them to
  // find a match to the settings in $display. This seems SUPER CLUNKY, but
  // might be the only way.
  $display_name = 'default';
  foreach ($instance['display'] as $display_key => $display_data) {
    if ($display['settings'] == $display_data['settings']) {
      $display_name = $display_key;
    }
  }

  // Generate a unique ID that can be used to identify this gallery and field
  // source details.
  $xml_id = 'entity/' . $entity_type . '/' . $entity_id . '/' . $field_name . '/' . $display_name;
  $xml_path = 'juicebox/xml/' . $xml_id;

  // Calculate the data that will ultimately be used to render the gallery XML.
  // We won't officially generate the XML until the Juicebox javascript requests
  // it, but we will still need the related data within parts of the embed code.
  $gallery_data = juicebox_build_gallery_data_from_entity_field($items, $settings, $entity, $xml_path);

  // Get a checksum for the gallery data. This can be useful for invalidating
  // any old caches of this data. Note json_encode() is faster than serialize().
  $gallery_checksum = md5(json_encode($gallery_data));

  // Construct the query parameters that should be added to the XML path. Be
  // sure to retain any currently active query parameters.
  $query = array_merge(array(
    'checksum' => $gallery_checksum,
  ), drupal_get_query_parameters());

  // Build the render array for the embed markup.
  $element[0] = array(
    '#theme' => 'juicebox_embed_markup',
    '#gallery_id' => preg_replace('/[^0-9a-zA-Z-]/', '_', str_replace('/', '-', $xml_id)),
    '#gallery_xml_path' => url($xml_path, array(
      'query' => $query,
    )),
    '#settings' => $settings,
    '#data' => $gallery_data,
  );
  return $element;
}