You are here

function juicebox_field_formatter_view in Juicebox HTML5 Responsive Image Galleries 7.2

Same name and namespace in other branches
  1. 7 juicebox.module \juicebox_field_formatter_view()

Implements hook_field_formatter_view().

File

includes/juicebox.field.inc, line 189
Contains all hooks and related methods for the juicebox_formatter field formatter.

Code

function juicebox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

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

  // 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']};

  // Get display data (such as the view mode) from our custom display variable.
  // This was set for us in juicebox_field_display_alter().
  $psuedo_instance = empty($display['juicebox_vm']) ? TRUE : FALSE;
  $add_js = empty($display['juicebox_vm_incompatible']) ? TRUE : FALSE;
  $display_name = !$psuedo_instance ? $display['juicebox_vm'] : 'unknown';

  // Generate xml path details.
  $xml_id = 'field/' . $entity_type . '/' . $entity_id . '/' . $field_name . '/' . $display_name;
  $xml_args = explode('/', $xml_id);
  $xml_path = 'juicebox/xml/' . $xml_id;

  // If this is a field on a node, we can calculate the path to edit the field
  // (used for admin contextual links).
  $context = array();
  if ($entity_type == 'node' && !$psuedo_instance) {
    $vm_settings = field_view_mode_settings($entity_type, $instance['bundle']);
    $display_name_conf = !empty($vm_settings[$display_name]['custom_settings']) ? $display_name : 'default';
    $context['conf_path'] = 'admin/structure/types/manage/' . $instance['bundle'] . '/display/' . $display_name_conf;
  }

  // Try building the gallery and its XML.
  try {

    // Initialize the gallery.
    $juicebox = juicebox();
    $juicebox
      ->init($xml_args, $display['settings'], $items);

    // Build the gallery.
    juicebox_field_build_gallery($juicebox, $items);

    // Create a render array with the gallery markup. Also include the built
    // gallery for external reference.
    $element[0] = array(
      'gallery' => $juicebox
        ->buildEmbed($xml_path, $add_js, $context, $psuedo_instance),
      '#juicebox' => $juicebox,
    );
  } catch (Exception $e) {
    $message = 'Exception building Juicebox embed code for field: !message in %function (line %line of %file).';
    watchdog_exception('juicebox', $e, $message);
  }
  return $element;
}