taxonomy.views.inc 12.5 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php

/**
 * @file
 * Provide views data and handlers for taxonomy.module.
 *
 * @ingroup views_module_handlers
 */

/**
 * Implements hook_views_data().
 */
function taxonomy_views_data() {
  $data = array();

 // taxonomy_term_data table

  $data['taxonomy_term_data']['table']['group']  = t('Taxonomy term');
  $data['taxonomy_term_data']['table']['base'] = array(
    'field' => 'tid',
    'title' => t('Term'),
    'help' => t('Taxonomy terms are attached to nodes.'),
    'access query tag' => 'term_access',
  );
  $data['taxonomy_term_data']['table']['entity type'] = 'taxonomy_term';
26 27
  $data['taxonomy_term_data']['table']['wizard_id'] = 'taxonomy_term';

merlinofchaos's avatar
merlinofchaos committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

  // The term data table
  $data['taxonomy_term_data']['table']['join'] = array(
    // This is provided for many_to_one argument
    'taxonomy_index' => array(
      'field' => 'tid',
      'left_field' => 'tid',
    ),
  );

  // tid field
  $data['taxonomy_term_data']['tid'] = array(
    'title' => t('Term ID'),
    'help' => t('The tid of a taxonomy term.'),
    'field' => array(
aspilicious's avatar
aspilicious committed
43
      'id' => 'numeric',
merlinofchaos's avatar
merlinofchaos committed
44 45
    ),
    'sort' => array(
aspilicious's avatar
aspilicious committed
46
      'id' => 'standard',
merlinofchaos's avatar
merlinofchaos committed
47 48
    ),
    'argument' => array(
aspilicious's avatar
aspilicious committed
49
      'id' => 'taxonomy',
merlinofchaos's avatar
merlinofchaos committed
50 51 52 53 54 55
      'name field' => 'name',
      'zero is null' => TRUE,
    ),
    'filter' => array(
      'title' => t('Term'),
      'help' => t('Taxonomy term chosen from autocomplete or select widget.'),
aspilicious's avatar
aspilicious committed
56
      'id' => 'taxonomy_index_tid',
merlinofchaos's avatar
merlinofchaos committed
57 58 59 60 61
      'hierarchy table' => 'taxonomy_term_hierarchy',
      'numeric' => TRUE,
    ),
  );

dawehner's avatar
dawehner committed
62 63 64 65 66 67
  // raw tid field
  $data['taxonomy_term_data']['tid_raw'] = array(
    'title' => t('Term ID'),
    'help' => t('The tid of a taxonomy term.'),
    'real field' => 'tid',
    'filter' => array(
aspilicious's avatar
aspilicious committed
68
      'id' => 'numeric',
dawehner's avatar
dawehner committed
69 70 71 72
      'allow empty' => TRUE,
    ),
  );

merlinofchaos's avatar
merlinofchaos committed
73 74 75 76 77
  $data['taxonomy_term_data']['tid_representative'] = array(
    'relationship' => array(
      'title' => t('Representative node'),
      'label'  => t('Representative node'),
      'help' => t('Obtains a single representative node for each term, according to a chosen sort criterion.'),
aspilicious's avatar
aspilicious committed
78
      'id' => 'groupwise_max',
merlinofchaos's avatar
merlinofchaos committed
79 80 81 82 83 84
      'relationship field' => 'tid',
      'outer field' => 'taxonomy_term_data.tid',
      'argument table' => 'taxonomy_term_data',
      'argument field' =>  'tid',
      'base'   => 'node',
      'field'  => 'nid',
85
      'relationship' => 'node:term_node_tid'
merlinofchaos's avatar
merlinofchaos committed
86 87 88 89 90 91 92 93
    ),
  );

  // Term name field
  $data['taxonomy_term_data']['name'] = array(
    'title' => t('Name'),
    'help' => t('The taxonomy term name.'),
    'field' => array(
aspilicious's avatar
aspilicious committed
94
      'id' => 'taxonomy',
merlinofchaos's avatar
merlinofchaos committed
95 96
    ),
    'sort' => array(
aspilicious's avatar
aspilicious committed
97
      'id' => 'standard',
merlinofchaos's avatar
merlinofchaos committed
98 99
    ),
    'filter' => array(
aspilicious's avatar
aspilicious committed
100
      'id' => 'string',
merlinofchaos's avatar
merlinofchaos committed
101 102 103
      'help' => t('Taxonomy term name.'),
    ),
    'argument' => array(
aspilicious's avatar
aspilicious committed
104
      'id' => 'string',
merlinofchaos's avatar
merlinofchaos committed
105 106 107 108 109 110 111 112 113 114 115
      'help' => t('Taxonomy term name.'),
      'many to one' => TRUE,
      'empty field name' => t('Uncategorized'),
    ),
  );

  // taxonomy weight
  $data['taxonomy_term_data']['weight'] = array(
    'title' => t('Weight'),
    'help' => t('The term weight field'),
    'field' => array(
aspilicious's avatar
aspilicious committed
116
      'id' => 'numeric',
merlinofchaos's avatar
merlinofchaos committed
117 118
    ),
    'sort' => array(
aspilicious's avatar
aspilicious committed
119
      'id' => 'standard',
merlinofchaos's avatar
merlinofchaos committed
120 121
    ),
    'filter' => array(
aspilicious's avatar
aspilicious committed
122
      'id' => 'numeric',
merlinofchaos's avatar
merlinofchaos committed
123
    ),
124
    'argument' => array(
aspilicious's avatar
aspilicious committed
125
      'id' => 'numeric',
126
    ),
merlinofchaos's avatar
merlinofchaos committed
127 128 129 130 131 132 133
  );

  // Term description
  $data['taxonomy_term_data']['description'] = array(
    'title' => t('Term description'),
    'help' => t('The description associated with a taxonomy term.'),
    'field' => array(
aspilicious's avatar
aspilicious committed
134
      'id' => 'markup',
merlinofchaos's avatar
merlinofchaos committed
135
      'format' => array('field' => 'format'),
136
      'click sortable' => FALSE,
merlinofchaos's avatar
merlinofchaos committed
137 138
    ),
    'filter' => array(
aspilicious's avatar
aspilicious committed
139
      'id' => 'string',
merlinofchaos's avatar
merlinofchaos committed
140 141 142 143 144 145 146 147
    ),
  );

  // Term vocabulary
  $data['taxonomy_term_data']['vid'] = array(
    'title' => t('Vocabulary'),
    'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
    'filter' => array(
148
      'id' => 'bundle',
merlinofchaos's avatar
merlinofchaos committed
149 150 151
    ),
  );

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  $data['taxonomy_term_data']['langcode'] = array(
    'title' => t('Language'), // The item it appears as on the UI,
    'help' => t('Language of the term'),
    'field' => array(
      'id' => 'taxonomy_term_language',
    ),
    'sort' => array(
      'id' => 'standard',
    ),
    'filter' => array(
      'id' => 'language',
    ),
    'argument' => array(
      'id' => 'language',
    ),
  );

merlinofchaos's avatar
merlinofchaos committed
169 170 171 172 173
  // Link to edit the term
  $data['taxonomy_term_data']['edit_term'] = array(
    'field' => array(
      'title' => t('Term edit link'),
      'help' => t('Provide a simple link to edit the term.'),
aspilicious's avatar
aspilicious committed
174
      'id' => 'term_link_edit',
175
      'click sortable' => FALSE,
merlinofchaos's avatar
merlinofchaos committed
176 177 178
    ),
  );

179
  // Entity translation field.
180
  if (Drupal::moduleHandler()->moduleExists('translation_entity')) {
181 182 183 184 185 186 187 188 189
    $data['taxonomy_term_data']['translation_link'] = array(
      'title' => t('Translation link'),
      'help' => t('Provide a link to the translations overview for taxonomy terms.'),
      'field' => array(
        'id' => 'translation_entity_link',
      ),
    );
  }

merlinofchaos's avatar
merlinofchaos committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  // taxonomy_index table

  $data['taxonomy_index']['table']['group']  = t('Taxonomy term');

  $data['taxonomy_index']['table']['join'] = array(
    'taxonomy_term_data' => array(
      // links directly to taxonomy_term_data via tid
      'left_field' => 'tid',
      'field' => 'tid',
    ),
    'node' => array(
      // links directly to node via nid
      'left_field' => 'nid',
      'field' => 'nid',
    ),
    'taxonomy_term_hierarchy' => array(
      'left_field' => 'tid',
      'field' => 'tid',
    ),
  );

  $data['taxonomy_index']['nid'] = array(
    'title' => t('Content with term'),
    'help' => t('Relate all content tagged with a term.'),
    'relationship' => array(
aspilicious's avatar
aspilicious committed
215
      'id' => 'standard',
merlinofchaos's avatar
merlinofchaos committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      'base' => 'node',
      'base field' => 'nid',
      'label' => t('node'),
      'skip base' => 'node',
    ),
  );

  // @todo This stuff needs to move to a node field since
  // really it's all about nodes.
  // tid field
  $data['taxonomy_index']['tid'] = array(
    'group' => t('Content'),
    'title' => t('Has taxonomy term ID'),
    'help' => t('Display content if it has the selected taxonomy terms.'),
    'argument' => array(
aspilicious's avatar
aspilicious committed
231
      'id' => 'taxonomy_index_tid',
merlinofchaos's avatar
merlinofchaos committed
232 233 234 235 236 237 238 239
      'name table' => 'taxonomy_term_data',
      'name field' => 'name',
      'empty field name' => t('Uncategorized'),
      'numeric' => TRUE,
      'skip base' => 'taxonomy_term_data',
    ),
    'filter' => array(
      'title' => t('Has taxonomy term'),
aspilicious's avatar
aspilicious committed
240
      'id' => 'taxonomy_index_tid',
merlinofchaos's avatar
merlinofchaos committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      'hierarchy table' => 'taxonomy_term_hierarchy',
      'numeric' => TRUE,
      'skip base' => 'taxonomy_term_data',
      'allow empty' => TRUE,
    ),
  );

  // term_hierarchy table

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

  $data['taxonomy_term_hierarchy']['table']['join'] = array(
    'taxonomy_term_hierarchy' => array(
      // links to self through left.parent = right.tid (going down in depth)
      'left_field' => 'tid',
      'field' => 'parent',
    ),
    'taxonomy_term_data' => array(
      // links directly to taxonomy_term_data via tid
      'left_field' => 'tid',
      'field' => 'tid',
    ),
  );

  $data['taxonomy_term_hierarchy']['parent'] = array(
    'title' => t('Parent term'),
    'help' => t('The parent term of the term. This can produce duplicate entries if you are using a vocabulary that allows multiple parents.'),
    'relationship' => array(
      'base' => 'taxonomy_term_data',
      'field' => 'parent',
      'label' => t('Parent'),
    ),
273 274
    'filter' => array(
      'help' => t('Filter the results of "Taxonomy: Term" by the parent pid.'),
275
      'id' => 'numeric',
276
    ),
merlinofchaos's avatar
merlinofchaos committed
277 278
    'argument' => array(
      'help' => t('The parent term of the term.'),
aspilicious's avatar
aspilicious committed
279
      'id' => 'taxonomy',
merlinofchaos's avatar
merlinofchaos committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293
    ),
  );

  return $data;
}

/**
 * Implements hook_views_data_alter().
 */
function taxonomy_views_data_alter(&$data) {
  $data['node']['term_node_tid'] = array(
    'title' => t('Taxonomy terms on node'),
    '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.'),
    'relationship' => array(
aspilicious's avatar
aspilicious committed
294
      'id' => 'node_term_data',
merlinofchaos's avatar
merlinofchaos committed
295 296 297 298 299 300
      'label' => t('term'),
      'base' => 'taxonomy_term_data',
    ),
    'field' => array(
      'title' => t('All taxonomy terms'),
      'help' => t('Display all taxonomy terms associated with a node from specified vocabularies.'),
aspilicious's avatar
aspilicious committed
301
      'id' => 'taxonomy_index_tid',
merlinofchaos's avatar
merlinofchaos committed
302
      'no group by' => TRUE,
303
      'click sortable' => FALSE,
merlinofchaos's avatar
merlinofchaos committed
304 305 306 307 308 309 310 311
    ),
  );

  $data['node']['term_node_tid_depth'] = array(
    '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.'),
    'real field' => 'nid',
    'argument' => array(
      'title' => t('Has taxonomy term ID (with depth)'),
aspilicious's avatar
aspilicious committed
312
      'id' => 'taxonomy_index_tid_depth',
merlinofchaos's avatar
merlinofchaos committed
313 314 315 316
      'accept depth modifier' => TRUE,
    ),
    'filter' => array(
      'title' => t('Has taxonomy terms (with depth)'),
aspilicious's avatar
aspilicious committed
317
      'id' => 'taxonomy_index_tid_depth',
merlinofchaos's avatar
merlinofchaos committed
318 319 320 321 322 323 324
    ),
  );

  $data['node']['term_node_tid_depth_modifier'] = array(
    'title' => t('Has taxonomy term ID depth modifier'),
    'help' => t('Allows the "depth" for Taxonomy: Term ID (with depth) to be modified via an additional contextual filter value.'),
    'argument' => array(
aspilicious's avatar
aspilicious committed
325
      'id' => 'taxonomy_index_tid_depth_modifier',
merlinofchaos's avatar
merlinofchaos committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    ),
  );
}

/**
 * Implements hook_field_views_data().
 *
 * Views integration for taxonomy_term_reference fields. Adds a term relationship to the default
 * field data.
 *
 * @see field_views_field_default_views_data()
 */
function taxonomy_field_views_data($field) {
  $data = field_views_field_default_views_data($field);
  foreach ($data as $table_name => $table_data) {
    foreach ($table_data as $field_name => $field_data) {
      if (isset($field_data['filter']) && $field_name != 'delta') {
aspilicious's avatar
aspilicious committed
343
        $data[$table_name][$field_name]['filter']['id'] = 'taxonomy_index_tid';
merlinofchaos's avatar
merlinofchaos committed
344 345 346 347 348
        $data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary'];
      }
    }

    // Add the relationship only on the tid field.
349
    $data[$table_name][$field['field_name'] . '_target_id']['relationship'] = array(
aspilicious's avatar
aspilicious committed
350
      'id' => 'standard',
merlinofchaos's avatar
merlinofchaos committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
      'base' => 'taxonomy_term_data',
      'base field' => 'tid',
      'label' => t('term from !field_name', array('!field_name' => $field['field_name'])),
    );

  }

  return $data;
}

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

    list($label, $all_labels) = field_views_field_label($field['field_name']);
    $entity = $entity_info['label'];
    if ($entity == t('Node')) {
      $entity = t('Content');
    }

    $data['taxonomy_term_data'][$pseudo_field_name]['relationship'] = array(
      'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)),
      'help' => t('Relate each @entity with a @field set to the term.', array('@entity' => $entity, '@field' => $label)),
aspilicious's avatar
aspilicious committed
380
      'id' => 'entity_reverse',
merlinofchaos's avatar
merlinofchaos committed
381 382
      'field_name' => $field['field_name'],
      'field table' => _field_sql_storage_tablename($field),
383
      'field field' => $field['field_name'] . '_target_id',
384 385
      'base' => $entity_info['base_table'],
      'base field' => $entity_info['entity_keys']['id'],
merlinofchaos's avatar
merlinofchaos committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
      'label' => t('!field_name', array('!field_name' => $field['field_name'])),
      'join_extra' => array(
        0 => array(
          'field' => 'entity_type',
          'value' => $entity_type,
        ),
        1 => array(
          'field' => 'deleted',
          'value' => 0,
          'numeric' => TRUE,
        ),
      ),
    );
  }
}

/**
 * Helper function to set a breadcrumb for taxonomy.
 */
function views_taxonomy_set_breadcrumb(&$breadcrumb, &$argument) {
  if (empty($argument->options['set_breadcrumb'])) {
    return;
  }

  $args = $argument->view->args;
  $parents = taxonomy_get_parents_all($argument->argument);
  foreach (array_reverse($parents) as $parent) {
    // Unfortunately parents includes the current argument. Skip.
414
    if ($parent->id() == $argument->argument) {
merlinofchaos's avatar
merlinofchaos committed
415 416 417
      continue;
    }
    if (!empty($argument->options['use_taxonomy_term_path'])) {
418
      $path = $parent->uri();
merlinofchaos's avatar
merlinofchaos committed
419 420 421
      $path = $path['path'];
    }
    else {
422
      $args[$argument->position] = $parent->id();
423
      $path = $argument->view->getUrl($args);
merlinofchaos's avatar
merlinofchaos committed
424
    }
425
    $breadcrumb[$path] = check_plain($parent->label());
merlinofchaos's avatar
merlinofchaos committed
426 427
  }
}