node_view.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Handle the 'node view' override task.
  5. *
  6. * This plugin overrides node/%node and reroutes it to the page manager, where
  7. * a list of tasks can be used to service this request based upon criteria
  8. * supplied by access plugins.
  9. */
  10. /**
  11. * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
  12. * more information.
  13. */
  14. function page_manager_node_view_page_manager_tasks() {
  15. return array(
  16. // This is a 'page' task and will fall under the page admin UI
  17. 'task type' => 'page',
  18. 'title' => t('Node template'),
  19. 'admin title' => t('Node template'),
  20. 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying nodes at <em>node/%node</em>. If you add variants, you may use selection criteria such as node type or language or user access to provide different views of nodes. If no variant is selected, the default Drupal node view will be used. This page only affects nodes viewed as pages, it will not affect nodes viewed in lists or at other locations. Also please note that if you are using pathauto, aliases may make a node to be somewhere else, but as far as Drupal is concerned, they are still at node/%node.'),
  21. 'admin path' => 'node/%node',
  22. // Menu hooks so that we can alter the node/%node menu entry to point to us.
  23. 'hook menu' => 'page_manager_node_view_menu',
  24. 'hook menu alter' => 'page_manager_node_view_menu_alter',
  25. // This is task uses 'context' handlers and must implement these to give the
  26. // handler data it needs.
  27. 'handler type' => 'context',
  28. 'get arguments' => 'page_manager_node_view_get_arguments',
  29. 'get context placeholders' => 'page_manager_node_view_get_contexts',
  30. // Allow this to be enabled or disabled:
  31. 'disabled' => variable_get('page_manager_node_view_disabled', TRUE),
  32. 'enable callback' => 'page_manager_node_view_enable',
  33. 'access callback' => 'page_manager_node_view_access_check',
  34. );
  35. }
  36. /**
  37. * Callback defined by page_manager_node_view_page_manager_tasks().
  38. *
  39. * Alter the node view input so that node view comes to us rather than the
  40. * normal node view process.
  41. */
  42. function page_manager_node_view_menu_alter(&$items, $task) {
  43. if (variable_get('page_manager_node_view_disabled', TRUE)) {
  44. return;
  45. }
  46. // Override the node view handler for our purpose.
  47. $callback = $items['node/%node']['page callback'];
  48. if ($callback == 'node_page_view' || variable_get('page_manager_override_anyway', FALSE)) {
  49. $items['node/%node']['page callback'] = 'page_manager_node_view_page';
  50. $items['node/%node']['file path'] = $task['path'];
  51. $items['node/%node']['file'] = $task['file'];
  52. }
  53. else {
  54. // automatically disable this task if it cannot be enabled.
  55. variable_set('page_manager_node_view_disabled', TRUE);
  56. if (!empty($GLOBALS['page_manager_enabling_node_view'])) {
  57. drupal_set_message(t('Page manager module is unable to enable node/%node because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error');
  58. }
  59. }
  60. // @todo override node revision handler as well?
  61. }
  62. /**
  63. * Entry point for our overridden node view.
  64. *
  65. * This function asks its assigned handlers who, if anyone, would like
  66. * to run with it. If no one does, it passes through to Drupal core's
  67. * node view, which is node_page_view().
  68. */
  69. function page_manager_node_view_page($node) {
  70. // Load my task plugin
  71. $task = page_manager_get_task('node_view');
  72. // Load the node into a context.
  73. ctools_include('context');
  74. ctools_include('context-task-handler');
  75. // Load all contexts.
  76. $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
  77. // Build the full output using the configured CTools plugin.
  78. $output = ctools_context_handler_render($task, '', $contexts, array($node->nid));
  79. if ($output !== FALSE) {
  80. node_tag_new($node);
  81. return $output;
  82. }
  83. // Try loading an override plugin.
  84. foreach (module_implements('page_manager_override') as $module) {
  85. $call = $module . '_page_manager_override';
  86. if (($rc = $call('node_view')) && function_exists($rc)) {
  87. return $rc($node);
  88. }
  89. }
  90. // Prepare the node to be displayed so all of the regular hooks are triggered.
  91. $default_output = node_page_view($node);
  92. // Otherwise, fall back to the default output generated by node_page_view().
  93. return $default_output;
  94. }
  95. /**
  96. * Callback to get arguments provided by this task handler.
  97. *
  98. * Since this is the node view and there is no UI on the arguments, we
  99. * create dummy arguments that contain the needed data.
  100. */
  101. function page_manager_node_view_get_arguments($task, $subtask_id) {
  102. return array(
  103. array(
  104. 'keyword' => 'node',
  105. 'identifier' => t('Node being viewed'),
  106. 'id' => 1,
  107. 'name' => 'entity_id:node',
  108. 'settings' => array(),
  109. ),
  110. );
  111. }
  112. /**
  113. * Callback to get context placeholders provided by this handler.
  114. */
  115. function page_manager_node_view_get_contexts($task, $subtask_id) {
  116. return ctools_context_get_placeholders_from_argument(page_manager_node_view_get_arguments($task, $subtask_id));
  117. }
  118. /**
  119. * Callback to enable/disable the page from the UI.
  120. */
  121. function page_manager_node_view_enable($cache, $status) {
  122. variable_set('page_manager_node_view_disabled', $status);
  123. // Set a global flag so that the menu routine knows it needs
  124. // to set a message if enabling cannot be done.
  125. if (!$status) {
  126. $GLOBALS['page_manager_enabling_node_view'] = TRUE;
  127. }
  128. }
  129. /**
  130. * Callback to determine if a page is accessible.
  131. *
  132. * @param $task
  133. * The task plugin.
  134. * @param $subtask_id
  135. * The subtask id
  136. * @param $contexts
  137. * The contexts loaded for the task.
  138. * @return
  139. * TRUE if the current user can access the page.
  140. */
  141. function page_manager_node_view_access_check($task, $subtask_id, $contexts) {
  142. $context = reset($contexts);
  143. return node_access('view', $context->data);
  144. }