node_edit.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_node_edit_page_manager_tasks() {
  7. return array(
  8. // This is a 'page' task and will fall under the page admin UI
  9. 'task type' => 'page',
  10. 'title' => t('Node add/edit form'),
  11. 'admin title' => t('Node add/edit form'),
  12. 'admin description' => t('When enabled, this overrides the default Drupal behavior for adding or edit nodes at <em>node/%node/edit</em> and <em>node/add/%node_type</em>. If you add variants, you may use selection criteria such as node type or language or user access to provide different edit forms for nodes. If no variant is selected, the default Drupal node edit will be used.'),
  13. 'admin path' => 'node/%node/edit',
  14. // Menu hooks so that we can alter the node/%node menu entry to point to us.
  15. 'hook menu' => 'page_manager_node_edit_menu',
  16. 'hook menu alter' => 'page_manager_node_edit_menu_alter',
  17. // This is task uses 'context' handlers and must implement these to give the
  18. // handler data it needs.
  19. 'handler type' => 'context',
  20. 'get arguments' => 'page_manager_node_edit_get_arguments',
  21. 'get context placeholders' => 'page_manager_node_edit_get_contexts',
  22. // Allow this to be enabled or disabled:
  23. 'disabled' => variable_get('page_manager_node_edit_disabled', TRUE),
  24. 'enable callback' => 'page_manager_node_edit_enable',
  25. 'access callback' => 'page_manager_node_edit_access_check',
  26. );
  27. }
  28. /**
  29. * Callback defined by page_manager_node_edit_page_manager_tasks().
  30. *
  31. * Alter the node edit input so that node edit comes to us rather than the
  32. * normal node edit process.
  33. */
  34. function page_manager_node_edit_menu_alter(&$items, $task) {
  35. if (variable_get('page_manager_node_edit_disabled', TRUE)) {
  36. return;
  37. }
  38. $callback = $items['node/%node/edit']['page callback'];
  39. // Override the node edit handler for our purpose.
  40. if ($callback == 'node_page_edit' || variable_get('page_manager_override_anyway', FALSE)) {
  41. $items['node/%node/edit']['page callback'] = 'page_manager_node_edit';
  42. $items['node/%node/edit']['file path'] = $task['path'];
  43. $items['node/%node/edit']['file'] = $task['file'];
  44. }
  45. else {
  46. variable_set('page_manager_node_edit_disabled', TRUE);
  47. if (!empty($GLOBALS['page_manager_enabling_node_edit'])) {
  48. drupal_set_message(t('Page manager module is unable to enable node/%node/edit because some other module already has overridden with %callback.', array('%callback' => $callback)), 'warning');
  49. }
  50. return;
  51. }
  52. // Also catch node/add handling:
  53. foreach (node_type_get_types() as $type) {
  54. $path = 'node/add/' . str_replace('_', '-', $type->type);
  55. if ($items[$path]['page callback'] != 'node_add') {
  56. if (!empty($GLOBALS['page_manager_enabling_node_edit'])) {
  57. drupal_set_message(t('Page manager module is unable to override @path because some other module already has overridden with %callback. Node edit will be enabled but that edit path will not be overridden.', array('@path' => $path, '%callback' => $items[$path]['page callback'])), 'warning');
  58. }
  59. continue;
  60. }
  61. $items[$path]['page callback'] = 'page_manager_node_add';
  62. $items[$path]['file path'] = $task['path'];
  63. $items[$path]['file'] = $task['file'];
  64. // Why str_replace things back?
  65. $items[$path]['page arguments'] = array($type->type);
  66. }
  67. }
  68. /**
  69. * Entry point for our overridden node edit.
  70. *
  71. * This function asks its assigned handlers who, if anyone, would like
  72. * to run with it. If no one does, it passes through to Drupal core's
  73. * node edit, which is node_page_edit().
  74. */
  75. function page_manager_node_edit($node) {
  76. // Load my task plugin
  77. $task = page_manager_get_task('node_edit');
  78. // Load the node into a context.
  79. ctools_include('context');
  80. ctools_include('context-task-handler');
  81. $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
  82. $arg = array(isset($node->nid) ? $node->nid : $node->type);
  83. $output = ctools_context_handler_render($task, '', $contexts, $arg);
  84. if ($output === FALSE) {
  85. // Fall back!
  86. // We've already built the form with the context, so we can't build it again, or
  87. // form_clean_id will mess up our ids. But we don't really need to, either:
  88. $context = reset($contexts);
  89. $output = $context->form;
  90. }
  91. return $output;
  92. }
  93. /**
  94. * Callback to handle the process of adding a node.
  95. *
  96. * This creates a basic $node and passes that off to page_manager_node_edit().
  97. * It is modelled after Drupal's node_add() function.
  98. *
  99. * Unlike node_add() we do not need to check node_access because that was
  100. * already checked by the menu system.
  101. */
  102. function page_manager_node_add($type) {
  103. global $user;
  104. $types = node_type_get_types();
  105. // Initialize settings:
  106. $node = (object) array(
  107. 'uid' => $user->uid,
  108. 'name' => (isset($user->name) ? $user->name : ''),
  109. 'type' => $type,
  110. 'language' => LANGUAGE_NONE,
  111. );
  112. drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
  113. return page_manager_node_edit($node);
  114. }
  115. /**
  116. * Callback to get arguments provided by this task handler.
  117. *
  118. * Since this is the node edit and there is no UI on the arguments, we
  119. * create dummy arguments that contain the needed data.
  120. */
  121. function page_manager_node_edit_get_arguments($task, $subtask_id) {
  122. return array(
  123. array(
  124. 'keyword' => 'node',
  125. 'identifier' => t('Node being edited'),
  126. 'id' => 1,
  127. 'name' => 'node_edit',
  128. 'settings' => array(),
  129. ),
  130. );
  131. }
  132. /**
  133. * Callback to get context placeholders provided by this handler.
  134. */
  135. function page_manager_node_edit_get_contexts($task, $subtask_id) {
  136. return ctools_context_get_placeholders_from_argument(page_manager_node_edit_get_arguments($task, $subtask_id));
  137. }
  138. /**
  139. * Callback to enable/disable the page from the UI.
  140. */
  141. function page_manager_node_edit_enable($cache, $status) {
  142. variable_set('page_manager_node_edit_disabled', $status);
  143. // Set a global flag so that the menu routine knows it needs
  144. // to set a message if enabling cannot be done.
  145. if (!$status) {
  146. $GLOBALS['page_manager_enabling_node_edit'] = TRUE;
  147. }
  148. }
  149. /**
  150. * Callback to determine if a page is accessible.
  151. *
  152. * @param $task
  153. * The task plugin.
  154. * @param $subtask_id
  155. * The subtask id
  156. * @param $contexts
  157. * The contexts loaded for the task.
  158. * @return
  159. * TRUE if the current user can access the page.
  160. */
  161. function page_manager_node_edit_access_check($task, $subtask_id, $contexts) {
  162. $context = reset($contexts);
  163. return node_access('update', $context->data);
  164. }