• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

modules/taxonomy.views.inc

Go to the documentation of this file.
00001 <?php
00017 function taxonomy_views_data() {
00018   $data = array();
00019 
00020   // ----------------------------------------------------------------------
00021   // taxonomy_vocabulary table
00022 
00023   $data['vocabulary']['moved to'] = 'taxonomy_vocabulary';
00024   $data['taxonomy_vocabulary']['table']['group']  = t('Taxonomy vocabulary');
00025 
00026   $data['taxonomy_vocabulary']['table']['join'] = array(
00027     // vocabulary links to taxonomy_term_data directly via vid.
00028     'taxonomy_term_data' => array(
00029       'left_field' => 'vid',
00030       'field' => 'vid',
00031     ),
00032   );
00033 
00034   // Provide a "default relationship" to keep older views from choking.
00035   $data['taxonomy_vocabulary']['table']['default_relationship'] = array(
00036     'node' => array(
00037       'table' => 'node',
00038       'field' => 'term_node_tid',
00039     ),
00040   );
00041 
00042   // vocabulary name
00043   $data['taxonomy_vocabulary']['name'] = array(
00044     'title' => t('Name'), // The item it appears as on the UI,
00045     'field' => array(
00046       '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.'),
00047       'handler' => 'views_handler_field',
00048       'click sortable' => TRUE,
00049     ),
00050     'sort' => array(
00051       'handler' => 'views_handler_sort',
00052       'help' => t('The taxonomy vocabulary name'),
00053     ),
00054   );
00055   $data['taxonomy_vocabulary']['machine_name'] = array(
00056     'title' => t('Machine name'), // The item it appears as on the UI,
00057     'field' => array(
00058       '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.'),
00059       'handler' => 'views_handler_field',
00060       'click sortable' => TRUE,
00061     ),
00062     'filter' => array(
00063       'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
00064       'handler' => 'views_handler_filter_vocabulary_machine_name',
00065     ),
00066     'argument' => array(
00067       'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
00068       'handler' => 'views_handler_argument_vocabulary_machine_name',
00069     ),
00070   );
00071   $data['taxonomy_vocabulary']['vid'] = array(
00072     'title' => t('Vocabulary ID'), // The item it appears as on the UI,
00073     'help' => t('The taxonomy vocabulary ID'),
00074     'field' => array(
00075       'handler' => 'views_handler_field_numeric',
00076       'click sortable' => TRUE,
00077     ),
00078     'argument' => array(
00079       'handler' => 'views_handler_argument_vocabulary_vid',
00080       'name field' => 'name',
00081     ),
00082     'sort' => array(
00083       'handler' => 'views_handler_sort',
00084     ),
00085   );
00086   $data['taxonomy_vocabulary']['description'] = array(
00087     'title' => t('Description'), // The item it appears as on the UI,
00088     'help' => t('The taxonomy vocabulary description'),
00089     'field' => array(
00090       'handler' => 'views_handler_field',
00091     ),
00092   );
00093 
00094   // ----------------------------------------------------------------------
00095  // taxonomy_term_data table
00096 
00097   $data['term_data']['moved to'] = 'taxonomy_term_data';
00098   $data['taxonomy_term_data']['table']['group']  = t('Taxonomy term');
00099   $data['taxonomy_term_data']['table']['base'] = array(
00100     'field' => 'tid',
00101     'title' => t('Term'),
00102     'help' => t('Taxonomy terms are attached to nodes.'),
00103     'access query tag' => 'term_access',
00104   );
00105   $data['taxonomy_term_data']['table']['entity type'] = 'taxonomy_term';
00106 
00107 
00108 
00109   // The term data table
00110   $data['taxonomy_term_data']['table']['join'] = array(
00111     'taxonomy_vocabulary' => array(
00112       'field' => 'vid',
00113       'left_field' => 'vid',
00114     ),
00115     // This is provided for many_to_one argument
00116     'taxonomy_index' => array(
00117       'field' => 'tid',
00118       'left_field' => 'tid',
00119     ),
00120   );
00121 
00122   // Provide a "default relationship" to keep older views from choking.
00123   $data['taxonomy_term_data']['table']['default_relationship'] = array(
00124     'node' => array(
00125       'table' => 'node',
00126       'field' => 'term_node_tid',
00127     ),
00128   );
00129 
00130   // tid field
00131   $data['taxonomy_term_data']['tid'] = array(
00132     'title' => t('Term ID'),
00133     'help' => t('The tid of a taxonomy term.'),
00134     'field' => array(
00135       'handler' => 'views_handler_field_numeric',
00136       'click sortable' => TRUE,
00137     ),
00138     'sort' => array(
00139       'handler' => 'views_handler_sort',
00140     ),
00141     'argument' => array(
00142       'handler' => 'views_handler_argument_taxonomy',
00143       'name field' => 'name',
00144       'zero is null' => TRUE,
00145     ),
00146     'filter' => array(
00147       'title' => t('Term'),
00148       'help' => t('Taxonomy term chosen from autocomplete or select widget.'),
00149       'handler' => 'views_handler_filter_term_node_tid',
00150       'hierarchy table' => 'taxonomy_term_hierarchy',
00151       'numeric' => TRUE,
00152     ),
00153   );
00154 
00155   $data['taxonomy_term_data']['tid_representative'] = array(
00156     'relationship' => array(
00157       'title' => t('Representative node'),
00158       'label'  => t('Representative node'),
00159       'help' => t('Obtains a single representative node for each term, according to a chosen sort criterion.'),
00160       'handler' => 'views_handler_relationship_groupwise_max',
00161       'relationship field' => 'tid',
00162       'outer field' => 'taxonomy_term_data.tid',
00163       'argument table' => 'taxonomy_term_data',
00164       'argument field' =>  'tid',
00165       'base'   => 'node',
00166       'field'  => 'nid',
00167     ),
00168   );
00169 
00170   // Term name field
00171   $data['taxonomy_term_data']['name'] = array(
00172     'title' => t('Name'),
00173     'help' => t('The taxonomy term name.'),
00174     'field' => array(
00175       'handler' => 'views_handler_field_taxonomy',
00176       'click sortable' => TRUE,
00177     ),
00178     'sort' => array(
00179       'handler' => 'views_handler_sort',
00180     ),
00181     'filter' => array(
00182       'handler' => 'views_handler_filter_string',
00183       'help' => t('Taxonomy term name.'),
00184     ),
00185     'argument' => array(
00186       'handler' => 'views_handler_argument_string',
00187       'help' => t('Taxonomy term name.'),
00188       'many to one' => TRUE,
00189       'empty field name' => t('Uncategorized'),
00190     ),
00191   );
00192 
00193   // taxonomy weight
00194   $data['taxonomy_term_data']['weight'] = array(
00195     'title' => t('Weight'),
00196     'help' => t('The term weight field'),
00197     'field' => array(
00198       'handler' => 'views_handler_field_numeric',
00199       'click sortable' => TRUE,
00200     ),
00201     'sort' => array(
00202       'handler' => 'views_handler_sort',
00203     ),
00204     'filter' => array(
00205       'handler' => 'views_handler_filter_numeric',
00206     ),
00207   );
00208 
00209   // Term description
00210   $data['taxonomy_term_data']['description'] = array(
00211     'title' => t('Term description'),
00212     'help' => t('The description associated with a taxonomy term.'),
00213     'field' => array(
00214       'handler' => 'views_handler_field_markup',
00215       'format' => array('field' => 'format'),
00216     ),
00217     'filter' => array(
00218       'handler' => 'views_handler_filter_string',
00219     ),
00220   );
00221 
00222   // Term vocabulary
00223   $data['taxonomy_term_data']['vid'] = array(
00224     'title' => t('Vocabulary'),
00225     'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
00226     'filter' => array(
00227       'handler' => 'views_handler_filter_vocabulary_vid',
00228     ),
00229   );
00230 
00231   // Link to edit the term
00232   $data['taxonomy_term_data']['edit_term'] = array(
00233     'field' => array(
00234       'title' => t('Term edit link'),
00235       'help' => t('Provide a simple link to edit the term.'),
00236       'handler' => 'views_handler_field_term_link_edit',
00237     ),
00238   );
00239 
00240   // ----------------------------------------------------------------------
00241   // taxonomy_index table
00242 
00243   $data['term_node']['moved to'] = 'taxonomy_index';
00244   $data['taxonomy_index']['table']['group']  = t('Taxonomy term');
00245 
00246   $data['taxonomy_index']['table']['join'] = array(
00247     'taxonomy_term_data' => array(
00248       // links directly to taxonomy_term_data via tid
00249       'left_field' => 'tid',
00250       'field' => 'tid',
00251     ),
00252     'node' => array(
00253       // links directly to node via nid
00254       'left_field' => 'nid',
00255       'field' => 'nid',
00256     ),
00257     'taxonomy_term_hierarchy' => array(
00258       'left_field' => 'tid',
00259       'field' => 'tid',
00260     ),
00261   );
00262 
00263   $data['taxonomy_index']['nid'] = array(
00264     'title' => t('Content with term'),
00265     'help' => t('Relate all content tagged with a term.'),
00266     'relationship' => array(
00267       'handler' => 'views_handler_relationship',
00268       'base' => 'node',
00269       'base field' => 'nid',
00270       'label' => t('node'),
00271       'skip base' => 'node',
00272     ),
00273   );
00274 
00275   // @todo This stuff needs to move to a node field since
00276   // really it's all about nodes.
00277   // tid field
00278   $data['taxonomy_index']['tid'] = array(
00279     'group' => t('Content'),
00280     'title' => t('Has taxonomy term ID'),
00281     'help' => t('Display content if it has the selected taxonomy terms.'),
00282     'argument' => array(
00283       'handler' => 'views_handler_argument_term_node_tid',
00284       'name table' => 'taxonomy_term_data',
00285       'name field' => 'name',
00286       'empty field name' => t('Uncategorized'),
00287       'numeric' => TRUE,
00288       'skip base' => 'taxonomy_term_data',
00289     ),
00290     'filter' => array(
00291       'title' => t('Has taxonomy term'),
00292       'handler' => 'views_handler_filter_term_node_tid',
00293       'hierarchy table' => 'taxonomy_term_hierarchy',
00294       'numeric' => TRUE,
00295       'skip base' => 'taxonomy_term_data',
00296       'allow empty' => TRUE,
00297     ),
00298   );
00299 
00300   // ----------------------------------------------------------------------
00301   // term_hierarchy table
00302 
00303   $data['taxonomy_term_hierarchy']['table']['group']  = t('Taxonomy term');
00304 
00305   $data['term_hierarchy']['moved to'] = 'taxonomy_term_hierarchy';
00306   $data['taxonomy_term_hierarchy']['table']['join'] = array(
00307     'taxonomy_term_hierarchy' => array(
00308       // links to self through left.parent = right.tid (going down in depth)
00309       'left_field' => 'tid',
00310       'field' => 'parent',
00311     ),
00312     'taxonomy_term_data' => array(
00313       // links directly to taxonomy_term_data via tid
00314       'left_field' => 'tid',
00315       'field' => 'tid',
00316     ),
00317   );
00318 
00319   // Provide a "default relationship" to keep older views from choking.
00320   $data['taxonomy_term_hierarchy']['table']['default_relationship'] = array(
00321     'node' => array(
00322       'table' => 'node',
00323       'field' => 'term_node_tid',
00324     ),
00325   );
00326 
00327   $data['taxonomy_term_hierarchy']['parent'] = array(
00328     'title' => t('Parent term'),
00329     'help' => t('The parent term of the term. This can produce duplicate entries if you are using a vocabulary that allows multiple parents.'),
00330     'relationship' => array(
00331       'base' => 'taxonomy_term_data',
00332       'field' => 'parent',
00333       'label' => t('Parent'),
00334     ),
00335     'argument' => array(
00336       'help' => t('The parent term of the term.'),
00337       'handler' => 'views_handler_argument_taxonomy',
00338     ),
00339   );
00340 
00341   return $data;
00342 }
00343 
00347 function taxonomy_views_data_alter(&$data) {
00348   $data['node']['term_node_tid'] = array(
00349     'title' => t('Taxonomy terms on node'),
00350     '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.'),
00351     'relationship' => array(
00352       'handler' => 'views_handler_relationship_node_term_data',
00353       'label' => t('term'),
00354       'base' => 'taxonomy_term_data',
00355     ),
00356     'field' => array(
00357       'title' => t('All taxonomy terms'),
00358       'help' => t('Display all taxonomy terms associated with a node from specified vocabularies.'),
00359       'handler' => 'views_handler_field_term_node_tid',
00360       'no group by' => TRUE,
00361     ),
00362   );
00363 
00364   $data['node']['term_node_tid_depth'] = array(
00365     '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.'),
00366     'real field' => 'nid',
00367     'argument' => array(
00368       'title' => t('Has taxonomy term ID (with depth)'),
00369       'handler' => 'views_handler_argument_term_node_tid_depth',
00370       'accept depth modifier' => TRUE,
00371     ),
00372     'filter' => array(
00373       'title' => t('Has taxonomy terms (with depth)'),
00374       'handler' => 'views_handler_filter_term_node_tid_depth',
00375     ),
00376   );
00377 
00378   $data['node']['term_node_tid_depth_modifier'] = array(
00379     'title' => t('Has taxonomy term ID depth modifier'),
00380     'help' => t('Allows the "depth" for Taxonomy: Term ID (with depth) to be modified via an additional contextual filter value.'),
00381     'argument' => array(
00382       'handler' => 'views_handler_argument_term_node_tid_depth_modifier',
00383     ),
00384   );
00385 }
00386 
00395 function taxonomy_field_views_data($field) {
00396   $data = field_views_field_default_views_data($field);
00397   foreach ($data as $table_name => $table_data) {
00398     foreach ($table_data as $field_name => $field_data) {
00399       if (isset($field_data['filter']) && $field_name != 'delta') {
00400         $data[$table_name][$field_name]['filter']['handler'] = 'views_handler_filter_term_node_tid';
00401         $data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary'];
00402       }
00403     }
00404 
00405     // Add the relationship only on the tid field.
00406     $data[$table_name][$field['field_name'] . '_tid']['relationship'] = array(
00407       'handler' => 'views_handler_relationship',
00408       'base' => 'taxonomy_term_data',
00409       'base field' => 'tid',
00410       'label' => t('term from !field_name', array('!field_name' => $field['field_name'])),
00411     );
00412 
00413   }
00414 
00415   return $data;
00416 }
00417 
00423 function taxonomy_field_views_data_views_data_alter(&$data, $field) {
00424   foreach ($field['bundles'] as $entity_type => $bundles) {
00425     $entity_info = entity_get_info($entity_type);
00426     $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
00427 
00428     list($label, $all_labels) = field_views_field_label($field['field_name']);
00429     $entity = $entity_info['label'];
00430     if ($entity == t('Node')) {
00431       $entity = t('Content');
00432     }
00433 
00434     $data['taxonomy_term_data'][$pseudo_field_name]['relationship'] = array(
00435       'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)),
00436       'help' => t('Relate each @entity with a @field set to the term.', array('@entity' => $entity, '@field' => $label)),
00437       'handler' => 'views_handler_relationship_entity_reverse',
00438       'field_name' => $field['field_name'],
00439       'field table' => _field_sql_storage_tablename($field),
00440       'field field' => $field['field_name'] . '_tid',
00441       'base' => $entity_info['base table'],
00442       'base field' => $entity_info['entity keys']['id'],
00443       'label' => t('!field_name', array('!field_name' => $field['field_name'])),
00444       'join_extra' => array(
00445         0 => array(
00446           'field' => 'entity_type',
00447           'value' => $entity_type,
00448         ),
00449         1 => array(
00450           'field' => 'deleted',
00451           'value' => 0,
00452           'numeric' => TRUE,
00453         ),
00454       ),
00455     );
00456   }
00457 }
00458 
00462 function taxonomy_views_plugins() {
00463   return array(
00464     'module' => 'views', // This just tells our themes are elsewhere.
00465     'argument validator' => array(
00466       'taxonomy_term' => array(
00467         'title' => t('Taxonomy term'),
00468         'handler' => 'views_plugin_argument_validate_taxonomy_term',
00469         'path' => drupal_get_path('module', 'views') . '/modules/taxonomy', // not necessary for most modules
00470       ),
00471     ),
00472     'argument default' => array(
00473       'taxonomy_tid' => array(
00474         'title' => t('Taxonomy term ID from URL'),
00475         'handler' => 'views_plugin_argument_default_taxonomy_tid',
00476         'path' => drupal_get_path('module', 'views') . '/modules/taxonomy',
00477         'parent' => 'fixed',
00478       ),
00479     ),
00480   );
00481 }
00482 
00486 function views_taxonomy_set_breadcrumb(&$breadcrumb, &$argument) {
00487   if (empty($argument->options['set_breadcrumb'])) {
00488     return;
00489   }
00490 
00491   $args = $argument->view->args;
00492   $parents = taxonomy_get_parents_all($argument->argument);
00493   foreach (array_reverse($parents) as $parent) {
00494     // Unfortunately parents includes the current argument. Skip.
00495     if ($parent->tid == $argument->argument) {
00496       continue;
00497     }
00498     if (!empty($argument->options['use_taxonomy_term_path'])) {
00499       $path = taxonomy_term_uri($parent);
00500     }
00501     else {
00502       $args[$argument->position] = $parent->tid;
00503       $path = $argument->view->get_url($args);
00504     }
00505     $breadcrumb[$path] = check_plain($parent->name);
00506   }
00507 }
00508 

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1