blog_user.inc 5.0 KB

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