comment_reply.inc 5.4 KB

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