You are here

function view_mode_page_change_entity_view_mode in View Mode Page 8.2

Same name and namespace in other branches
  1. 7.2 view_mode_page.module \view_mode_page_change_entity_view_mode()

Render the entity with the given view mode

This function renders the entity with the given view mode. This is matched from the hook_menu item that is added for the pattern specified via the VMP in the admin.

There are two hooks called from this function to allow developers to change or influence the output.

See also

hook_view_mode_page_pre_entity_view()

hook_view_mode_page_post_entity_view()

1 call to view_mode_page_change_entity_view_mode()
view_mode_page_change_view_mode in ./view_mode_page.module
DEPRECATED: Render the node with the given view mode.
1 string reference to 'view_mode_page_change_entity_view_mode'
view_mode_page_menu_alter in ./view_mode_page.module
Implements hook_menu_alter().

File

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

Code

function view_mode_page_change_entity_view_mode() {

  // figure out the arguments
  $args = func_get_args();
  if (count($args) == 1) {
    $settings = $args;
  }
  else {
    $settings = array();
    $setting_keys = array(
      'entity_type',
      'content_type',
      'view_mode',
      'url_pattern',
      'show_title',
      'title',
    );
    if (count($args) > count($setting_keys)) {
      watchdog('view_mode_page', 'There are more arguments to view_mode_page_change_entity_view_mode than there should be: ' . var_export($args, TRUE), WATCHDOG_ERROR);
    }
    for ($i = 0; $i < count($args); $i++) {
      if (!isset($setting_keys[$i])) {
        continue;
      }
      $settings[$setting_keys[$i]] = $args[$i];
    }
  }
  $settings = view_mode_page_assert_settings($settings, array(
    'content_type',
    'url_pattern',
    'view_mode',
  ), array(
    'entity_type' => 'node',
    'show_title' => FALSE,
    'title' => '',
  ));

  // If the args are simply node/N we can get the node ID that way.
  if (arg(0) == $settings['entity_type'] && 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('/', $settings['url_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);
  }

  // get the local tasks for the entity path by temporarily setting the
  // $_GET['q'] to the entity path, running menu_local_tasks(), and then
  // restoring
  $q = $_GET['q'];
  $_GET['q'] = $settings['entity_type'] . '/' . $nid;
  menu_local_tasks();
  $_GET['q'] = $q;

  // get the entity
  $entity = entity_load($settings['entity_type'], array(
    $nid,
  ));
  if ($entity) {

    // entity_load returns an array
    $entity = array_pop($entity);
  }

  // Check that we only use this on the specified content types.
  if ($entity) {
    if (isset($entity->type) && $entity->type != $settings['content_type']) {
      return drupal_not_found();
    }
    elseif (isset($entity->vocabulary_machine_name) && $entity->vocabulary_machine_name != $settings['content_type']) {
      return drupal_not_found();
    }
  }

  // Check that the user can access this entity
  if (!entity_access('view', $settings['entity_type'], $entity)) {
    return drupal_access_denied();
  }

  // call our pre_view hooks
  $entity = module_invoke_all('view_mode_page_pre_view', $entity, $settings['content_type'], $settings['view_mode'], $settings['url_pattern']);
  $entity = module_invoke_all('view_mode_page_pre_entity_view', $entity, $settings['entity_type'], $settings['content_type'], $settings['view_mode'], $settings['url_pattern']);

  // create/render the entity
  $entity = $entity[0];
  $view = entity_view($settings['entity_type'], array(
    $entity,
  ), $settings['view_mode']);
  if ($settings['show_title']) {
    if (!$settings['title'] && isset($entity->title)) {
      $settings['title'] = $entity->title;
    }
    $token_data = array(
      $settings['entity_type'] => $entity,
    );
    $title = token_replace($settings['title'], $token_data, array(
      'sanitize' => FALSE,
    ));
    drupal_set_title($title);
  }

  // call the post_view hooks
  $view = module_invoke_all('view_mode_page_post_view', $entity, $view, $settings['content_type'], $settings['view_mode'], $settings['url_pattern']);
  $view = module_invoke_all('view_mode_page_post_entity_view', $entity, $view, $settings['entity_type'], $settings['content_type'], $settings['view_mode'], $settings['url_pattern']);
  return $view;
}