You are here

function view_mode_page_change_view_mode in View Mode Page 7

Same name and namespace in other branches
  1. 8 view_mode_page.module \view_mode_page_change_view_mode()
  2. 8.2 view_mode_page.module \view_mode_page_change_view_mode()
  3. 7.2 view_mode_page.module \view_mode_page_change_view_mode()

Render the node with the given view mode.

Parameters

string $content_type: The name of the content type that the pattern is used for.

string $view_mode: The name of the view mode.

string $pattern: The URL pattern that is used in the hook_menu() call.

1 string reference to 'view_mode_page_change_view_mode'
view_mode_page_menu_alter in ./view_mode_page.module
Implements hook_menu_alter().

File

./view_mode_page.module, line 144
View Mode Page module allows users to add a page for a specific view mode.

Code

function view_mode_page_change_view_mode($content_type, $view_mode, $pattern, $show_title = FALSE, $title = '') {

  // If the args are simply node/N we can get the node ID that way.
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
  }
  else {

    // This is an alias, so we need to lookup the path of the node based on
    // the pattern
    //
    // To do this, we look at the url_pattern for our view_mode_page and find
    // what position the wildcard is in. based on this we can figure out what
    // section of the path is likely the url/alias for the node.
    $pattern_array = explode('/', $pattern);
    $url_array = arg();

    // Find the wildcard position in the pattern.
    $wildcard_positions = array_keys($pattern_array, '%');
    $wildcard_position = array_pop($wildcard_positions) + 1;

    // Create the node url/alias based on the wildcard position.
    $node_url = implode('/', array_slice($url_array, 0, $wildcard_position));

    // Look that up...
    $source_path = drupal_lookup_path('source', $node_url);
    if (!$source_path) {
      return drupal_not_found();
    }

    // Parse out the nid.
    $source_path_array = explode('/', $source_path);
    $nid = array_pop($source_path_array);
  }
  $node = node_load($nid);

  // Check that we only use this on the specified content types.
  if ($node->type != $content_type) {
    return drupal_not_found();
  }

  // Check if they have access to this node
  if (!node_access('view', $node)) {
    return drupal_access_denied();
  }
  $node = module_invoke_all('view_mode_page_pre_view', $node, $content_type, $view_mode, $pattern);
  $node = $node[0];
  $view = node_view($node, $view_mode);
  if ($show_title) {
    if (!$title && isset($node->title)) {
      $title = $node->title;
    }
    $tokenData = array(
      'node' => $node,
    );
    $title = token_replace($title, $tokenData);
    drupal_set_title($title);
  }
  $view = module_invoke_all('view_mode_page_post_view', $node, $view, $content_type, $view_mode, $pattern);
  return $view;
}