Projet

Général

Profil

Paste
Télécharger (18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy.views.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Provide views data and handlers for taxonomy.module.
6
 *
7
 * @ingroup views_module_handlers
8
 */
9

    
10
/**
11
 * Implements hook_views_data().
12
 */
13
function taxonomy_views_data() {
14
  $data = array();
15

    
16
  // ----------------------------------------------------------------------
17
  // 'taxonomy_vocabulary' table.
18
  $data['vocabulary']['moved to'] = 'taxonomy_vocabulary';
19
  $data['taxonomy_vocabulary']['table']['group']  = t('Taxonomy vocabulary');
20
  $data['taxonomy_vocabulary']['table']['entity type'] = 'taxonomy_vocabulary';
21
  $data['taxonomy_vocabulary']['table']['join'] = array(
22
    // Vocabulary links to taxonomy_term_data directly via vid.
23
    'taxonomy_term_data' => array(
24
      'left_field' => 'vid',
25
      'field' => 'vid',
26
    ),
27
  );
28

    
29
  // Provide a "default relationship" to keep older views from choking.
30
  $data['taxonomy_vocabulary']['table']['default_relationship'] = array(
31
    'node' => array(
32
      'table' => 'node',
33
      'field' => 'term_node_tid',
34
    ),
35
  );
36

    
37
  // Vocabulary name.
38
  $data['taxonomy_vocabulary']['name'] = array(
39
    // The item it appears as on the UI,
40
    'title' => t('Name'),
41
    'field' => array(
42
      'help' => t('Name of the vocabulary a term is a member of. This will be the vocabulary that whichever term the "Taxonomy: Term" field is; and can similarly cause duplicates.'),
43
      'handler' => 'views_handler_field',
44
      'click sortable' => TRUE,
45
    ),
46
    'sort' => array(
47
      'handler' => 'views_handler_sort',
48
      'help' => t('The taxonomy vocabulary name'),
49
    ),
50
  );
51
  $data['taxonomy_vocabulary']['machine_name'] = array(
52
    // The item it appears as on the UI,
53
    'title' => t('Machine name'),
54
    'field' => array(
55
      'help' => t('Machine-Name of the vocabulary a term is a member of. This will be the vocabulary that whichever term the "Taxonomy: Term" field is; and can similarly cause duplicates.'),
56
      'handler' => 'views_handler_field',
57
      'click sortable' => TRUE,
58
    ),
59
    'filter' => array(
60
      'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
61
      'handler' => 'views_handler_filter_vocabulary_machine_name',
62
    ),
63
    'argument' => array(
64
      'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
65
      'handler' => 'views_handler_argument_vocabulary_machine_name',
66
    ),
67
  );
68
  $data['taxonomy_vocabulary']['vid'] = array(
69
    // The item it appears as on the UI,
70
    'title' => t('Vocabulary ID'),
71
    'help' => t('The taxonomy vocabulary ID'),
72
    'field' => array(
73
      'handler' => 'views_handler_field_numeric',
74
      'click sortable' => TRUE,
75
    ),
76
    'argument' => array(
77
      'handler' => 'views_handler_argument_vocabulary_vid',
78
      'name field' => 'name',
79
    ),
80
    'sort' => array(
81
      'handler' => 'views_handler_sort',
82
    ),
83
  );
84
  $data['taxonomy_vocabulary']['description'] = array(
85
    // The item it appears as on the UI,
86
    'title' => t('Description'),
87
    'help' => t('The taxonomy vocabulary description'),
88
    'field' => array(
89
      'handler' => 'views_handler_field',
90
    ),
91
  );
92
  $data['taxonomy_vocabulary']['weight'] = array(
93
    'title' => t('Weight'),
94
    'help' => t('The taxonomy vocabulary weight'),
95
    'field' => array(
96
      'handler' => 'views_handler_field_numeric',
97
      'click sortable' => TRUE,
98
    ),
99
    'argument' => array(
100
      'handler' => 'views_handler_argument_numeric',
101
      'name field' => 'weight',
102
    ),
103
    'sort' => array(
104
      'handler' => 'views_handler_sort',
105
    ),
106
    'filter' => array(
107
      'handler' => 'views_handler_filter_numeric',
108
    ),
109
  );
110

    
111
  // ----------------------------------------------------------------------
112
  // 'taxonomy_term_data' table.
113
  $data['term_data']['moved to'] = 'taxonomy_term_data';
114
  $data['taxonomy_term_data']['table']['group']  = t('Taxonomy term');
115
  $data['taxonomy_term_data']['table']['base'] = array(
116
    'field' => 'tid',
117
    'title' => t('Term'),
118
    'help' => t('Taxonomy terms are attached to nodes.'),
119
    'access query tag' => 'taxonomy_term_access',
120
  );
121
  $data['taxonomy_term_data']['table']['entity type'] = 'taxonomy_term';
122

    
123
  // The term data table.
124
  $data['taxonomy_term_data']['table']['join'] = array(
125
    'taxonomy_vocabulary' => array(
126
      'field' => 'vid',
127
      'left_field' => 'vid',
128
    ),
129
    // This is provided for many_to_one argument.
130
    'taxonomy_index' => array(
131
      'field' => 'tid',
132
      'left_field' => 'tid',
133
    ),
134
  );
135

    
136
  // Provide a "default relationship" to keep older views from choking.
137
  $data['taxonomy_term_data']['table']['default_relationship'] = array(
138
    'node' => array(
139
      'table' => 'node',
140
      'field' => 'term_node_tid',
141
    ),
142
  );
143

    
144
  // Term ID / 'tid' field.
145
  $data['taxonomy_term_data']['tid'] = array(
146
    'title' => t('Term ID'),
147
    'help' => t('The tid of a taxonomy term.'),
148
    'field' => array(
149
      'handler' => 'views_handler_field_numeric',
150
      'click sortable' => TRUE,
151
    ),
152
    'sort' => array(
153
      'handler' => 'views_handler_sort',
154
    ),
155
    'argument' => array(
156
      'handler' => 'views_handler_argument_taxonomy',
157
      'name field' => 'name',
158
      'zero is null' => TRUE,
159
    ),
160
    'filter' => array(
161
      'title' => t('Term'),
162
      'help' => t('Taxonomy term chosen from autocomplete or select widget.'),
163
      'handler' => 'views_handler_filter_term_node_tid',
164
      'hierarchy table' => 'taxonomy_term_hierarchy',
165
      'numeric' => TRUE,
166
    ),
167
  );
168

    
169
  // The raw term 'tid' field.
170
  $data['taxonomy_term_data']['tid_raw'] = array(
171
    'title' => t('Term ID'),
172
    'help' => t('The tid of a taxonomy term.'),
173
    'real field' => 'tid',
174
    'filter' => array(
175
      'handler' => 'views_handler_filter_numeric',
176
      'allow empty' => TRUE,
177
    ),
178
  );
179

    
180
  $data['taxonomy_term_data']['tid_representative'] = array(
181
    'relationship' => array(
182
      'title' => t('Representative node'),
183
      'label'  => t('Representative node'),
184
      'help' => t('Obtains a single representative node for each term, according to a chosen sort criterion.'),
185
      'handler' => 'views_handler_relationship_groupwise_max',
186
      'relationship field' => 'tid',
187
      'outer field' => 'taxonomy_term_data.tid',
188
      'argument table' => 'taxonomy_term_data',
189
      'argument field' => 'tid',
190
      'base'   => 'node',
191
      'field'  => 'nid',
192
    ),
193
  );
194

    
195
  // Term name field.
196
  $data['taxonomy_term_data']['name'] = array(
197
    'title' => t('Name'),
198
    'help' => t('The taxonomy term name.'),
199
    'field' => array(
200
      'handler' => 'views_handler_field_taxonomy',
201
      'click sortable' => TRUE,
202
    ),
203
    'sort' => array(
204
      'handler' => 'views_handler_sort',
205
    ),
206
    'filter' => array(
207
      'handler' => 'views_handler_filter_string',
208
      'help' => t('Taxonomy term name.'),
209
    ),
210
    'argument' => array(
211
      'handler' => 'views_handler_argument_string',
212
      'help' => t('Taxonomy term name.'),
213
      'many to one' => TRUE,
214
      'empty field name' => t('Uncategorized'),
215
    ),
216
  );
217

    
218
  // Term weight.
219
  $data['taxonomy_term_data']['weight'] = array(
220
    'title' => t('Weight'),
221
    'help' => t('The term weight field'),
222
    'field' => array(
223
      'handler' => 'views_handler_field_numeric',
224
      'click sortable' => TRUE,
225
    ),
226
    'sort' => array(
227
      'handler' => 'views_handler_sort',
228
    ),
229
    'filter' => array(
230
      'handler' => 'views_handler_filter_numeric',
231
    ),
232
    'argument' => array(
233
      'handler' => 'views_handler_argument_numeric',
234
    ),
235
  );
236

    
237
  // Term description.
238
  $data['taxonomy_term_data']['description'] = array(
239
    'title' => t('Term description'),
240
    'help' => t('The description associated with a taxonomy term.'),
241
    'field' => array(
242
      'handler' => 'views_handler_field_markup',
243
      'format' => array('field' => 'format'),
244
    ),
245
    'filter' => array(
246
      'handler' => 'views_handler_filter_string',
247
    ),
248
  );
249

    
250
  // Term vocabulary.
251
  $data['taxonomy_term_data']['vid'] = array(
252
    'title' => t('Vocabulary'),
253
    'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
254
    'filter' => array(
255
      'handler' => 'views_handler_filter_vocabulary_vid',
256
    ),
257
  );
258

    
259
  // Link to edit the term.
260
  $data['taxonomy_term_data']['edit_term'] = array(
261
    'field' => array(
262
      'title' => t('Term edit link'),
263
      'help' => t('Provide a simple link to edit the term.'),
264
      'handler' => 'views_handler_field_term_link_edit',
265
    ),
266
  );
267

    
268
  // ----------------------------------------------------------------------
269
  // 'taxonomy_index' table.
270
  $data['term_node']['moved to'] = 'taxonomy_index';
271
  $data['taxonomy_index']['table']['group']  = t('Taxonomy term');
272

    
273
  $data['taxonomy_index']['table']['join'] = array(
274
    'taxonomy_term_data' => array(
275
      // Links directly to taxonomy_term_data via tid.
276
      'left_field' => 'tid',
277
      'field' => 'tid',
278
    ),
279
    'node' => array(
280
      // Links directly to node via nid.
281
      'left_field' => 'nid',
282
      'field' => 'nid',
283
    ),
284
    'taxonomy_term_hierarchy' => array(
285
      'left_field' => 'tid',
286
      'field' => 'tid',
287
    ),
288
  );
289

    
290
  $data['taxonomy_index']['nid'] = array(
291
    'title' => t('Content with term'),
292
    'help' => t('Relate all content tagged with a term.'),
293
    'relationship' => array(
294
      'handler' => 'views_handler_relationship',
295
      'base' => 'node',
296
      'base field' => 'nid',
297
      'label' => t('node'),
298
      'skip base' => 'node',
299
    ),
300
  );
301

    
302
  // @todo Should be moved to a node field since really it's all about nodes.
303
  // Term ID / 'tid' field.
304
  $data['taxonomy_index']['tid'] = array(
305
    'group' => t('Content'),
306
    'title' => t('Has taxonomy term ID'),
307
    'help' => t('Display content if it has the selected taxonomy terms.'),
308
    'argument' => array(
309
      'handler' => 'views_handler_argument_term_node_tid',
310
      'name table' => 'taxonomy_term_data',
311
      'name field' => 'name',
312
      'empty field name' => t('Uncategorized'),
313
      'numeric' => TRUE,
314
      'skip base' => 'taxonomy_term_data',
315
    ),
316
    'filter' => array(
317
      'title' => t('Has taxonomy term'),
318
      'handler' => 'views_handler_filter_term_node_tid',
319
      'hierarchy table' => 'taxonomy_term_hierarchy',
320
      'numeric' => TRUE,
321
      'skip base' => 'taxonomy_term_data',
322
      'allow empty' => TRUE,
323
    ),
324
  );
325

    
326
  // ----------------------------------------------------------------------
327
  // 'term_hierarchy' table.
328
  $data['taxonomy_term_hierarchy']['table']['group']  = t('Taxonomy term');
329

    
330
  $data['term_hierarchy']['moved to'] = 'taxonomy_term_hierarchy';
331
  $data['taxonomy_term_hierarchy']['table']['join'] = array(
332
    'taxonomy_term_hierarchy' => array(
333
      // Links to self through left.parent = right.tid (going down in depth).
334
      'left_field' => 'tid',
335
      'field' => 'parent',
336
    ),
337
    'taxonomy_term_data' => array(
338
      // Links directly to taxonomy_term_data via tid.
339
      'left_field' => 'tid',
340
      'field' => 'tid',
341
    ),
342
  );
343

    
344
  // Provide a "default relationship" to keep older views from choking.
345
  $data['taxonomy_term_hierarchy']['table']['default_relationship'] = array(
346
    'node' => array(
347
      'table' => 'node',
348
      'field' => 'term_node_tid',
349
    ),
350
  );
351

    
352
  $data['taxonomy_term_hierarchy']['parent'] = array(
353
    'title' => t('Parent term'),
354
    'help' => t('The parent term of the term. This can produce duplicate entries if you are using a vocabulary that allows multiple parents.'),
355
    'relationship' => array(
356
      'base' => 'taxonomy_term_data',
357
      'field' => 'parent',
358
      'label' => t('Parent'),
359
    ),
360
    'filter' => array(
361
      'help' => t('Filter the results of "Taxonomy: Term" by the parent pid.'),
362
      'handler' => 'views_handler_filter_numeric',
363
    ),
364
    'argument' => array(
365
      'help' => t('The parent term of the term.'),
366
      'handler' => 'views_handler_argument_taxonomy',
367
    ),
368
  );
369

    
370
  return $data;
371
}
372

    
373
/**
374
 * Implements hook_views_data_alter().
375
 */
376
function taxonomy_views_data_alter(&$data) {
377
  $data['node']['term_node_tid'] = array(
378
    'title' => t('Taxonomy terms on node'),
379
    'help' => t('Relate nodes to taxonomy terms, specifiying which vocabulary or vocabularies to use. This relationship will cause duplicated records if there are multiple terms.'),
380
    'relationship' => array(
381
      'handler' => 'views_handler_relationship_node_term_data',
382
      'label' => t('term'),
383
      'base' => 'taxonomy_term_data',
384
    ),
385
    'field' => array(
386
      'title' => t('All taxonomy terms'),
387
      'help' => t('Display all taxonomy terms associated with a node from specified vocabularies.'),
388
      'handler' => 'views_handler_field_term_node_tid',
389
      'no group by' => TRUE,
390
    ),
391
  );
392

    
393
  $data['node']['term_node_tid_depth'] = array(
394
    'help' => t('Display content if it has the selected taxonomy terms, or children of the selected terms. Due to additional complexity, this has fewer options than the versions without depth.'),
395
    'real field' => 'nid',
396
    'argument' => array(
397
      'title' => t('Has taxonomy term ID (with depth)'),
398
      'handler' => 'views_handler_argument_term_node_tid_depth',
399
      'accept depth modifier' => TRUE,
400
    ),
401
    'filter' => array(
402
      'title' => t('Has taxonomy terms (with depth)'),
403
      'handler' => 'views_handler_filter_term_node_tid_depth',
404
    ),
405
  );
406

    
407
  $data['node']['term_node_tid_depth_join'] = array(
408
    'help' => t('Display content if it has the selected taxonomy terms, or children of the selected terms. Due to additional complexity, this has fewer options than the versions without depth.'),
409
    'real field' => 'nid',
410
    'argument' => array(
411
      'title' => t('Has taxonomy term ID with depth (using joins)'),
412
      'handler' => 'views_handler_argument_term_node_tid_depth_join',
413
      'accept depth modifier' => TRUE,
414
    ),
415
    'filter' => array(
416
      'title' => t('Has taxonomy terms with depth (using joins)'),
417
      'handler' => 'views_handler_filter_term_node_tid_depth_join',
418
    ),
419
  );
420

    
421
  $data['node']['term_node_tid_depth_modifier'] = array(
422
    'title' => t('Has taxonomy term ID depth modifier'),
423
    'help' => t('Allows the "depth" for Taxonomy: Term ID (with depth) to be modified via an additional contextual filter value.'),
424
    'argument' => array(
425
      'handler' => 'views_handler_argument_term_node_tid_depth_modifier',
426
    ),
427
  );
428
}
429

    
430
/**
431
 * Implements hook_field_views_data().
432
 *
433
 * Views integration for taxonomy_term_reference fields. Adds a term
434
 * relationship to the default field data.
435
 *
436
 * @see field_views_field_default_views_data()
437
 */
438
function taxonomy_field_views_data($field) {
439
  $data = field_views_field_default_views_data($field);
440
  foreach ($data as $table_name => $table_data) {
441
    foreach ($table_data as $field_name => $field_data) {
442
      if (isset($field_data['filter']) && $field_name != 'delta') {
443
        $data[$table_name][$field_name]['filter']['handler'] = 'views_handler_filter_term_node_tid';
444
        $data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary'];
445
      }
446
    }
447

    
448
    // Add the relationship only on the tid field.
449
    $data[$table_name][$field['field_name'] . '_tid']['relationship'] = array(
450
      'handler' => 'views_handler_relationship',
451
      'base' => 'taxonomy_term_data',
452
      'base field' => 'tid',
453
      'label' => t('term from !field_name', array('!field_name' => $field['field_name'])),
454
    );
455
  }
456

    
457
  return $data;
458
}
459

    
460
/**
461
 * Implements hook_field_views_data_views_data_alter().
462
 *
463
 * Views integration to provide reverse relationships on term references.
464
 */
465
function taxonomy_field_views_data_views_data_alter(&$data, $field) {
466
  foreach ($field['bundles'] as $entity_type => $bundles) {
467
    $entity_info = entity_get_info($entity_type);
468
    $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
469

    
470
    list($label, $all_labels) = field_views_field_label($field['field_name']);
471
    $entity = $entity_info['label'];
472
    if ($entity == t('Node')) {
473
      $entity = t('Content');
474
    }
475

    
476
    $data['taxonomy_term_data'][$pseudo_field_name]['relationship'] = array(
477
      'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)),
478
      'help' => t('Relate each @entity with a @field set to the term.', array('@entity' => $entity, '@field' => $label)),
479
      'handler' => 'views_handler_relationship_entity_reverse',
480
      'field_name' => $field['field_name'],
481
      'field table' => _field_sql_storage_tablename($field),
482
      'field field' => $field['field_name'] . '_tid',
483
      'base' => $entity_info['base table'],
484
      'base field' => $entity_info['entity keys']['id'],
485
      'label' => t('!field_name', array('!field_name' => $field['field_name'])),
486
      'join_extra' => array(
487
        0 => array(
488
          'field' => 'entity_type',
489
          'value' => $entity_type,
490
        ),
491
        1 => array(
492
          'field' => 'deleted',
493
          'value' => 0,
494
          'numeric' => TRUE,
495
        ),
496
      ),
497
    );
498
  }
499
}
500

    
501
/**
502
 * Implements hook_views_plugins().
503
 */
504
function taxonomy_views_plugins() {
505
  return array(
506
    // This just tells our themes are elsewhere.
507
    'module' => 'views',
508
    'argument validator' => array(
509
      'taxonomy_term' => array(
510
        'title' => t('Taxonomy term'),
511
        'handler' => 'views_plugin_argument_validate_taxonomy_term',
512
        'path' => drupal_get_path('module', 'views') . '/modules/taxonomy',
513
      ),
514
    ),
515
    'argument default' => array(
516
      'taxonomy_tid' => array(
517
        'title' => t('Taxonomy term ID from URL'),
518
        'handler' => 'views_plugin_argument_default_taxonomy_tid',
519
        'path' => drupal_get_path('module', 'views') . '/modules/taxonomy',
520
        'parent' => 'fixed',
521
      ),
522
    ),
523
  );
524
}
525

    
526
/**
527
 * Helper function to set a breadcrumb for taxonomy.
528
 */
529
function views_taxonomy_set_breadcrumb(&$breadcrumb, &$argument) {
530
  if (empty($argument->options['set_breadcrumb'])) {
531
    return;
532
  }
533

    
534
  $args = $argument->view->args;
535
  $parents = taxonomy_get_parents_all($argument->argument);
536
  foreach (array_reverse($parents) as $parent) {
537
    // Unfortunately parents includes the current argument. Skip.
538
    if ($parent->tid == $argument->argument) {
539
      continue;
540
    }
541
    if (!empty($argument->options['use_taxonomy_term_path'])) {
542
      $path = taxonomy_term_uri($parent);
543
      $path = $path['path'];
544
    }
545
    else {
546
      $args[$argument->position] = $parent->tid;
547
      $path = $argument->view->get_url($args);
548
    }
549
    $breadcrumb[$path] = check_plain($parent->name);
550
  }
551
}