Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / content_types / term_context / term_list.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * Plugins are described by creating a $plugin array which will be used
5
 * by the system that includes this file.
6
 */
7
$plugin = array(
8
  'single' => TRUE,
9
  'title' => t('List of related terms'),
10
  'icon' => 'icon_term.png',
11
  'description' => t('Terms related to an existing term; may be child, siblings or top level.'),
12
  'required context' => new ctools_context_required(t('Term'), array('term', 'taxonomy_term')),
13
  'category' => t('Taxonomy term'),
14
  'defaults' => array(
15
    'title' => '',
16
    'type' => 'child',
17
    'include_current_term' => FALSE,
18
    'list_type' => 'ul',
19
    'path' => 'taxonomy/term',
20
  ),
21
);
22

    
23
function ctools_term_list_content_type_render($subtype, $conf, $panel_args, $context) {
24
  $term = isset($context->data) ? clone($context->data) : NULL;
25
  $block = new stdClass();
26
  $block->module = 'term-list';
27
  $path = empty($conf['path']) ? 'taxonomy/term/%tid' : $conf['path'];
28
  if (strpos($path, '%tid') === FALSE) {
29
    if (substr($path, -1) != '/') {
30
      $path .= '/';
31
    }
32
    $path .= '%tid';
33
  }
34

    
35
  $options = ctools_admin_term_list_options();
36
  $skip = array();
37

    
38
  if ($term) {
39
    $block->title = $options[$conf['type']];
40
    $block->delta = $conf['type'];
41
    switch ($conf['type']) {
42
      case 'related':
43
        // FIXME this no longer exists, must be done with Field API
44
        // $terms = taxonomy_get_related($term->tid);
45
        break;
46

    
47
      case 'child':
48
      default:
49
        if (!empty($conf['include_current_term'])) {
50
          $terms[] = $term;
51
        }
52
        $terms = taxonomy_get_children($term->tid);
53
        break;
54

    
55
      case 'top':
56
        $terms = taxonomy_get_tree($term->vid, 0, 1);
57
        break;
58

    
59
      case 'parent':
60
        $terms = taxonomy_get_parents($term->tid);
61
        if (!empty($conf['include_current_term'])) {
62
          $terms[] = $term;
63
        }
64
        $block->title = count($terms) == 1 ? t('Parent term') : t('Parent terms');
65
        break;
66

    
67
      case 'sibling':
68
        $parent = db_query('SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
69
        if ($parent) {
70
          $terms = taxonomy_get_children($parent, $term->vid);
71
        }
72
        else {
73
          $terms = taxonomy_get_tree($term->vid, 0, 1);
74
        }
75

    
76
        $skip[$term->tid] = $term->tid;
77
        break;
78

    
79
      case 'synonyms':
80
        // FIXME this no longer exists, must be done with Field API
81
//        $terms = taxonomy_get_synonyms($term->tid);
82
        break;
83
    }
84

    
85
    if (!empty($terms)) {
86
      foreach ($terms as $related) {
87
        if (empty($skip[$related->tid])) {
88
          $items[] = l($related->name, str_replace('%tid', $related->tid, $path), array('rel' => 'tag', 'title' => strip_tags($related->description)));
89
        }
90
      }
91

    
92
      if (!empty($items)) {
93
        $block->content = theme('item_list', array('items' => $items, 'type' => $conf['list_type']));
94
      }
95
    }
96
  }
97
  else {
98
    $block->content = t('Term description goes here.');
99
    $block->delta = 'unknown';
100
  }
101

    
102
  return $block;
103
}
104

    
105
function ctools_admin_term_list_options() {
106
  return array(
107
    'child' => t('Child terms'),
108
    'related' => t('Related terms (does not work in D7)'),
109
    'sibling' => t('Sibling terms'),
110
    'top' => t('Top level terms'),
111
    'synonyms' => t('Term synonyms (does not work in D7)'),
112
    'parent' => t('Parent term(s)'),
113
  );
114
}
115

    
116
/**
117
 * Returns an edit form for the custom type.
118
 */
119
function ctools_term_list_content_type_edit_form($form, &$form_state) {
120
  $conf = $form_state['conf'];
121

    
122
  $form['type'] = array(
123
    '#type' => 'radios',
124
    '#title' => t('Which terms'),
125
    '#options' => ctools_admin_term_list_options(),
126
    '#default_value' => $conf['type'],
127
    '#prefix' => '<div class="clearfix no-float">',
128
    '#suffix' => '</div>',
129
  );
130

    
131
  $form['include_current_term'] = array(
132
    '#type' => 'checkbox',
133
    '#title' => t('Include the current term in the list'),
134
    '#default_value' => !empty($conf['include_current_term']),
135
    '#prefix' => '<div class="clearfix no-float">',
136
    '#suffix' => '</div>',
137
    '#states' => array(
138
      'invisible' => array(
139
        ':input[name="type"], unique1' => array('!value' => 'child'),
140
        ':input[name="type"], unique2' => array('!value' => 'parent'),
141
      ),
142
    ),
143
  );
144

    
145
  $form['list_type'] = array(
146
    '#type' => 'select',
147
    '#title' => t('List type'),
148
    '#options' => array('ul' => t('Unordered'), 'ol' => t('Ordered')),
149
    '#default_value' => $conf['list_type'],
150
  );
151

    
152
  $form['path'] = array(
153
    '#type' => 'textfield',
154
    '#title' => t('Path'),
155
    '#default_value' => empty($conf['path']) ? 'taxonomy/term/%tid' : $conf['path'],
156
    '#description' => t('The path to use for the terms. You may use %tid to place the term id as part of the path; if let off, it will be appended to the end.'),
157
  );
158
  return $form;
159
}
160

    
161
function ctools_term_list_content_type_admin_title($subtype, $conf, $context) {
162
  $options = ctools_admin_term_list_options();
163
  return t('"@s" @type', array('@s' => $context->identifier, '@type' => drupal_strtolower($options[$conf['type']])));
164
}
165

    
166
function ctools_term_list_content_type_edit_form_submit($form, &$form_state) {
167
  // Copy everything from our defaults.
168
  foreach (array_keys($form_state['plugin']['defaults']) as $key) {
169
    $form_state['conf'][$key] = $form_state['values'][$key];
170
  }
171
}
172