You are here

colorbox_node.module in Colorbox Node 7

Same filename and directory in other branches
  1. 7.3 colorbox_node.module
  2. 7.2 colorbox_node.module

Creates a menu callback with support for displaying a node inside of a colorbox.

File

colorbox_node.module
View source
<?php

/**
 * @file
 * Creates a menu callback with support for displaying a node inside of
 * a colorbox.
 */

/*
 * Implments hook_init().
 */
function colorbox_node_init() {
  drupal_add_js(drupal_get_path('module', 'colorbox_node') . '/colorbox_node.js');
}

/*
 * Implements hook_menu().
 */
function colorbox_node_menu() {
  $items['colorbox/%colorbox_node_url'] = array(
    'page callback' => 'colorbox_node_output',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'access content',
    ),
    'load arguments' => array(
      '%map',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/*
 * Displays a node view without the page template.
 * Takes the path as our argument and from that we
 * will determine the internal path and node id.
 */
function colorbox_node_output($path) {

  // @TODO, we should be able to use this for users too,
  // Lets beef this up and make it more intelligent around
  // the system path.
  // Retrieve our node id from the path.
  $nid = array_pop(explode('/', $path));

  // If we have a node, lets display it
  if ($node = node_load($nid)) {

    // @TODO:  If this is a webform, lets modifiy it and add
    // ajax support to keep the webform inside the modal incase
    // of any error messages.
    // We need to fix our destination for contextual links.
    // This prevents a redirect to a screen with no styles.
    $_GET['destination'] = drupal_lookup_path('alias', 'node/' . $node->nid);

    // Render the node to the screen.
    print drupal_render(node_view($node));
  }
  else {
    print '<h2>' . t('Page Not Found.') . '</h2>';
    print t('The following page %path was not found.', array(
      '%path' => $path,
    ));

    // This isn't a node or the node failed to load.
    watchdog('colorbox_node', t('Failed to load a node while attempting to display it through a colorbox.<br />Node ID: ' . $node_id));
  }

  // Do our end of page tasks.  This is very important if using
  // the session for anonymous users.
  drupal_exit();
}

/*
 * Takes a path as an array of all our arguments from the
 * url structure.  We then put that structure back together,
 * find our system path and then return it.
 */
function colorbox_node_url_load($arg, $path = array()) {

  // First lets remove colorbox
  array_shift($path);

  // Lets determine if we have a prefix from our languages.
  if (module_exists('locale') && function_exists('language_url_split_prefix')) {

    // Get our language list to pass into our url splitter.
    $languages = language_list();

    // Turn the path into a string so we can then remove our prefix.
    $path_string = implode('/', $path);
    $language_path = language_url_split_prefix($path_string, $languages);

    // Separate out the returned path and language.  We should always
    // have two indexes, the first is the language, second is the path.
    $language = $language_path[0] ? $language_path[0]->language : '';
    $final_path = $language_path[1];

    // Lets find our path based on the current translation.
    return drupal_get_normal_path($final_path, $language);
  }

  // Now lets buld our path to return our system path,
  return drupal_get_normal_path(implode('/', $path));
}