d21475a624e698d6795a7be24e648190d442c721
[project/ctools.git] / page_manager / plugins / tasks / term_view.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Handle the 'term view' override task.
7 *
8 * This plugin overrides term/%term and reroutes it to the page manager, where
9 * a list of tasks can be used to service this request based upon criteria
10 * supplied by access plugins.
11 */
12
13 /**
14 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
15 * more information.
16 */
17 function page_manager_term_view_page_manager_tasks() {
18 if (module_exists('taxonomy')) {
19 return array(
20 // This is a 'page' task and will fall under the page admin UI
21 'task type' => 'page',
22
23 'title' => t('Taxonomy term template'),
24 'admin title' => t('Taxonomy term template'),
25 '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.'),
26 'admin path' => 'taxonomy/term/%term',
27 'admin summary' => 'page_manager_term_view_admin_summary',
28
29 // Menu hooks so that we can alter the term/%term menu entry to point to us.
30 'hook menu' => 'page_manager_term_view_menu',
31 'hook menu alter' => 'page_manager_term_view_menu_alter',
32
33 // Provide a setting to the primary settings UI for Panels
34 'admin settings' => 'page_manager_term_view_admin_settings',
35 // Even though we don't have subtasks, this allows us to save our settings.
36 'save subtask callback' => 'page_manager_term_view_save',
37
38 // Callback to add items to the page manager task administration form:
39 'task admin' => 'page_manager_term_view_task_admin',
40
41 // This is task uses 'context' handlers and must implement these to give the
42 // handler data it needs.
43 'handler type' => 'context',
44 'get arguments' => 'page_manager_term_view_get_arguments',
45 'get context placeholders' => 'page_manager_term_view_get_contexts',
46
47 // Allow this to be enabled or disabled:
48 'disabled' => variable_get('page_manager_term_view_disabled', TRUE),
49 'enable callback' => 'page_manager_term_view_enable',
50
51 // Allow additional operations
52 'operations' => array(
53 'settings' => array(
54 'title' => t('Settings'),
55 'description' => t('Edit name, path and other basic settings for the page.'),
56 'form' => 'page_manager_term_view_settings',
57 ),
58 ),
59 );
60 }
61 }
62
63 /**
64 * Callback defined by page_manager_term_view_page_manager_tasks().
65 *
66 * Alter the term view input so that term view comes to us rather than the
67 * normal term view process.
68 */
69 function page_manager_term_view_menu_alter(&$items, $task) {
70 if (variable_get('page_manager_term_view_disabled', TRUE)) {
71 return;
72 }
73
74 // @todo --fix.
75 variable_set('page_manager_term_view_disabled', TRUE);
76 drupal_set_message(t('Due to changes in taxonomy, the taxonomy integration with Page Manager is currently not implemented. See <a href="http://drupal.org/node/1016510">.', array('%callback' => $items['taxonomy/term/%taxonomy_term']['page callback'])), 'error');
77 return;
78
79 // Override the term view handler for our purpose, but only if someone else
80 // has not already done so.
81 if (isset($items['taxonomy/term/%taxonomy_term']) && $items['taxonomy/term/%taxonomy_term']['page callback'] == 'taxonomy_term_page' || variable_get('page_manager_override_anyway', FALSE)) {
82 $items['taxonomy/term/%taxonomy_term']['page callback'] = 'page_manager_term_view_page';
83 $items['taxonomy/term/%taxonomy_term']['file path'] = $task['path'];
84 $items['taxonomy/term/%taxonomy_term']['file'] = $task['file'];
85 }
86 else {
87 // automatically disable this task if it cannot be enabled.
88 variable_set('page_manager_term_view_disabled', TRUE);
89 if (!empty($GLOBALS['page_manager_enabling_term_view'])) {
90 drupal_set_message(t('Page manager module is unable to enable taxonomy/term/%term because some other module already has overridden with %callback.', array('%callback' => $items['taxonomy/term/%taxonomy_term']['page callback'])), 'error');
91 }
92 }
93 }
94
95 /**
96 * Entry point for our overridden term view.
97 *
98 * This function asks its assigned handlers who, if anyone, would like
99 * to run with it. If no one does, it passes through to Drupal core's
100 * term view, which is term_page_view().
101 */
102 function page_manager_term_view_page($term) {
103 // While we ordinarily should never actually get feeds through here,
104 // just in case
105 if ($op != 'feed') {
106 // Load my task plugin
107 $task = page_manager_get_task('term_view');
108
109 // Load the term into a context.
110 ctools_include('context');
111 ctools_include('context-task-handler');
112 $contexts = ctools_context_handler_get_task_contexts($task, '', array($terms, $depth));
113
114 if (empty($contexts)) {
115 return drupal_not_found();
116 }
117
118 // Add a fake tab for 'View' so that edit tabs can be added.
119 if (user_access('administer page manager')) {
120 ctools_include('menu');
121 ctools_menu_add_tab(array(
122 'title' => t('View'),
123 'href' => $_GET['q'],
124 'type' => MENU_DEFAULT_LOCAL_TASK,
125 'weight' => -10,
126 ));
127 }
128
129 $output = ctools_context_handler_render($task, '', $contexts, array($terms, $depth, $op));
130 if ($output !== FALSE) {
131 return $output;
132 }
133 }
134
135 // Otherwise, fall back.
136 module_load_include('inc', 'taxonomy', 'taxonomy.pages');
137 return taxonomy_term_page($terms, $depth, $op);
138 }
139
140 /**
141 * Callback to get arguments provided by this task handler.
142 *
143 * Since this is the term view and there is no UI on the arguments, we
144 * create dummy arguments that contain the needed data.
145 */
146 function page_manager_term_view_get_arguments($task, $subtask_id) {
147 return array(
148 array(
149 'keyword' => 'term',
150 'identifier' => variable_get('page_manager_term_view_type', 'multiple') == 'multiple' ? t('Term(s) being viewed') : t('Term being viewed'),
151 'id' => 1,
152 'name' => variable_get('page_manager_term_view_type', 'multiple') == 'multiple' ? 'terms' : 'term',
153 'settings' => array('input_form' => 'tid', 'breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE)),
154 'default' => '404',
155 ),
156 array(
157 'keyword' => 'depth',
158 'identifier' => t('Depth'),
159 'id' => 1,
160 'name' => 'string',
161 'settings' => array(),
162 ),
163 );
164 }
165
166 /**
167 * Callback to get context placeholders provided by this handler.
168 */
169 function page_manager_term_view_get_contexts($task, $subtask_id) {
170 return ctools_context_get_placeholders_from_argument(page_manager_term_view_get_arguments($task, $subtask_id));
171 }
172
173 /**
174 * Settings page for this item.
175 */
176 function page_manager_term_view_settings(&$form, &$form_state) {
177 // This passes thru because the setting can also appear on the main Panels
178 // settings form. If $settings is an array it will just pick up the default.
179 $settings = isset($form_state->update_values) ? $form_state->update_values : array();
180 page_manager_term_view_admin_settings($form, $settings);
181 }
182
183 /**
184 * Copy form values into the page cache.
185 */
186 function page_manager_term_view_settings_submit(&$form, &$form_state) {
187 $form_state['page']->update_values = $form_state['values'];
188 }
189
190 /**
191 * Save when the page cache is saved.
192 */
193 function page_manager_term_view_save($subtask, $cache) {
194 if (isset($cache->update_values)) {
195 variable_set('page_manager_term_view_type', $cache->update_values['page_manager_term_view_type']);
196 variable_set('page_manager_taxonomy_breadcrumb', $cache->update_values['page_manager_taxonomy_breadcrumb']);
197 }
198 }
199
200 /**
201 * Provide a setting to the Panels administrative form.
202 */
203 function page_manager_term_view_admin_settings(&$form, $settings = array()) {
204 if (empty($settings)) {
205 $settings = array(
206 'page_manager_term_view_type' => variable_get('page_manager_term_view_type', 'multiple'),
207 'page_manager_taxonomy_breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE),
208 );
209 }
210
211 $form['page_manager_term_view_type'] = array(
212 '#type' => 'radios',
213 '#title' => t('Allow multiple terms on taxonomy/term/%term'),
214 '#options' => array('single' => t('Single term'), 'multiple' => t('Multiple terms')),
215 '#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.'),
216 '#default_value' => $settings['page_manager_term_view_type'],
217 );
218 $form['page_manager_taxonomy_breadcrumb'] = array(
219 '#title' => t('Inject hierarchy of first term into breadcrumb trail'),
220 '#type' => 'checkbox',
221 '#default_value' => $settings['page_manager_taxonomy_breadcrumb'],
222 '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
223 );
224 }
225
226 /**
227 * Callback to enable/disable the page from the UI.
228 */
229 function page_manager_term_view_enable($cache, $status) {
230 variable_set('page_manager_term_view_disabled', $status);
231
232 // Set a global flag so that the menu routine knows it needs
233 // to set a message if enabling cannot be done.
234 if (!$status) {
235 $GLOBALS['page_manager_enabling_term_view'] = TRUE;
236 }
237 }
238
239 /**
240 * Provide a nice administrative summary of the page so an admin can see at a
241 * glance what this page does and how it is configured.
242 */
243 function page_manager_term_view_admin_summary($task, $subtask) {
244 $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
245
246 $rows[] = array(
247 array('class' => array('page-summary-label'), 'data' => t('Path')),
248 array('class' => array('page-summary-data'), 'data' => 'taxonomy/term/%term'),
249 array('class' => array('page-summary-operation'), 'data' => ''),
250 );
251
252 $rows[] = array(
253 array('class' => array('page-summary-label'), 'data' => t('Access')),
254 array('class' => array('page-summary-data'), 'data' => t('This page is publicly accessible.')),
255 array('class' => array('page-summary-operation'), 'data' => ''),
256 );
257
258 $menu = t('No menu entry');
259
260 $rows[] = array(
261 array('class' => array('page-summary-label'), 'data' => t('Menu')),
262 array('class' => array('page-summary-data'), 'data' => $menu),
263 array('class' => array('page-summary-operation'), 'data' => ''),
264 );
265
266 if (variable_get('page_manager_term_view_type', 'multiple') == 'multiple') {
267 $message = t('Multiple terms may be used, separated by , or +.');
268 }
269 else {
270 $message = t('Only a single term may be used.');
271 }
272
273 $rows[] = array(
274 array('class' => array('page-summary-label'), 'data' => t('%term')),
275 array('class' => array('page-summary-data'), 'data' => $message),
276 array('class' => array('page-summary-operation'), 'data' => ''),
277 );
278
279 if (variable_get('page_manager_taxonomy_breadcrumb', TRUE)) {
280 $message = t('Breadcrumb trail will contain taxonomy term hierarchy');
281 }
282 else {
283 $message = t('Breadcrumb trail will not contain taxonomy term hiearchy.');
284 }
285
286 $rows[] = array(
287 array('class' => array('page-summary-label'), 'data' => t('Breadcrumb')),
288 array('class' => array('page-summary-data'), 'data' => $message),
289 array('class' => array('page-summary-operation'), 'data' => ''),
290 );
291
292 $output = theme('table', array(), $rows, array('id' => 'page-manager-page-summary'));
293 return $output;
294 }