Project

General

Profile

Paste
Download (14.3 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / tasks / term_view.inc @ 219d19c4

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 at 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
    elseif (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
  // Assign the term name as the page title.
116
  drupal_set_title($term->name);
117

    
118
  // If there is a menu link to this term, the link becomes the last part
119
  // of the active trail, and the link name becomes the page title.
120
  // Thus, we must explicitly set the page title to be the node title.
121
  $uri = entity_uri('taxonomy_term', $term);
122

    
123
  // Set the term path as the canonical URL to prevent duplicate content.
124
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
125
  // Set the non-aliased path as a default shortlink.
126
  drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
127

    
128
  // Trigger the main.
129
  $build = taxonomy_term_show($term);
130

    
131
  // Load my task plugin.
132
  $task = page_manager_get_task('term_view');
133

    
134
  // Load the term into a context.
135
  ctools_include('context');
136
  ctools_include('context-task-handler');
137
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($term, $depth));
138

    
139
  if (empty($contexts)) {
140
    return MENU_NOT_FOUND;
141
  }
142

    
143
  // Build the full output using the configured CTools plugin.
144
  $output = ctools_context_handler_render($task, '', $contexts, array($term->tid));
145
  if ($output !== FALSE) {
146
    return $output;
147
  }
148

    
149
  // Try loading an override plugin.
150
  foreach (module_implements('page_manager_override') as $module) {
151
    $call = $module . '_page_manager_override';
152
    if (($rc = $call('term_view')) && function_exists($rc)) {
153
      return $rc($term, $depth);
154
    }
155
  }
156

    
157
  // Otherwise, fall back to replicating the output normally generated by
158
  // taxonomy_term_page().
159
  // Build breadcrumb based on the hierarchy of the term.
160
  $current = (object) array(
161
    'tid' => $term->tid,
162
  );
163
  // @todo This overrides any other possible breadcrumb and is a pure hard-coded
164
  //   presumption. Make this behavior configurable per vocabulary or term.
165
  $breadcrumb = array();
166
  while ($parents = taxonomy_get_parents($current->tid)) {
167
    $current = array_shift($parents);
168
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
169
  }
170
  $breadcrumb[] = l(t('Home'), NULL);
171
  $breadcrumb = array_reverse($breadcrumb);
172
  drupal_set_breadcrumb($breadcrumb);
173
  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);
174

    
175
  if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
176
    $nodes = node_load_multiple($nids);
177
    $build += node_view_multiple($nodes);
178
    $build['pager'] = array(
179
      '#theme' => 'pager',
180
      '#weight' => 5,
181
    );
182
  }
183
  else {
184
    $build['no_content'] = array(
185
      '#prefix' => '<p>',
186
      '#markup' => t('There is currently no content classified with this term.'),
187
      '#suffix' => '</p>',
188
    );
189
  }
190
  return $build;
191
}
192

    
193
/**
194
 * Callback to get arguments provided by this task handler.
195
 *
196
 * Since this is the term view and there is no UI on the arguments, we
197
 * create dummy arguments that contain the needed data.
198
 */
199
function page_manager_term_view_get_arguments($task, $subtask_id) {
200
  return array(
201
    array(
202
      'keyword' => 'term',
203
      'identifier' => page_manager_term_view_get_type() == 'multiple' ? t('Term(s) being viewed') : t('Term being viewed'),
204
      'id' => 1,
205
      'name' => page_manager_term_view_get_type() == 'multiple' ? 'terms' : 'term',
206
      'settings' => array('input_form' => 'tid', 'breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE)),
207
      'default' => '404',
208
    ),
209
    array(
210
      'keyword' => 'depth',
211
      'identifier' => t('Depth'),
212
      'id' => 1,
213
      'name' => 'string',
214
      'settings' => array(),
215
    ),
216
  );
217
}
218

    
219
/**
220
 * Callback to get context placeholders provided by this handler.
221
 */
222
function page_manager_term_view_get_contexts($task, $subtask_id) {
223
  return ctools_context_get_placeholders_from_argument(page_manager_term_view_get_arguments($task, $subtask_id));
224
}
225

    
226
/**
227
 * Settings page for this item.
228
 */
229
function page_manager_term_view_settings($form, &$form_state) {
230
  // This passes thru because the setting can also appear on the main Panels
231
  // settings form. If $settings is an array it will just pick up the default.
232
  $settings = isset($form_state->update_values) ? $form_state->update_values : array();
233
  return page_manager_term_view_admin_settings($form, $settings);
234
}
235

    
236
/**
237
 * Copy form values into the page cache.
238
 */
239
function page_manager_term_view_settings_submit(&$form, &$form_state) {
240
  $form_state['page']->update_values = $form_state['values'];
241
}
242

    
243
/**
244
 * Save when the page cache is saved.
245
 */
246
function page_manager_term_view_save($subtask, $cache) {
247
  if (isset($cache->update_values)) {
248
    variable_set('page_manager_term_view_type', $cache->update_values['page_manager_term_view_type']);
249
    variable_set('page_manager_taxonomy_breadcrumb', $cache->update_values['page_manager_taxonomy_breadcrumb']);
250
  }
251
}
252

    
253
/**
254
 * Provide a setting to the Panels administrative form.
255
 */
256
function page_manager_term_view_admin_settings($form, $settings = array()) {
257
  if (empty($settings)) {
258
    $settings = array(
259
      'page_manager_term_view_type' => page_manager_term_view_get_type(),
260
      'page_manager_taxonomy_breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE),
261
    );
262
  }
263

    
264
  $form['page_manager_term_view_type'] = array(
265
    '#type' => 'radios',
266
    '#title' => t('Allow multiple terms on taxonomy/term/%term'),
267
    '#options' => array('single' => t('Single term'), 'multiple' => t('Multiple terms')),
268
    '#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.'),
269
    '#default_value' => $settings['page_manager_term_view_type'],
270
    // @todo -- fix this
271
    '#disabled' => TRUE,
272
  );
273
  $form['page_manager_taxonomy_breadcrumb'] = array(
274
    '#title' => t('Inject hierarchy of first term into breadcrumb trail'),
275
    '#type' => 'checkbox',
276
    '#default_value' => $settings['page_manager_taxonomy_breadcrumb'],
277
    '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'),
278
  );
279

    
280
  return $form;
281
}
282

    
283
/**
284
 * Callback to enable/disable the page from the UI.
285
 */
286
function page_manager_term_view_enable($cache, $status) {
287
  variable_set('page_manager_term_view_disabled', $status);
288

    
289
  // Set a global flag so that the menu routine knows it needs
290
  // to set a message if enabling cannot be done.
291
  if (!$status) {
292
    $GLOBALS['page_manager_enabling_term_view'] = TRUE;
293
  }
294
}
295

    
296
function page_manager_term_view_get_type() {
297
  // Revert to just allowing single.
298
  $view_type = 'single';
299

    
300
  return $view_type;
301
}
302

    
303
/**
304
 * Provide a nice administrative summary of the page so an admin can see at a
305
 * glance what this page does and how it is configured.
306
 */
307
function page_manager_term_view_admin_summary($task, $subtask) {
308
  $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
309

    
310
  $rows[] = array(
311
    array('class' => array('page-summary-label'), 'data' => t('Path')),
312
    array('class' => array('page-summary-data'), 'data' => 'taxonomy/term/%term'),
313
    array('class' => array('page-summary-operation'), 'data' => ''),
314
  );
315

    
316
  $rows[] = array(
317
    array('class' => array('page-summary-label'), 'data' => t('Access')),
318
    array('class' => array('page-summary-data'), 'data' => t('This page is publicly accessible.')),
319
    array('class' => array('page-summary-operation'), 'data' => ''),
320
  );
321

    
322
  $menu = t('No menu entry');
323

    
324
  $rows[] = array(
325
    array('class' => array('page-summary-label'), 'data' => t('Menu')),
326
    array('class' => array('page-summary-data'), 'data' => $menu),
327
    array('class' => array('page-summary-operation'), 'data' => ''),
328
  );
329

    
330
  if (page_manager_term_view_get_type() == 'multiple') {
331
    $message = t('Multiple terms may be used, separated by , or +.');
332
  }
333
  else {
334
    $message = t('Only a single term may be used.');
335
  }
336

    
337
  $rows[] = array(
338
    array('class' => array('page-summary-label'), 'data' => t('%term')),
339
    array('class' => array('page-summary-data'), 'data' => $message),
340
    array('class' => array('page-summary-operation'), 'data' => ''),
341
  );
342

    
343
  if (variable_get('page_manager_taxonomy_breadcrumb', TRUE)) {
344
    $message = t('Breadcrumb trail will contain taxonomy term hierarchy');
345
  }
346
  else {
347
    $message = t('Breadcrumb trail will not contain taxonomy term hiearchy.');
348
  }
349

    
350
  $rows[] = array(
351
    array('class' => array('page-summary-label'), 'data' => t('Breadcrumb')),
352
    array('class' => array('page-summary-data'), 'data' => $message),
353
    array('class' => array('page-summary-operation'), 'data' => ''),
354
  );
355

    
356
  $output = theme('table', array(), $rows, array('id' => 'page-manager-page-summary'));
357
  return $output;
358
}
359

    
360
/**
361
 * Callback to determine if a page is accessible.
362
 *
363
 * @param $task
364
 *   The task plugin.
365
 * @param $subtask_id
366
 *   The subtask id
367
 * @param $contexts
368
 *   The contexts loaded for the task.
369
 *
370
 * @return bool
371
 *   TRUE if the current user can access the page.
372
 */
373
function page_manager_term_view_access_check($task, $subtask_id, $contexts) {
374
  return user_access('access content');
375
}