user_edit.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. * Overrides the user profile display at user/%user.
  5. *
  6. * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
  7. * more information.
  8. */
  9. function page_manager_user_edit_page_manager_tasks() {
  10. return array(
  11. // This is a 'page' task and will fall under the page admin UI
  12. 'task type' => 'page',
  13. 'title' => t('User Edit Template'),
  14. 'admin title' => t('User edit template'),
  15. 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying user edit form at <em>user/%user/edit</em>.'),
  16. 'admin path' => 'user/%user/edit',
  17. // Callback to add items to the page managertask administration form:
  18. 'task admin' => 'page_manager_user_edit_task_admin',
  19. 'hook menu' => 'page_manager_user_edit_menu',
  20. 'hook menu alter' => 'page_manager_user_edit_menu_alter',
  21. // This is task uses 'context' handlers and must implement these to give the
  22. // handler data it needs.
  23. 'handler type' => 'context', // handler type -- misnamed
  24. 'get arguments' => 'page_manager_user_edit_get_arguments',
  25. 'get context placeholders' => 'page_manager_user_edit_get_contexts',
  26. // Allow this to be enabled or disabled:
  27. 'disabled' => variable_get('page_manager_user_edit_disabled', TRUE),
  28. 'enable callback' => 'page_manager_user_edit_enable',
  29. 'access callback' => 'page_manager_user_edit_access_check',
  30. );
  31. }
  32. /**
  33. * Callback defined by page_manager_user_view_page_manager_tasks().
  34. *
  35. * Alter the user view input so that user view comes to us rather than the
  36. * normal user view process.
  37. */
  38. function page_manager_user_edit_menu_alter(&$items, $task) {
  39. if (variable_get('page_manager_user_edit_disabled', TRUE)) {
  40. return;
  41. }
  42. // Override the user view handler for our purpose.
  43. if ($items['user/%user/edit']['page callback'] == 'drupal_get_form' || variable_get('page_manager_override_anyway', FALSE)) {
  44. $items['user/%user/edit']['page callback'] = 'page_manager_user_edit_page';
  45. $items['user/%user/edit']['page arguments'] = array(1);
  46. $items['user/%user/edit']['file path'] = $task['path'];
  47. $items['user/%user/edit']['file'] = $task['file'];
  48. if (($categories = _user_categories()) && (count($categories) > 1)) {
  49. foreach ($categories as $key => $category) {
  50. // 'account' is already handled by the MENU_DEFAULT_LOCAL_TASK.
  51. if ($category['name'] != 'account') {
  52. $items['user/%user_category/edit/' . $category['name']]['page callback'] = 'page_manager_user_edit_page';
  53. $items['user/%user_category/edit/' . $category['name']]['page arguments'] = array(1, 3);
  54. $items['user/%user_category/edit/' . $category['name']]['file path'] = $task['path'];
  55. $items['user/%user_category/edit/' . $category['name']]['file'] = $task['file'];
  56. }
  57. }
  58. }
  59. }
  60. else {
  61. // automatically disable this task if it cannot be enabled.
  62. variable_set('page_manager_user_edit_disabled', TRUE);
  63. if (!empty($GLOBALS['page_manager_enabling_user_edit'])) {
  64. drupal_set_message(t('Page manager module is unable to enable user/%user/edit because some other module already has overridden with %callback.', array('%callback' => $items['user/%user']['page callback'])), 'error');
  65. }
  66. }
  67. }
  68. /**
  69. * Entry point for our overridden user view.
  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. * user edit, which is drupal_get_form('user_profile_form',$account).
  74. */
  75. function page_manager_user_edit_page($account, $category = 'account') {
  76. // Store the category on the user for later usage.
  77. $account->user_category = $category;
  78. // Load my task plugin:
  79. $task = page_manager_get_task('user_edit');
  80. // Load the account into a context.
  81. ctools_include('context');
  82. ctools_include('context-task-handler');
  83. $contexts = ctools_context_handler_get_task_contexts($task, '', array($account));
  84. // Build content. @todo -- this may not be right.
  85. user_build_content($account);
  86. $output = ctools_context_handler_render($task, '', $contexts, array($account->uid));
  87. if (is_array($output)) {
  88. $output = drupal_render($output);
  89. }
  90. if ($output !== FALSE) {
  91. return $output;
  92. }
  93. $function = 'drupal_get_form';
  94. foreach (module_implements('page_manager_override') as $module) {
  95. $call = $module . '_page_manager_override';
  96. if (($rc = $call('user_edit')) && function_exists($rc)) {
  97. $function = $rc;
  98. break;
  99. }
  100. }
  101. // Otherwise, fall back.
  102. if ($function == 'drupal_get_form') {
  103. //In order to ajax fields to work we need to run form_load_include.
  104. //Hence we eschew drupal_get_form and manually build the info and
  105. //call drupal_build_form.
  106. $form_state = array();
  107. $form_id = 'user_profile_form';
  108. $args = array($account, $category);
  109. $form_state['build_info']['args'] = $args;
  110. form_load_include($form_state, 'inc', 'user', 'user.pages');
  111. $output = drupal_build_form($form_id, $form_state);
  112. return $output;
  113. }
  114. //fire off "view" op so that triggers still work
  115. // @todo -- this doesn't work anymore, and the alternatives seem bad.
  116. // will have to figure out how to fix this.
  117. // user_module_invoke('view', $array = array(), $account);
  118. return $function($account);
  119. }
  120. /**
  121. * Callback to get arguments provided by this task handler.
  122. *
  123. * Since this is the node view and there is no UI on the arguments, we
  124. * create dummy arguments that contain the needed data.
  125. */
  126. function page_manager_user_edit_get_arguments($task, $subtask_id) {
  127. return array(
  128. array(
  129. 'keyword' => 'user',
  130. 'identifier' => t('User being edited'),
  131. 'id' => 1,
  132. 'name' => 'user_edit',
  133. 'settings' => array(),
  134. ),
  135. );
  136. }
  137. /**
  138. * Callback to get context placeholders provided by this handler.
  139. */
  140. function page_manager_user_edit_get_contexts($task, $subtask_id) {
  141. return ctools_context_get_placeholders_from_argument(page_manager_user_edit_get_arguments($task, $subtask_id));
  142. }
  143. /**
  144. * Callback to enable/disable the page from the UI.
  145. */
  146. function page_manager_user_edit_enable($cache, $status) {
  147. variable_set('page_manager_user_edit_disabled', $status);
  148. // Set a global flag so that the menu routine knows it needs
  149. // to set a message if enabling cannot be done.
  150. if (!$status) {
  151. $GLOBALS['page_manager_enabling_user_edit'] = TRUE;
  152. }
  153. }
  154. /**
  155. * Callback to determine if a page is accessible.
  156. *
  157. * @param $task
  158. * The task plugin.
  159. * @param $subtask_id
  160. * The subtask id
  161. * @param $contexts
  162. * The contexts loaded for the task.
  163. * @return
  164. * TRUE if the current user can access the page.
  165. */
  166. function page_manager_user_edit_access_check($task, $subtask_id, $contexts) {
  167. $context = reset($contexts);
  168. return user_edit_access($context->data);
  169. }