term_view.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * @file
  4. * Handle the 'term view' override task.
  5. *
  6. * This plugin overrides term/%term and reroutes it to the page manager, where
  7. * a list of tasks can be used to service this request based upon criteria
  8. * supplied by access plugins.
  9. */
  10. /**
  11. * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
  12. * more information.
  13. */
  14. function page_manager_term_view_page_manager_tasks() {
  15. if (module_exists('taxonomy')) {
  16. return array(
  17. // This is a 'page' task and will fall under the page admin UI
  18. 'task type' => 'page',
  19. 'title' => t('Taxonomy term template'),
  20. 'admin title' => t('Taxonomy term template'),
  21. 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying taxonomy terms at <em>taxonomy/term/%term</em>. If you add variants, you may use selection criteria such as vocabulary or user access to provide different displays of the taxonomy term and associated nodes. If no variant is selected, the default Drupal taxonomy term display will be used. This page only affects items actually displayed ad taxonomy/term/%term. Some taxonomy terms, such as forums, have their displays moved elsewhere. Also please note that if you are using pathauto, aliases may make a taxonomy terms appear somewhere else, but as far as Drupal is concerned, they are still at taxonomy/term/%term.'),
  22. 'admin path' => 'taxonomy/term/%taxonomy_term',
  23. 'admin summary' => 'page_manager_term_view_admin_summary',
  24. // Menu hooks so that we can alter the term/%term menu entry to point to us.
  25. 'hook menu' => 'page_manager_term_view_menu',
  26. 'hook menu alter' => 'page_manager_term_view_menu_alter',
  27. // Provide a setting to the primary settings UI for Panels
  28. 'admin settings' => 'page_manager_term_view_admin_settings',
  29. // Even though we don't have subtasks, this allows us to save our settings.
  30. 'save subtask callback' => 'page_manager_term_view_save',
  31. // Callback to add items to the page manager task administration form:
  32. 'task admin' => 'page_manager_term_view_task_admin',
  33. // This is task uses 'context' handlers and must implement these to give the
  34. // handler data it needs.
  35. 'handler type' => 'context',
  36. 'get arguments' => 'page_manager_term_view_get_arguments',
  37. 'get context placeholders' => 'page_manager_term_view_get_contexts',
  38. // Allow this to be enabled or disabled:
  39. 'disabled' => variable_get('page_manager_term_view_disabled', TRUE),
  40. 'enable callback' => 'page_manager_term_view_enable',
  41. 'access callback' => 'page_manager_term_view_access_check',
  42. // Allow additional operations
  43. 'operations' => array(
  44. 'settings' => array(
  45. 'title' => t('Settings'),
  46. 'description' => t('Edit name, path and other basic settings for the page.'),
  47. 'form' => 'page_manager_term_view_settings',
  48. ),
  49. ),
  50. );
  51. }
  52. }
  53. /**
  54. * Callback defined by page_manager_term_view_page_manager_tasks().
  55. *
  56. * Alter the term view input so that term view comes to us rather than the
  57. * normal term view process.
  58. */
  59. function page_manager_term_view_menu_alter(&$items, $task) {
  60. if (variable_get('page_manager_term_view_disabled', TRUE)) {
  61. return;
  62. }
  63. // Override the term view handler for our purpose, but only if someone else
  64. // has not already done so.
  65. if (isset($items['taxonomy/term/%taxonomy_term']) && $items['taxonomy/term/%taxonomy_term']['page callback'] == 'taxonomy_term_page' || variable_get('page_manager_override_anyway', FALSE)) {
  66. $items['taxonomy/term/%taxonomy_term']['page callback'] = 'page_manager_term_view_page';
  67. $items['taxonomy/term/%taxonomy_term']['file path'] = $task['path'];
  68. $items['taxonomy/term/%taxonomy_term']['file'] = $task['file'];
  69. }
  70. else {
  71. // automatically disable this task if it cannot be enabled.
  72. variable_set('page_manager_term_view_disabled', TRUE);
  73. if (isset($items['taxonomy/term/%taxonomy_term']['page callback'])) {
  74. $callback = $items['taxonomy/term/%taxonomy_term']['page callback'];
  75. }
  76. // Because Views changes %taxonomy_term to %views_arg, check to see if that
  77. // is why we can't enable:
  78. else if (isset($items['taxonomy/term/%views_arg']['page callback'])) {
  79. $callback = $items['taxonomy/term/%views_arg']['page callback'];
  80. }
  81. else {
  82. $callback = t('an unknown callback');
  83. }
  84. if (!empty($GLOBALS['page_manager_enabling_term_view'])) {
  85. drupal_set_message(t('Page manager module is unable to enable taxonomy/term/%taxonomy_term because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error');
  86. }
  87. }
  88. }
  89. /**
  90. * Entry point for our overridden term view.
  91. *
  92. * This function asks its assigned handlers who, if anyone, would like
  93. * to run with it. If no one does, it passes through to Drupal core's
  94. * term view, which is term_page_view().
  95. */
  96. function page_manager_term_view_page($term, $depth = NULL) {
  97. // Prep the term to be displayed so all of the regular hooks are triggered.
  98. // Rather than calling taxonomy_term_page() directly, as it that would
  99. // potentially load nodes that were not necessary, execute some of the code
  100. // prior to identifying the correct CTools or Page Manager task handler and
  101. // only proceed with the rest of the code if necessary.
  102. // Assign the term name as the page title.
  103. drupal_set_title($term->name);
  104. // If there is a menu link to this term, the link becomes the last part
  105. // of the active trail, and the link name becomes the page title.
  106. // Thus, we must explicitly set the page title to be the node title.
  107. $uri = entity_uri('taxonomy_term', $term);
  108. // Set the term path as the canonical URL to prevent duplicate content.
  109. drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  110. // Set the non-aliased path as a default shortlink.
  111. drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
  112. // Trigger the main
  113. $build = taxonomy_term_show($term);
  114. // Load my task plugin
  115. $task = page_manager_get_task('term_view');
  116. // Load the term into a context.
  117. ctools_include('context');
  118. ctools_include('context-task-handler');
  119. $contexts = ctools_context_handler_get_task_contexts($task, '', array($term, $depth));
  120. if (empty($contexts)) {
  121. return MENU_NOT_FOUND;
  122. }
  123. // Build the full output using the configured CTools plugin.
  124. $output = ctools_context_handler_render($task, '', $contexts, array($term->tid));
  125. if ($output !== FALSE) {
  126. return $output;
  127. }
  128. // Try loading an override plugin.
  129. foreach (module_implements('page_manager_override') as $module) {
  130. $call = $module . '_page_manager_override';
  131. if (($rc = $call('term_view')) && function_exists($rc)) {
  132. return $rc($term, $depth);
  133. }
  134. }
  135. // Otherwise, fall back to replicating the output normally generated by
  136. // taxonomy_term_page().
  137. // Build breadcrumb based on the hierarchy of the term.
  138. $current = (object) array(
  139. 'tid' => $term->tid,
  140. );
  141. // @todo This overrides any other possible breadcrumb and is a pure hard-coded
  142. // presumption. Make this behavior configurable per vocabulary or term.
  143. $breadcrumb = array();
  144. while ($parents = taxonomy_get_parents($current->tid)) {
  145. $current = array_shift($parents);
  146. $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  147. }
  148. $breadcrumb[] = l(t('Home'), NULL);
  149. $breadcrumb = array_reverse($breadcrumb);
  150. drupal_set_breadcrumb($breadcrumb);
  151. drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);
  152. if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
  153. $nodes = node_load_multiple($nids);
  154. $build += node_view_multiple($nodes);
  155. $build['pager'] = array(
  156. '#theme' => 'pager',
  157. '#weight' => 5,
  158. );
  159. }
  160. else {
  161. $build['no_content'] = array(
  162. '#prefix' => '<p>',
  163. '#markup' => t('There is currently no content classified with this term.'),
  164. '#suffix' => '</p>',
  165. );
  166. }
  167. return $build;
  168. }
  169. /**
  170. * Callback to get arguments provided by this task handler.
  171. *
  172. * Since this is the term view and there is no UI on the arguments, we
  173. * create dummy arguments that contain the needed data.
  174. */
  175. function page_manager_term_view_get_arguments($task, $subtask_id) {
  176. return array(
  177. array(
  178. 'keyword' => 'term',
  179. 'identifier' => page_manager_term_view_get_type() == 'multiple' ? t('Term(s) being viewed') : t('Term being viewed'),
  180. 'id' => 1,
  181. 'name' => page_manager_term_view_get_type() == 'multiple' ? 'terms' : 'term',
  182. 'settings' => array('input_form' => 'tid', 'breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE)),
  183. 'default' => '404',
  184. ),
  185. array(
  186. 'keyword' => 'depth',
  187. 'identifier' => t('Depth'),
  188. 'id' => 1,
  189. 'name' => 'string',
  190. 'settings' => array(),
  191. ),
  192. );
  193. }
  194. /**
  195. * Callback to get context placeholders provided by this handler.
  196. */
  197. function page_manager_term_view_get_contexts($task, $subtask_id) {
  198. return ctools_context_get_placeholders_from_argument(page_manager_term_view_get_arguments($task, $subtask_id));
  199. }
  200. /**
  201. * Settings page for this item.
  202. */
  203. function page_manager_term_view_settings($form, &$form_state) {
  204. // This passes thru because the setting can also appear on the main Panels
  205. // settings form. If $settings is an array it will just pick up the default.
  206. $settings = isset($form_state->update_values) ? $form_state->update_values : array();
  207. return page_manager_term_view_admin_settings($form, $settings);
  208. }
  209. /**
  210. * Copy form values into the page cache.
  211. */
  212. function page_manager_term_view_settings_submit(&$form, &$form_state) {
  213. $form_state['page']->update_values = $form_state['values'];
  214. }
  215. /**
  216. * Save when the page cache is saved.
  217. */
  218. function page_manager_term_view_save($subtask, $cache) {
  219. if (isset($cache->update_values)) {
  220. variable_set('page_manager_term_view_type', $cache->update_values['page_manager_term_view_type']);
  221. variable_set('page_manager_taxonomy_breadcrumb', $cache->update_values['page_manager_taxonomy_breadcrumb']);
  222. }
  223. }
  224. /**
  225. * Provide a setting to the Panels administrative form.
  226. */
  227. function page_manager_term_view_admin_settings($form, $settings = array()) {
  228. if (empty($settings)) {
  229. $settings = array(
  230. 'page_manager_term_view_type' => page_manager_term_view_get_type(),
  231. 'page_manager_taxonomy_breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE),
  232. );
  233. }
  234. $form['page_manager_term_view_type'] = array(
  235. '#type' => 'radios',
  236. '#title' => t('Allow multiple terms on taxonomy/term/%term'),
  237. '#options' => array('single' => t('Single term'), 'multiple' => t('Multiple terms')),
  238. '#description' => t('By default, Drupal allows multiple terms as an argument by separating them with commas or plus signs. If you set this to single, that feature will be disabled.') . ' ' . t('This feature does not currently work and is disabled.'),
  239. '#default_value' => $settings['page_manager_term_view_type'],
  240. // @todo -- fix this
  241. '#disabled' => TRUE,
  242. );
  243. $form['page_manager_taxonomy_breadcrumb'] = array(
  244. '#title' => t('Inject hierarchy of first term into breadcrumb trail'),
  245. '#type' => 'checkbox',
  246. '#default_value' => $settings['page_manager_taxonomy_breadcrumb'],
  247. '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
  248. );
  249. return $form;
  250. }
  251. /**
  252. * Callback to enable/disable the page from the UI.
  253. */
  254. function page_manager_term_view_enable($cache, $status) {
  255. variable_set('page_manager_term_view_disabled', $status);
  256. // Set a global flag so that the menu routine knows it needs
  257. // to set a message if enabling cannot be done.
  258. if (!$status) {
  259. $GLOBALS['page_manager_enabling_term_view'] = TRUE;
  260. }
  261. }
  262. function page_manager_term_view_get_type() {
  263. // $view_type = variable_get('page_manager_term_view_type', 'multiple');
  264. // Revert to just allowing single.
  265. $view_type = 'single';
  266. return $view_type;
  267. }
  268. /**
  269. * Provide a nice administrative summary of the page so an admin can see at a
  270. * glance what this page does and how it is configured.
  271. */
  272. function page_manager_term_view_admin_summary($task, $subtask) {
  273. $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
  274. $rows[] = array(
  275. array('class' => array('page-summary-label'), 'data' => t('Path')),
  276. array('class' => array('page-summary-data'), 'data' => 'taxonomy/term/%term'),
  277. array('class' => array('page-summary-operation'), 'data' => ''),
  278. );
  279. $rows[] = array(
  280. array('class' => array('page-summary-label'), 'data' => t('Access')),
  281. array('class' => array('page-summary-data'), 'data' => t('This page is publicly accessible.')),
  282. array('class' => array('page-summary-operation'), 'data' => ''),
  283. );
  284. $menu = t('No menu entry');
  285. $rows[] = array(
  286. array('class' => array('page-summary-label'), 'data' => t('Menu')),
  287. array('class' => array('page-summary-data'), 'data' => $menu),
  288. array('class' => array('page-summary-operation'), 'data' => ''),
  289. );
  290. if (page_manager_term_view_get_type() == 'multiple') {
  291. $message = t('Multiple terms may be used, separated by , or +.');
  292. }
  293. else {
  294. $message = t('Only a single term may be used.');
  295. }
  296. $rows[] = array(
  297. array('class' => array('page-summary-label'), 'data' => t('%term')),
  298. array('class' => array('page-summary-data'), 'data' => $message),
  299. array('class' => array('page-summary-operation'), 'data' => ''),
  300. );
  301. if (variable_get('page_manager_taxonomy_breadcrumb', TRUE)) {
  302. $message = t('Breadcrumb trail will contain taxonomy term hierarchy');
  303. }
  304. else {
  305. $message = t('Breadcrumb trail will not contain taxonomy term hiearchy.');
  306. }
  307. $rows[] = array(
  308. array('class' => array('page-summary-label'), 'data' => t('Breadcrumb')),
  309. array('class' => array('page-summary-data'), 'data' => $message),
  310. array('class' => array('page-summary-operation'), 'data' => ''),
  311. );
  312. $output = theme('table', array(), $rows, array('id' => 'page-manager-page-summary'));
  313. return $output;
  314. }
  315. /**
  316. * Callback to determine if a page is accessible.
  317. *
  318. * @param $task
  319. * The task plugin.
  320. * @param $subtask_id
  321. * The subtask id
  322. * @param $contexts
  323. * The contexts loaded for the task.
  324. * @return
  325. * TRUE if the current user can access the page.
  326. */
  327. function page_manager_term_view_access_check($task, $subtask_id, $contexts) {
  328. return user_access('access content');
  329. }