comment_reply.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
  7. * more information.
  8. */
  9. function page_manager_comment_reply_page_manager_tasks() {
  10. if (!module_exists('comment')) {
  11. return;
  12. }
  13. return array(
  14. // This is a 'page' task and will fall under the page admin UI.
  15. 'task type' => 'page',
  16. 'title' => t('Comment Reply page'),
  17. 'admin title' => t('Comment Reply page'),
  18. 'admin description' => t('When enabled, this overrides the default Drupal behavior for the site contact page at <em>/contact</em>. If no variant is selected, the default Drupal contact form will be used.'),
  19. 'admin path' => 'comment/reply/%node',
  20. // Menu hooks so that we can alter the node/%node menu entry to point to us.
  21. 'hook menu alter' => 'page_manager_comment_reply_menu_alter',
  22. // This is task uses 'context' handlers and must implement these to give the
  23. // handler data it needs.
  24. 'handler type' => 'context',
  25. 'get arguments' => 'page_manager_comment_reply_get_arguments',
  26. 'get context placeholders' => 'page_manager_comment_reply_get_contexts',
  27. // Allow this to be enabled or disabled:
  28. 'disabled' => variable_get('page_manager_comment_reply_disabled', TRUE),
  29. 'enable callback' => 'page_manager_comment_reply_enable',
  30. 'access callback' => 'page_manager_comment_reply_check',
  31. );
  32. }
  33. /**
  34. * Callback to enable/disable the page from the UI.
  35. */
  36. function page_manager_comment_reply_enable($cache, $status) {
  37. variable_set('page_manager_comment_reply_disabled', $status);
  38. // Set a global flag so that the menu routine knows it needs
  39. // to set a message if enabling cannot be done.
  40. if (!$status) {
  41. $GLOBALS['page_manager_enabling_comment_reply'] = TRUE;
  42. }
  43. }
  44. /**
  45. * Entry point for our overridden comment.
  46. */
  47. function page_manager_comment_reply_page($node, $pid = NULL) {
  48. // Load my task plugin.
  49. $task = page_manager_get_task('comment_reply');
  50. // Load the node into a context.
  51. ctools_include('context');
  52. ctools_include('context-task-handler');
  53. $contexts = ctools_context_handler_get_task_contexts($task, '', array($node, $pid));
  54. if (array_key_exists('argument_cid_3', $contexts) && $contexts['argument_cid_3']->data->nid != $node->nid) {
  55. // Attempting to reply to a comment not belonging to the current nid.
  56. drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
  57. drupal_goto("node/$node->nid");
  58. }
  59. $output = ctools_context_handler_render($task, '', $contexts, array($node, $pid));
  60. if ($output !== FALSE) {
  61. return $output;
  62. }
  63. $function = 'comment_reply';
  64. foreach (module_implements('page_manager_override') as $module) {
  65. $call = $module . '_page_manager_override';
  66. if (($rc = $call('comment_reply')) && function_exists($rc)) {
  67. $function = $rc;
  68. break;
  69. }
  70. }
  71. module_load_include('inc', 'comment', 'comment.pages');
  72. return $function($node, $pid);
  73. }
  74. /**
  75. * Callback to get arguments provided by this task handler.
  76. *
  77. * Since this is the node view and there is no UI on the arguments, we
  78. * create dummy arguments that contain the needed data.
  79. */
  80. function page_manager_comment_reply_get_arguments($task, $subtask_id) {
  81. return array(
  82. array(
  83. 'keyword' => 'node',
  84. 'identifier' => t('Node being commented on'),
  85. 'id' => 2,
  86. 'name' => 'entity_id:node',
  87. 'settings' => array(),
  88. ),
  89. array(
  90. 'keyword' => 'comment',
  91. 'identifier' => t('Comment being replied to'),
  92. 'id' => 3,
  93. 'name' => 'entity_id:comment',
  94. 'settings' => array(),
  95. ),
  96. );
  97. }
  98. /**
  99. * Callback to get context placeholders provided by this handler.
  100. */
  101. function page_manager_comment_reply_get_contexts($task, $subtask_id) {
  102. return ctools_context_get_placeholders_from_argument(page_manager_comment_reply_get_arguments($task, $subtask_id));
  103. }
  104. /**
  105. * Callback defined by page_manager_node_view_page_manager_tasks().
  106. *
  107. * Alter the node view input so that node view comes to us rather than the
  108. * normal node view process.
  109. */
  110. function page_manager_comment_reply_menu_alter(&$items, $task) {
  111. if (variable_get('page_manager_comment_reply_disabled', TRUE)) {
  112. return;
  113. }
  114. // Override the node view handler for our purpose.
  115. $callback = $items['comment/reply/%node']['page callback'];
  116. if ($callback == 'comment_reply' || variable_get('page_manager_override_anyway', FALSE)) {
  117. $items['comment/reply/%node']['page callback'] = 'page_manager_comment_reply_page';
  118. $items['comment/reply/%node']['file path'] = $task['path'];
  119. $items['comment/reply/%node']['file'] = $task['file'];
  120. }
  121. else {
  122. // Automatically disable this task if it cannot be enabled.
  123. variable_set('page_manager_comment_reply_disabled', TRUE);
  124. if (!empty($GLOBALS['page_manager_enabling_comment_reply'])) {
  125. drupal_set_message(t('Page manager module is unable to enable comment/reply/%node because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error');
  126. }
  127. }
  128. // @todo override node revision handler as well?
  129. }
  130. /**
  131. * Callback to determine if a page is accessible.
  132. *
  133. * @param $task
  134. * The task plugin.
  135. * @param $subtask_id
  136. * The subtask id
  137. * @param $contexts
  138. * The contexts loaded for the task.
  139. *
  140. * @return
  141. * TRUE if the current user can access the page.
  142. */
  143. function page_manager_comment_reply_access_check($task, $subtask_id, $contexts) {
  144. $context = reset($contexts);
  145. return TRUE;
  146. }