fixed spelling
[project/section.git] / plugins / tasks / section_view.inc
1 <?php
2
3 /**
4 * @file
5 * Handle the 'section view' override task.
6 *
7 * This plugin overrides section/%section and reroutes it to the page manager, where
8 * a list of tasks can be used to service this request based upon criteria
9 * supplied by access plugins.
10 */
11
12 /**
13 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
14 * more information.
15 */
16 function section_section_view_page_manager_tasks() {
17 return array(
18 // This is a 'page' task and will fall under the page admin UI
19 'task type' => 'page',
20
21 'title' => t('Section template'),
22
23 'admin title' => t('Section template'),
24 'admin description' => t('When enabled, this overrides the default behavior for displaying sections at <em>section/%section</em>.'),
25 'admin path' => 'section/%section',
26
27 // Menu hooks so that we can alter the section/%section menu entry to point to us.
28 'hook menu' => 'section_section_view_menu',
29 'hook menu alter' => 'section_section_view_menu_alter',
30
31 // This is task uses 'context' handlers and must implement these to give the
32 // handler data it needs.
33 'handler type' => 'context',
34 'get arguments' => 'section_section_view_get_arguments',
35 'get context placeholders' => 'section_section_view_get_contexts',
36
37 // Allow this to be enabled or disabled:
38 'disabled' => variable_get('section_section_view_disabled', TRUE),
39 'enable callback' => 'section_section_view_enable',
40 );
41 }
42
43 /**
44 * Callback defined by section_section_view_page_manager_tasks().
45 *
46 * Alter the node view input so that node view comes to us rather than the
47 * normal node view process.
48 */
49 function section_section_view_menu_alter(&$items, $task) {
50 if (variable_get('section_section_view_disabled', TRUE)) {
51 return;
52 }
53
54 // Override the node view handler for our purpose.
55 $callback = $items['section/%section']['page callback'];
56 if ($callback === 'section_page_view' || variable_get('page_manager_override_anyway', FALSE)) {
57 $items['section/%section']['page callback'] = 'section_section_view_page';
58 $items['section/%section']['file path'] = $task['path'];
59 $items['section/%section']['file'] = $task['file'];
60 }
61 else {
62 // automatically disable this task if it cannot be enabled.
63 variable_set('section_section_view_disabled', TRUE);
64 if (!empty($GLOBALS['section_enabling_section_view'])) {
65 drupal_set_message(t('Page manager module is unable to enable section/%section because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error');
66 }
67 }
68 }
69
70 /**
71 * Entry point for our overridden section view.
72 *
73 * This function asks its assigned handlers who, if anyone, would like
74 * to run with it. If no one does, it passes through to sections
75 * section view, which is section_page_view().
76 */
77 function section_section_view_page($section) {
78 // Load my task plugin
79 $task = page_manager_get_task('section_view');
80
81 // Load the node into a context.
82 ctools_include('context');
83 ctools_include('context-task-handler');
84 $contexts = ctools_context_handler_get_task_contexts($task, '', array($section));
85
86 $output = ctools_context_handler_render($task, '', $contexts, array($section->sid));
87 if ($output != FALSE) {
88 return $output;
89 }
90
91 $function = 'section_page_view';
92 foreach (module_implements('page_manager_override') as $module) {
93 $call = $module . '_page_manager_override';
94 if (($rc = $call('section_view')) && function_exists($rc)) {
95 $function = $rc;
96 break;
97 }
98 }
99
100 // Otherwise, fall back.
101 return $function($section);
102 }
103
104 /**
105 * Callback to get arguments provided by this task handler.
106 *
107 * Since this is the section view and there is no UI on the arguments, we
108 * create dummy arguments that contain the needed data.
109 */
110 function section_section_view_get_arguments($task, $subtask_id) {
111 return array(
112 array(
113 'keyword' => 'section',
114 'identifier' => t('Section being viewed'),
115 'id' => 1,
116 'name' => 'entity_id:section',
117 'settings' => array(),
118 ),
119 );
120 }
121
122 /**
123 * Callback to get context placeholders provided by this handler.
124 */
125 function section_section_view_get_contexts($task, $subtask_id) {
126 return ctools_context_get_placeholders_from_argument(section_section_view_get_arguments($task, $subtask_id));
127 }
128
129 /**
130 * Callback to enable/disable the page from the UI.
131 */
132 function section_section_view_enable($cache, $status) {
133 variable_set('section_section_view_disabled', $status);
134
135 // Set a global flag so that the menu routine knows it needs
136 // to set a message if enabling cannot be done.
137 if (!$status) {
138 $GLOBALS['section_enabling_section_view'] = TRUE;
139 }
140 }