Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / content_types / vocabulary_context / vocabulary_terms.inc @ c304a780

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
if (module_exists('taxonomy')) {
8
  /**
9
   * Plugins are described by creating a $plugin array which will be used
10
   * by the system that includes this file.
11
   */
12
  $plugin = array(
13
    'single' => TRUE,
14
    'title' => t('Vocabulary terms'),
15
    'icon' => 'icon_vocabulary.png',
16
    'description' => t('All the terms in a vocabulary.'),
17
    'required context' => new ctools_context_required(t('Vocabulary'), 'entity:taxonomy_vocabulary'),
18
    'category' => t('Vocabulary'),
19
    'defaults' => array('max_depth' => 0, 'tree' => 1),
20
  );
21
}
22

    
23
/**
24
 * Output function for the 'vocabulary terms' content type. Outputs a
25
 * list of terms for the input vocabulary.
26
 */
27
function ctools_vocabulary_terms_content_type_render($subtype, $conf, $panel_args, $context) {
28
  $vocab = isset($context->data) ? clone $context->data : NULL;
29
  $max_depth = (!empty($conf['max_depth']) ? (int) $conf['max_depth'] : NULL);
30
  if ($conf['tree'] == FALSE) {
31
    $terms = taxonomy_get_tree($vocab->vid, 0, $max_depth);
32
    $items = array();
33
    foreach ($terms as $term) {
34
      $items[] = l($term->name, 'taxonomy/term/' . $term->tid);
35
    }
36
    $output = theme('item_list', array('items' => $items));
37
  }
38
  else {
39
    $output = theme('item_list', array('items' => _ctools_content_vocabulary_terms($vocab->vid, $max_depth)));
40
  }
41

    
42
  $block          = new stdClass();
43
  $block->module  = 'node_type';
44
  $block->title   = check_plain($vocab->name);
45
  $block->content = $output;
46
  $block->delta   = $vocab->vid;
47

    
48
  return $block;
49
}
50

    
51
function _ctools_content_vocabulary_terms($vid, $max_depth, $depth = -1, $tid = 0) {
52
  $depth++;
53
  if ($max_depth != NULL && $depth == $max_depth) {
54
    return array();
55
  }
56
  $return = array();
57
  $query = db_select('taxonomy_term_data', 't')->fields('t', array('tid'));
58
  $query->join('taxonomy_term_hierarchy', 'h', ' t.tid = h.tid');
59
  $query->condition('t.vid', $vid)
60
    ->condition('h.parent', $tid)
61
    ->orderBy('t.weight')
62
    ->orderBy('t.name');
63
  $tids = $query->execute()->fetchCol();
64
  $terms = taxonomy_term_load_multiple($tids);
65
  foreach ($terms as $term) {
66
    $return[] = array(
67
      'data' => l($term->name, 'taxonomy/term/' . $term->tid),
68
      'children' => _ctools_content_vocabulary_terms($vid, $max_depth, $depth, $term->tid),
69
    );
70
  }
71
  return $return;
72
}
73

    
74
function ctools_vocabulary_terms_content_type_admin_title($subtype, $conf, $context) {
75
  return t('"@s" terms', array('@s' => $context->identifier));
76
}
77

    
78
function ctools_vocabulary_terms_content_type_edit_form($form, &$form_state) {
79
  $conf = $form_state['conf'];
80
  $form['max_depth'] = array(
81
    '#type' => 'select',
82
    '#title' => t('Maximum depth'),
83
    '#options' => array_merge(array(t('unlimited')), range(1, 9)),
84
    '#default_value' => $conf['max_depth'],
85
    '#description' => t('Define the maximum depth of terms being displayed.'),
86
  );
87

    
88
  $form['tree'] = array(
89
    '#type' => 'checkbox',
90
    '#title' => t('Display as tree'),
91
    '#default_value' => $conf['tree'],
92
    '#description' => t('If checked, the terms are displayed in a tree, otherwise in a flat list.'),
93
  );
94

    
95
  return $form;
96
}
97

    
98
function ctools_vocabulary_terms_content_type_edit_form_submit($form, &$form_state) {
99
  // Copy everything from our defaults.
100
  foreach (array_keys($form_state['plugin']['defaults']) as $key) {
101
    $form_state['conf'][$key] = $form_state['values'][$key];
102
  }
103
}