Projet

Général

Profil

Paste
Télécharger (17,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy.views.inc @ 7547bb19

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

    
19
  $data['vocabulary']['moved to'] = 'taxonomy_vocabulary';
20
  $data['taxonomy_vocabulary']['table']['group']  = t('Taxonomy vocabulary');
21

    
22
  $data['taxonomy_vocabulary']['table']['join'] = array(
23
    // vocabulary links to taxonomy_term_data directly via vid.
24
    'taxonomy_term_data' => array(
25
      'left_field' => 'vid',
26
      'field' => 'vid',
27
    ),
28
  );
29

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

    
38
  // vocabulary name
39
  $data['taxonomy_vocabulary']['name'] = array(
40
    'title' => t('Name'), // The item it appears as on the UI,
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
    'title' => t('Machine name'), // The item it appears as on the UI,
53
    'field' => array(
54
      '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.'),
55
      'handler' => 'views_handler_field',
56
      'click sortable' => TRUE,
57
    ),
58
    'filter' => array(
59
      'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
60
      'handler' => 'views_handler_filter_vocabulary_machine_name',
61
    ),
62
    'argument' => array(
63
      'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
64
      'handler' => 'views_handler_argument_vocabulary_machine_name',
65
    ),
66
  );
67
  $data['taxonomy_vocabulary']['vid'] = array(
68
    'title' => t('Vocabulary ID'), // The item it appears as on the UI,
69
    'help' => t('The taxonomy vocabulary ID'),
70
    'field' => array(
71
      'handler' => 'views_handler_field_numeric',
72
      'click sortable' => TRUE,
73
    ),
74
    'argument' => array(
75
      'handler' => 'views_handler_argument_vocabulary_vid',
76
      'name field' => 'name',
77
    ),
78
    'sort' => array(
79
      'handler' => 'views_handler_sort',
80
    ),
81
  );
82
  $data['taxonomy_vocabulary']['description'] = array(
83
    'title' => t('Description'), // The item it appears as on the UI,
84
    'help' => t('The taxonomy vocabulary description'),
85
    'field' => array(
86
      'handler' => 'views_handler_field',
87
    ),
88
  );
89
  $data['taxonomy_vocabulary']['weight'] = array(
90
    'title' => t('Weight'),
91
    'help' => t('The taxonomy vocabulary weight'),
92
    'field' => array(
93
      'handler' => 'views_handler_field_numeric',
94
      'click sortable' => TRUE,
95
    ),
96
    'argument' => array(
97
      'handler' => 'views_handler_argument_numeric',
98
      'name field' => 'weight',
99
    ),
100
    'sort' => array(
101
      'handler' => 'views_handler_sort',
102
    ),
103
    'filter' => array(
104
      'handler' => 'views_handler_filter_numeric',
105
    ),
106
  );
107

    
108
  // ----------------------------------------------------------------------
109
 // taxonomy_term_data table
110

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

    
121

    
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
  // 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
  // raw 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
  // taxonomy 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

    
271
  $data['term_node']['moved to'] = 'taxonomy_index';
272
  $data['taxonomy_index']['table']['group']  = t('Taxonomy term');
273

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

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

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

    
328
  // ----------------------------------------------------------------------
329
  // term_hierarchy table
330

    
331
  $data['taxonomy_term_hierarchy']['table']['group']  = t('Taxonomy term');
332

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

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

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

    
373
  return $data;
374
}
375

    
376
/**
377
 * Implements hook_views_data_alter().
378
 */
379
function taxonomy_views_data_alter(&$data) {
380
  $data['node']['term_node_tid'] = array(
381
    'title' => t('Taxonomy terms on node'),
382
    '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.'),
383
    'relationship' => array(
384
      'handler' => 'views_handler_relationship_node_term_data',
385
      'label' => t('term'),
386
      'base' => 'taxonomy_term_data',
387
    ),
388
    'field' => array(
389
      'title' => t('All taxonomy terms'),
390
      'help' => t('Display all taxonomy terms associated with a node from specified vocabularies.'),
391
      'handler' => 'views_handler_field_term_node_tid',
392
      'no group by' => TRUE,
393
    ),
394
  );
395

    
396
  $data['node']['term_node_tid_depth'] = array(
397
    '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.'),
398
    'real field' => 'nid',
399
    'argument' => array(
400
      'title' => t('Has taxonomy term ID (with depth)'),
401
      'handler' => 'views_handler_argument_term_node_tid_depth',
402
      'accept depth modifier' => TRUE,
403
    ),
404
    'filter' => array(
405
      'title' => t('Has taxonomy terms (with depth)'),
406
      'handler' => 'views_handler_filter_term_node_tid_depth',
407
    ),
408
  );
409

    
410
  $data['node']['term_node_tid_depth_join'] = array(
411
    '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.'),
412
    'real field' => 'nid',
413
    'argument' => array(
414
      'title' => t('Has taxonomy term ID with depth (using joins)'),
415
      'handler' => 'views_handler_argument_term_node_tid_depth_join',
416
      'accept depth modifier' => TRUE,
417
    ),
418
    'filter' => array(
419
      'title' => t('Has taxonomy terms with depth (using joins)'),
420
      'handler' => 'views_handler_filter_term_node_tid_depth_join',
421
    ),
422
  );
423

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

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

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

    
459
  }
460

    
461
  return $data;
462
}
463

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

    
474
    list($label, $all_labels) = field_views_field_label($field['field_name']);
475
    $entity = $entity_info['label'];
476
    if ($entity == t('Node')) {
477
      $entity = t('Content');
478
    }
479

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

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

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

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