Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugins are described by creating a $plugin array which will be used
6
 * by the system that includes this file.
7
 */
8

    
9
$plugin = array(
10
  'single' => TRUE,
11
  'title' => t('List of related terms'),
12
  'icon' => 'icon_term.png',
13
  'description' => t('Terms related to an existing term; may be child, siblings or top level.'),
14
  'required context' => new ctools_context_required(t('Term'), array('term', 'taxonomy_term')),
15
  'category' => t('Taxonomy term'),
16
  'defaults' => array(
17
    'title' => '',
18
    'type' => 'child',
19
    'include_current_term' => FALSE,
20
    'list_type' => 'ul',
21
    'path' => 'taxonomy/term',
22
  ),
23
);
24

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

    
37
  $options = ctools_admin_term_list_options();
38
  $skip = array();
39

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

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

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

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

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

    
78
        $skip[$term->tid] = $term->tid;
79
        break;
80

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

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

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

    
104
  return $block;
105
}
106

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

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

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

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

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

    
154
  $form['path'] = array(
155
    '#type' => 'textfield',
156
    '#title' => t('Path'),
157
    '#default_value' => empty($conf['path']) ? 'taxonomy/term/%tid' : $conf['path'],
158
    '#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.'),
159
  );
160
  return $form;
161
}
162

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

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