comparison sites/all/modules/ctools/page_manager/plugins/tasks/term_view.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Handle the 'term view' override task.
6 *
7 * This plugin overrides term/%term 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 page_manager_term_view_page_manager_tasks() {
17 if (module_exists('taxonomy')) {
18 return array(
19 // This is a 'page' task and will fall under the page admin UI
20 'task type' => 'page',
21
22 'title' => t('Taxonomy term template'),
23 'admin title' => t('Taxonomy term template'),
24 '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.'),
25 'admin path' => 'taxonomy/term/%taxonomy_term',
26 'admin summary' => 'page_manager_term_view_admin_summary',
27
28 // Menu hooks so that we can alter the term/%term menu entry to point to us.
29 'hook menu' => 'page_manager_term_view_menu',
30 'hook menu alter' => 'page_manager_term_view_menu_alter',
31
32 // Provide a setting to the primary settings UI for Panels
33 'admin settings' => 'page_manager_term_view_admin_settings',
34 // Even though we don't have subtasks, this allows us to save our settings.
35 'save subtask callback' => 'page_manager_term_view_save',
36
37 // Callback to add items to the page manager task administration form:
38 'task admin' => 'page_manager_term_view_task_admin',
39
40 // This is task uses 'context' handlers and must implement these to give the
41 // handler data it needs.
42 'handler type' => 'context',
43 'get arguments' => 'page_manager_term_view_get_arguments',
44 'get context placeholders' => 'page_manager_term_view_get_contexts',
45
46 // Allow this to be enabled or disabled:
47 'disabled' => variable_get('page_manager_term_view_disabled', TRUE),
48 'enable callback' => 'page_manager_term_view_enable',
49 'access callback' => 'page_manager_term_view_access_check',
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 // Override the term view handler for our purpose, but only if someone else
75 // has not already done so.
76 if (isset($items['taxonomy/term/%taxonomy_term']) && $items['taxonomy/term/%taxonomy_term']['page callback'] == 'taxonomy_term_page' || variable_get('page_manager_override_anyway', FALSE)) {
77 $items['taxonomy/term/%taxonomy_term']['page callback'] = 'page_manager_term_view_page';
78 $items['taxonomy/term/%taxonomy_term']['file path'] = $task['path'];
79 $items['taxonomy/term/%taxonomy_term']['file'] = $task['file'];
80 }
81 else {
82 // automatically disable this task if it cannot be enabled.
83 variable_set('page_manager_term_view_disabled', TRUE);
84
85 if (isset($items['taxonomy/term/%taxonomy_term']['page callback'])) {
86 $callback = $items['taxonomy/term/%taxonomy_term']['page callback'];
87 }
88 // Because Views changes %taxonomy_term to %views_arg, check to see if that
89 // is why we can't enable:
90 else if (isset($items['taxonomy/term/%views_arg']['page callback'])) {
91 $callback = $items['taxonomy/term/%views_arg']['page callback'];
92 }
93 else {
94 $callback = t('an unknown callback');
95 }
96 if (!empty($GLOBALS['page_manager_enabling_term_view'])) {
97 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');
98 }
99 }
100 }
101
102 /**
103 * Entry point for our overridden term view.
104 *
105 * This function asks its assigned handlers who, if anyone, would like
106 * to run with it. If no one does, it passes through to Drupal core's
107 * term view, which is term_page_view().
108 */
109 function page_manager_term_view_page($term, $depth = NULL) {
110 // Prep the term to be displayed so all of the regular hooks are triggered.
111 // Rather than calling taxonomy_term_page() directly, as it that would
112 // potentially load nodes that were not necessary, execute some of the code
113 // prior to identifying the correct CTools or Page Manager task handler and
114 // only proceed with the rest of the code if necessary.
115
116 // Assign the term name as the page title.
117 drupal_set_title($term->name);
118
119 // If there is a menu link to this term, the link becomes the last part
120 // of the active trail, and the link name becomes the page title.
121 // Thus, we must explicitly set the page title to be the node title.
122 $uri = entity_uri('taxonomy_term', $term);
123
124 // Set the term path as the canonical URL to prevent duplicate content.
125 drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
126 // Set the non-aliased path as a default shortlink.
127 drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
128
129 // Trigger the main
130 $build = taxonomy_term_show($term);
131
132 // Load my task plugin
133 $task = page_manager_get_task('term_view');
134
135 // Load the term into a context.
136 ctools_include('context');
137 ctools_include('context-task-handler');
138 $contexts = ctools_context_handler_get_task_contexts($task, '', array($term, $depth));
139
140 if (empty($contexts)) {
141 return drupal_not_found();
142 }
143
144 // Build the full output using the configured CTools plugin.
145 $output = ctools_context_handler_render($task, '', $contexts, array($term->tid));
146 if ($output !== FALSE) {
147 return $output;
148 }
149
150 // Try loading an override plugin.
151 foreach (module_implements('page_manager_override') as $module) {
152 $call = $module . '_page_manager_override';
153 if (($rc = $call('term_view')) && function_exists($rc)) {
154 return $rc($node);
155 }
156 }
157
158 // Otherwise, fall back to replicating the output normally generated by
159 // taxonomy_term_page().
160
161 // Build breadcrumb based on the hierarchy of the term.
162 $current = (object) array(
163 'tid' => $term->tid,
164 );
165 // @todo This overrides any other possible breadcrumb and is a pure hard-coded
166 // presumption. Make this behavior configurable per vocabulary or term.
167 $breadcrumb = array();
168 while ($parents = taxonomy_get_parents($current->tid)) {
169 $current = array_shift($parents);
170 $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
171 }
172 $breadcrumb[] = l(t('Home'), NULL);
173 $breadcrumb = array_reverse($breadcrumb);
174 drupal_set_breadcrumb($breadcrumb);
175 drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);
176
177 if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
178 $nodes = node_load_multiple($nids);
179 $build += node_view_multiple($nodes);
180 $build['pager'] = array(
181 '#theme' => 'pager',
182 '#weight' => 5,
183 );
184 }
185 else {
186 $build['no_content'] = array(
187 '#prefix' => '<p>',
188 '#markup' => t('There is currently no content classified with this term.'),
189 '#suffix' => '</p>',
190 );
191 }
192 return $build;
193 }
194
195 /**
196 * Callback to get arguments provided by this task handler.
197 *
198 * Since this is the term view and there is no UI on the arguments, we
199 * create dummy arguments that contain the needed data.
200 */
201 function page_manager_term_view_get_arguments($task, $subtask_id) {
202 return array(
203 array(
204 'keyword' => 'term',
205 'identifier' => page_manager_term_view_get_type() == 'multiple' ? t('Term(s) being viewed') : t('Term being viewed'),
206 'id' => 1,
207 'name' => page_manager_term_view_get_type() == 'multiple' ? 'terms' : 'term',
208 'settings' => array('input_form' => 'tid', 'breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE)),
209 'default' => '404',
210 ),
211 array(
212 'keyword' => 'depth',
213 'identifier' => t('Depth'),
214 'id' => 1,
215 'name' => 'string',
216 'settings' => array(),
217 ),
218 );
219 }
220
221 /**
222 * Callback to get context placeholders provided by this handler.
223 */
224 function page_manager_term_view_get_contexts($task, $subtask_id) {
225 return ctools_context_get_placeholders_from_argument(page_manager_term_view_get_arguments($task, $subtask_id));
226 }
227
228 /**
229 * Settings page for this item.
230 */
231 function page_manager_term_view_settings($form, &$form_state) {
232 // This passes thru because the setting can also appear on the main Panels
233 // settings form. If $settings is an array it will just pick up the default.
234 $settings = isset($form_state->update_values) ? $form_state->update_values : array();
235 return page_manager_term_view_admin_settings($form, $settings);
236 }
237
238 /**
239 * Copy form values into the page cache.
240 */
241 function page_manager_term_view_settings_submit(&$form, &$form_state) {
242 $form_state['page']->update_values = $form_state['values'];
243 }
244
245 /**
246 * Save when the page cache is saved.
247 */
248 function page_manager_term_view_save($subtask, $cache) {
249 if (isset($cache->update_values)) {
250 variable_set('page_manager_term_view_type', $cache->update_values['page_manager_term_view_type']);
251 variable_set('page_manager_taxonomy_breadcrumb', $cache->update_values['page_manager_taxonomy_breadcrumb']);
252 }
253 }
254
255 /**
256 * Provide a setting to the Panels administrative form.
257 */
258 function page_manager_term_view_admin_settings($form, $settings = array()) {
259 if (empty($settings)) {
260 $settings = array(
261 'page_manager_term_view_type' => page_manager_term_view_get_type(),
262 'page_manager_taxonomy_breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE),
263 );
264 }
265
266 $form['page_manager_term_view_type'] = array(
267 '#type' => 'radios',
268 '#title' => t('Allow multiple terms on taxonomy/term/%term'),
269 '#options' => array('single' => t('Single term'), 'multiple' => t('Multiple terms')),
270 '#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.'),
271 '#default_value' => $settings['page_manager_term_view_type'],
272 // @todo -- fix this
273 '#disabled' => TRUE,
274 );
275 $form['page_manager_taxonomy_breadcrumb'] = array(
276 '#title' => t('Inject hierarchy of first term into breadcrumb trail'),
277 '#type' => 'checkbox',
278 '#default_value' => $settings['page_manager_taxonomy_breadcrumb'],
279 '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
280 );
281
282 return $form;
283 }
284
285 /**
286 * Callback to enable/disable the page from the UI.
287 */
288 function page_manager_term_view_enable($cache, $status) {
289 variable_set('page_manager_term_view_disabled', $status);
290
291 // Set a global flag so that the menu routine knows it needs
292 // to set a message if enabling cannot be done.
293 if (!$status) {
294 $GLOBALS['page_manager_enabling_term_view'] = TRUE;
295 }
296 }
297
298 function page_manager_term_view_get_type() {
299 // $view_type = variable_get('page_manager_term_view_type', 'multiple');
300 // Revert to just allowing single.
301 $view_type = 'single';
302
303 return $view_type;
304 }
305
306 /**
307 * Provide a nice administrative summary of the page so an admin can see at a
308 * glance what this page does and how it is configured.
309 */
310 function page_manager_term_view_admin_summary($task, $subtask) {
311 $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
312
313 $rows[] = array(
314 array('class' => array('page-summary-label'), 'data' => t('Path')),
315 array('class' => array('page-summary-data'), 'data' => 'taxonomy/term/%term'),
316 array('class' => array('page-summary-operation'), 'data' => ''),
317 );
318
319 $rows[] = array(
320 array('class' => array('page-summary-label'), 'data' => t('Access')),
321 array('class' => array('page-summary-data'), 'data' => t('This page is publicly accessible.')),
322 array('class' => array('page-summary-operation'), 'data' => ''),
323 );
324
325 $menu = t('No menu entry');
326
327 $rows[] = array(
328 array('class' => array('page-summary-label'), 'data' => t('Menu')),
329 array('class' => array('page-summary-data'), 'data' => $menu),
330 array('class' => array('page-summary-operation'), 'data' => ''),
331 );
332
333 if (page_manager_term_view_get_type() == 'multiple') {
334 $message = t('Multiple terms may be used, separated by , or +.');
335 }
336 else {
337 $message = t('Only a single term may be used.');
338 }
339
340 $rows[] = array(
341 array('class' => array('page-summary-label'), 'data' => t('%term')),
342 array('class' => array('page-summary-data'), 'data' => $message),
343 array('class' => array('page-summary-operation'), 'data' => ''),
344 );
345
346 if (variable_get('page_manager_taxonomy_breadcrumb', TRUE)) {
347 $message = t('Breadcrumb trail will contain taxonomy term hierarchy');
348 }
349 else {
350 $message = t('Breadcrumb trail will not contain taxonomy term hiearchy.');
351 }
352
353 $rows[] = array(
354 array('class' => array('page-summary-label'), 'data' => t('Breadcrumb')),
355 array('class' => array('page-summary-data'), 'data' => $message),
356 array('class' => array('page-summary-operation'), 'data' => ''),
357 );
358
359 $output = theme('table', array(), $rows, array('id' => 'page-manager-page-summary'));
360 return $output;
361 }
362
363 /**
364 * Callback to determine if a page is accessible.
365 *
366 * @param $task
367 * The task plugin.
368 * @param $subtask_id
369 * The subtask id
370 * @param $contexts
371 * The contexts loaded for the task.
372 * @return
373 * TRUE if the current user can access the page.
374 */
375 function page_manager_term_view_access_check($task, $subtask_id, $contexts) {
376 return user_access('access content');
377 }