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

modules/taxonomy/views_handler_field_term_node_tid.inc

00001 <?php
00002 
00008 class views_handler_field_term_node_tid extends views_handler_field_prerender_list {
00009   function init(&$view, &$options) {
00010     parent::init($view, $options);
00011     // @todo: Wouldn't it be possible to use $this->base_table and no if here?
00012     if ($view->base_table == 'node_revision') {
00013       $this->additional_fields['nid'] = array('table' => 'node_revision', 'field' => 'nid');
00014     }
00015     else {
00016       $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
00017     }
00018 
00019     // Convert legacy vids option to machine name vocabularies.
00020     if (!empty($this->options['vids'])) {
00021       $vocabularies = taxonomy_get_vocabularies();
00022       foreach ($this->options['vids'] as $vid) {
00023         if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
00024           $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
00025         }
00026       }
00027     }
00028   }
00029 
00030   function option_definition() {
00031     $options = parent::option_definition();
00032 
00033     $options['link_to_taxonomy'] = array('default' => TRUE);
00034     $options['limit'] = array('default' => FALSE);
00035     $options['vocabularies'] = array('default' => array());
00036 
00037     return $options;
00038   }
00039 
00043   function options_form(&$form, &$form_state) {
00044     $form['link_to_taxonomy'] = array(
00045       '#title' => t('Link this field to its term page'),
00046       '#type' => 'checkbox',
00047       '#default_value' => !empty($this->options['link_to_taxonomy']),
00048     );
00049 
00050     $form['limit'] = array(
00051       '#type' => 'checkbox',
00052       '#title' => t('Limit terms by vocabulary'),
00053       '#default_value'=> $this->options['limit'],
00054       '#fieldset' => 'more',
00055     );
00056 
00057     $options = array();
00058     $vocabularies = taxonomy_get_vocabularies();
00059     foreach ($vocabularies as $voc) {
00060       $options[$voc->machine_name] = check_plain($voc->name);
00061     }
00062 
00063     $form['vocabularies'] = array(
00064       '#prefix' => '<div><div id="edit-options-vocabularies">',
00065       '#suffix' => '</div></div>',
00066       '#type' => 'checkboxes',
00067       '#title' => t('Vocabularies'),
00068       '#options' => $options,
00069       '#default_value' => $this->options['vocabularies'],
00070       '#dependency' => array('edit-options-limit' => array(TRUE)),
00071       '#fieldset' => 'more',
00072     );
00073 
00074     parent::options_form($form, $form_state);
00075   }
00076 
00080   function query() {
00081     $this->add_additional_fields();
00082   }
00083 
00084   function pre_render(&$values) {
00085     $this->field_alias = $this->aliases['nid'];
00086     $nids = array();
00087     foreach ($values as $result) {
00088       if (!empty($result->{$this->aliases['nid']})) {
00089         $nids[] = $result->{$this->aliases['nid']};
00090       }
00091     }
00092 
00093     if ($nids) {
00094       $query = db_select('taxonomy_term_data', 'td');
00095       $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
00096       $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
00097       $query->fields('td');
00098       $query->addField('tn', 'nid', 'node_nid');
00099       $query->addField('tv', 'name', 'vocabulary');
00100       $query->addField('tv', 'machine_name', 'vocabulary_machine_name');
00101       $query->orderby('td.weight');
00102       $query->orderby('td.name');
00103       $query->condition('tn.nid', $nids);
00104       $query->addTag('term_access');
00105       $vocabs = array_filter($this->options['vocabularies']);
00106       if (!empty($this->options['limit']) && !empty($vocabs)) {
00107         $query->condition('tv.machine_name', $vocabs);
00108       }
00109       $result = $query->execute();
00110 
00111       foreach ($result as $term) {
00112         $this->items[$term->node_nid][$term->tid]['name'] = check_plain($term->name);
00113         $this->items[$term->node_nid][$term->tid]['tid'] = $term->tid;
00114         $this->items[$term->node_nid][$term->tid]['vocabulary_machine_name'] = check_plain($term->vocabulary_machine_name);
00115         $this->items[$term->node_nid][$term->tid]['vocabulary'] = check_plain($term->vocabulary);
00116 
00117         if (!empty($this->options['link_to_taxonomy'])) {
00118           $this->items[$term->node_nid][$term->tid]['make_link'] = TRUE;
00119           $this->items[$term->node_nid][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
00120         }
00121       }
00122     }
00123   }
00124 
00125   function render_item($count, $item) {
00126     return $item['name'];
00127   }
00128 
00129   function document_self_tokens(&$tokens) {
00130     $tokens['[' . $this->options['id'] . '-tid' . ']'] = t('The taxonomy term ID for the term.');
00131     $tokens['[' . $this->options['id'] . '-name' . ']'] = t('The taxonomy term name for the term.');
00132     $tokens['[' . $this->options['id'] . '-vocabulary-machine-name' . ']'] = t('The machine name for the vocabulary the term belongs to.');
00133     $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = t('The name for the vocabulary the term belongs to.');
00134   }
00135 
00136   function add_self_tokens(&$tokens, $item) {
00137     foreach(array('tid', 'name', 'vocabulary_machine_name', 'vocabulary') as $token) {
00138       // Replace _ with - for the vocabulary machine name.
00139       $tokens['[' . $this->options['id'] . '-' . str_replace('_', '-', $token). ']'] = isset($item[$token]) ? $item[$token] : '';
00140     }
00141   }
00142 }

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