You are here

function juicebox_page_xml in Juicebox HTML5 Responsive Image Galleries 7

Same name and namespace in other branches
  1. 7.2 juicebox.module \juicebox_page_xml()

Menu callback: generate Juicebox XML.

Note that this callback directly sets page headers and prints the XML result (if one can successfully be rendered).

See also

juicebox_menu()

1 string reference to 'juicebox_page_xml'
juicebox_menu in ./juicebox.module
Implements hook_menu().

File

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

Code

function juicebox_page_xml() {
  $got_result = FALSE;

  // We don't always know exactly how many args are being passed, so we have to
  // fetch them programmatically with func_get_args().
  $args = func_get_args();

  // If this XML request is related to a view, we first have to re-construct the
  // view before we can extract the needed XML data.
  if ($args[0] == 'view') {

    // Set key variables from the path.
    $view_name = $args[1];
    $view_display = $args[2];

    // The view arguments are what remains after the first 3 entries are
    // removed from $args.
    $view_args = array_slice($args, 3);

    // Load the view.
    $view = views_get_view($view_name);
    if ($view) {

      // Check that this user actually has access to the view we are building
      // the XML for. Kill callback with "access denied" if not.
      if (!$view
        ->access($view_display)) {
        return MENU_ACCESS_DENIED;
      }

      // Render the view.
      $view
        ->preview($view_display, $view_args);
      $data = juicebox_build_gallery_data_from_view($view, current_path());
      if (!empty($data)) {

        // Render the Juicebox XML.
        $xml = juicebox_render_gallery_xml($data);
        $got_result = TRUE;
      }
    }
  }

  // If this XML request is related to an entity field, we first have to
  // re-construct the entity and the field details before we can extract the
  // needed XML data.
  if ($args[0] == 'entity') {

    // Set key variables from the path.
    $entity_type = $args[1];
    $entity_id = $args[2];
    $field_name = $args[3];
    $display_name = $args[4];

    // Build the entity.
    $entities = entity_load($entity_type, array(
      $entity_id,
    ));
    if (isset($entities[$entity_id])) {
      $entity = $entities[$entity_id];

      // Get the bundle details.
      $info = entity_get_info($entity_type);
      if (empty($info['entity keys']['bundle'])) {
        $bundle = $entity_type;
      }
      else {
        $bundle = $entity->{$info['entity keys']['bundle']};
      }

      // Get the instance and display details.
      $instance = field_info_instance($entity_type, $field_name, $bundle);
      if (!empty($instance['display'])) {
        $settings = $instance['display']['default']['settings'];
        if (isset($instance['display'][$display_name]['settings'])) {
          $settings = $instance['display'][$display_name]['settings'];
        }

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

        // Build the field. We don't actually need a fully built field (we just
        // need the raw items) but building easily triggers all field hooks.
        $built_field = field_view_field($entity_type, $entity, $field_name, $instance['display']);

        // Check that this user has access. We need to check both field-level
        // and entity-level access.
        if (empty($built_field['#access']) || !_juicebox_check_entity_view_access($entity_type, $entity)) {
          return MENU_ACCESS_DENIED;
        }

        // If we have items proceed building the Juicebox XML data.
        if (!empty($built_field['#items'])) {

          // Build the Juicebox gallery data.
          $xml_path = current_path();
          $data = juicebox_build_gallery_data_from_entity_field($built_field['#items'], $settings, $entity, $xml_path);
          if (!empty($data)) {

            // Render the Juicebox XML.
            $xml = juicebox_render_gallery_xml($data);
            $got_result = TRUE;
          }
        }
      }
    }
  }

  // If we did not get any XML result take any special actions needed.
  if (!$got_result) {

    // Make it clear that we don't have any XML to send.
    return MENU_NOT_FOUND;
  }
  else {
    drupal_add_http_header('Content-Type', 'text/xml');

    // Bypass all themeing but still return (don't die) so that
    // drupal_page_footer() is called.
    print $xml;
    return NULL;
  }
}