drupal/core/modules/taxonomy/taxonomy.views.inc

428 lines
12 KiB
PHP
Raw Normal View History

2009-05-17 11:16:51 +00:00
<?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';
$data['taxonomy_term_data']['table']['wizard_id'] = 'taxonomy_term';
2009-05-17 11:16:51 +00:00
// 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(
2012-08-11 11:46:07 +00:00
'id' => 'numeric',
2009-05-17 11:16:51 +00:00
),
'sort' => array(
2012-08-11 11:46:07 +00:00
'id' => 'standard',
2009-05-17 11:16:51 +00:00
),
'argument' => array(
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy',
2009-05-17 11:16:51 +00:00
'name field' => 'name',
'zero is null' => TRUE,
),
'filter' => array(
'title' => t('Term'),
'help' => t('Taxonomy term chosen from autocomplete or select widget.'),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid',
2009-05-17 11:16:51 +00:00
'hierarchy table' => 'taxonomy_term_hierarchy',
'numeric' => TRUE,
),
);
2012-06-06 22:26:49 +00:00
// 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(
2012-08-11 11:46:07 +00:00
'id' => 'numeric',
2012-06-06 22:26:49 +00:00
'allow empty' => TRUE,
),
);
2009-05-17 11:16:51 +00:00
$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.'),
2012-08-11 11:46:07 +00:00
'id' => 'groupwise_max',
2009-05-17 11:16:51 +00:00
'relationship field' => 'tid',
'outer field' => 'taxonomy_term_data.tid',
'argument table' => 'taxonomy_term_data',
'argument field' => 'tid',
'base' => 'node',
'field' => 'nid',
'relationship' => 'node:term_node_tid'
2009-05-17 11:16:51 +00:00
),
);
// Term name field
$data['taxonomy_term_data']['name'] = array(
'title' => t('Name'),
'help' => t('The taxonomy term name.'),
'field' => array(
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy',
2009-05-17 11:16:51 +00:00
),
'sort' => array(
2012-08-11 11:46:07 +00:00
'id' => 'standard',
2009-05-17 11:16:51 +00:00
),
'filter' => array(
2012-08-11 11:46:07 +00:00
'id' => 'string',
2009-05-17 11:16:51 +00:00
'help' => t('Taxonomy term name.'),
),
'argument' => array(
2012-08-11 11:46:07 +00:00
'id' => 'string',
2009-05-17 11:16:51 +00:00
'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(
2012-08-11 11:46:07 +00:00
'id' => 'numeric',
2009-05-17 11:16:51 +00:00
),
'sort' => array(
2012-08-11 11:46:07 +00:00
'id' => 'standard',
2009-05-17 11:16:51 +00:00
),
'filter' => array(
2012-08-11 11:46:07 +00:00
'id' => 'numeric',
2009-05-17 11:16:51 +00:00
),
'argument' => array(
2012-08-11 11:46:07 +00:00
'id' => 'numeric',
),
2009-05-17 11:16:51 +00:00
);
// Term description
$data['taxonomy_term_data']['description'] = array(
'title' => t('Term description'),
'help' => t('The description associated with a taxonomy term.'),
'field' => array(
2012-08-11 11:46:07 +00:00
'id' => 'markup',
2009-05-17 11:16:51 +00:00
'format' => array('field' => 'format'),
'click sortable' => FALSE,
2009-05-17 11:16:51 +00:00
),
'filter' => array(
2012-08-11 11:46:07 +00:00
'id' => 'string',
2009-05-17 11:16:51 +00:00
),
);
// 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(
'id' => 'bundle',
2009-05-17 11:16:51 +00:00
),
);
$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',
),
);
2009-05-17 11:16:51 +00:00
// 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.'),
2012-08-11 11:46:07 +00:00
'id' => 'term_link_edit',
'click sortable' => FALSE,
2009-05-17 11:16:51 +00:00
),
);
// Entity translation field.
if (Drupal::moduleHandler()->moduleExists('translation_entity')) {
$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',
),
);
}
2009-05-17 11:16:51 +00:00
// 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(
2012-08-11 11:46:07 +00:00
'id' => 'standard',
2009-05-17 11:16:51 +00:00
'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(
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid',
2009-05-17 11:16:51 +00:00
'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'),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid',
2009-05-17 11:16:51 +00:00
'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'),
),
'filter' => array(
'help' => t('Filter the results of "Taxonomy: Term" by the parent pid.'),
'id' => 'numeric',
),
2009-05-17 11:16:51 +00:00
'argument' => array(
'help' => t('The parent term of the term.'),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy',
2009-05-17 11:16:51 +00:00
),
);
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(
2012-08-11 11:46:07 +00:00
'id' => 'node_term_data',
2009-05-17 11:16:51 +00:00
'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.'),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid',
2009-05-17 11:16:51 +00:00
'no group by' => TRUE,
'click sortable' => FALSE,
2009-05-17 11:16:51 +00:00
),
);
$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)'),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid_depth',
2009-05-17 11:16:51 +00:00
'accept depth modifier' => TRUE,
),
'filter' => array(
'title' => t('Has taxonomy terms (with depth)'),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid_depth',
2009-05-17 11:16:51 +00:00
),
);
$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(
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid_depth_modifier',
2009-05-17 11:16:51 +00:00
),
);
}
/**
* 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') {
2012-08-11 11:46:07 +00:00
$data[$table_name][$field_name]['filter']['id'] = 'taxonomy_index_tid';
2009-05-17 11:16:51 +00:00
$data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary'];
}
}
// Add the relationship only on the tid field.
$data[$table_name][$field['field_name'] . '_tid']['relationship'] = array(
2012-08-11 11:46:07 +00:00
'id' => 'standard',
2009-05-17 11:16:51 +00:00
'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)),
2012-08-11 11:46:07 +00:00
'id' => 'entity_reverse',
2009-05-17 11:16:51 +00:00
'field_name' => $field['field_name'],
'field table' => _field_sql_storage_tablename($field),
'field field' => $field['field_name'] . '_tid',
'base' => $entity_info['base_table'],
'base field' => $entity_info['entity_keys']['id'],
2009-05-17 11:16:51 +00:00
'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.
if ($parent->id() == $argument->argument) {
2009-05-17 11:16:51 +00:00
continue;
}
if (!empty($argument->options['use_taxonomy_term_path'])) {
$path = $parent->uri();
2009-05-17 11:16:51 +00:00
$path = $path['path'];
}
else {
$args[$argument->position] = $parent->id();
$path = $argument->view->getUrl($args);
2009-05-17 11:16:51 +00:00
}
$breadcrumb[$path] = check_plain($parent->label());
2009-05-17 11:16:51 +00:00
}
}