blog_user.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_blog_user_page_manager_tasks() {
  10. if (!module_exists('blog')) {
  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('User blog'),
  17. 'admin title' => t('User blog'),
  18. 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying user blogs at <em>blog/%user</em>. If no variant is selected, the default Drupal user blog will be used.'),
  19. 'admin path' => 'blog/%user',
  20. // Callback to add items to the page managertask administration form:
  21. 'task admin' => 'page_manager_blog_user_task_admin',
  22. 'hook menu alter' => 'page_manager_blog_user_menu_alter',
  23. // This is task uses 'context' handlers and must implement these to give the
  24. // handler data it needs.
  25. 'handler type' => 'context', // handler type -- misnamed
  26. 'get arguments' => 'page_manager_blog_user_get_arguments',
  27. 'get context placeholders' => 'page_manager_blog_user_get_contexts',
  28. // Allow this to be enabled or disabled:
  29. 'disabled' => variable_get('page_manager_blog_user_disabled', TRUE),
  30. 'enable callback' => 'page_manager_blog_user_enable',
  31. 'access callback' => 'page_manager_blog_user_access_check',
  32. );
  33. }
  34. /**
  35. * Callback defined by page_manager_blog_user_page_manager_tasks().
  36. *
  37. * Alter the user view input so that user view comes to us rather than the
  38. * normal user view process.
  39. */
  40. function page_manager_blog_user_menu_alter(&$items, $task) {
  41. if (variable_get('page_manager_blog_user_disabled', TRUE)) {
  42. return;
  43. }
  44. // Override the user view handler for our purpose.
  45. if ($items['blog/%user_uid_optional']['page callback'] == 'blog_page_user' || variable_get('page_manager_override_anyway', FALSE)) {
  46. $items['blog/%user_uid_optional']['page callback'] = 'page_manager_blog_user';
  47. $items['blog/%user_uid_optional']['file path'] = $task['path'];
  48. $items['blog/%user_uid_optional']['file'] = $task['file'];
  49. }
  50. else {
  51. // Automatically disable this task if it cannot be enabled.
  52. variable_set('page_manager_blog_user_disabled', TRUE);
  53. if (!empty($GLOBALS['page_manager_enabling_blog_user'])) {
  54. drupal_set_message(t('Page manager module is unable to enable blog/%user because some other module already has overridden with %callback.', array('%callback' => $items['blog/%user']['page callback'])), 'error');
  55. }
  56. }
  57. }
  58. /**
  59. * Entry point for our overridden user view.
  60. *
  61. * This function asks its assigned handlers who, if anyone, would like
  62. * to run with it. If no one does, it passes through to Drupal core's
  63. * user view, which is user_page_view().
  64. */
  65. function page_manager_blog_user($account) {
  66. // Load my task plugin:
  67. $task = page_manager_get_task('blog_user');
  68. // Load the account into a context.
  69. ctools_include('context');
  70. ctools_include('context-task-handler');
  71. $contexts = ctools_context_handler_get_task_contexts($task, '', array($account));
  72. $output = ctools_context_handler_render($task, '', $contexts, array($account->uid));
  73. if ($output !== FALSE) {
  74. return $output;
  75. }
  76. module_load_include('inc', 'blog', 'blog.pages');
  77. $function = 'blog_page_user';
  78. foreach (module_implements('page_manager_override') as $module) {
  79. $call = $module . '_page_manager_override';
  80. if (($rc = $call('blog_user')) && function_exists($rc)) {
  81. $function = $rc;
  82. break;
  83. }
  84. }
  85. // Otherwise, fall back.
  86. return $function($account);
  87. }
  88. /**
  89. * Callback to get arguments provided by this task handler.
  90. *
  91. * Since this is the node view and there is no UI on the arguments, we
  92. * create dummy arguments that contain the needed data.
  93. */
  94. function page_manager_blog_user_get_arguments($task, $subtask_id) {
  95. return array(
  96. array(
  97. 'keyword' => 'user',
  98. 'identifier' => t('User being viewed'),
  99. 'id' => 1,
  100. 'name' => 'uid',
  101. 'settings' => array(),
  102. ),
  103. );
  104. }
  105. /**
  106. * Callback to get context placeholders provided by this handler.
  107. */
  108. function page_manager_blog_user_get_contexts($task, $subtask_id) {
  109. return ctools_context_get_placeholders_from_argument(page_manager_blog_user_get_arguments($task, $subtask_id));
  110. }
  111. /**
  112. * Callback to enable/disable the page from the UI.
  113. */
  114. function page_manager_blog_user_enable($cache, $status) {
  115. variable_set('page_manager_blog_user_disabled', $status);
  116. // Set a global flag so that the menu routine knows it needs
  117. // to set a message if enabling cannot be done.
  118. if (!$status) {
  119. $GLOBALS['page_manager_enabling_blog_user'] = TRUE;
  120. }
  121. }
  122. /**
  123. * Callback to determine if a page is accessible.
  124. *
  125. * @param $task
  126. * The task plugin.
  127. * @param $subtask_id
  128. * The subtask id
  129. * @param $contexts
  130. * The contexts loaded for the task.
  131. *
  132. * @return
  133. * TRUE if the current user can access the page.
  134. */
  135. function page_manager_blog_user_access_check($task, $subtask_id, $contexts) {
  136. $context = reset($contexts);
  137. return blog_page_user_access($context->data);
  138. }