Projet

Général

Profil

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

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

1
<?php
2

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

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

    
38
  $block = new stdClass();
39
  $block->module  = 'node_type';
40
  $block->title = check_plain($vocab->name);
41
  $block->content = $output;
42
  $block->delta   = $vocab->vid;
43

    
44
  return $block;
45
}
46

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

    
70
function ctools_vocabulary_terms_content_type_admin_title($subtype, $conf, $context) {
71
  return t('"@s" terms', array('@s' => $context->identifier));
72
}
73

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

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

    
91
  return $form;
92
}
93

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