Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / content_types / node_context / node_terms.inc @ 1e39edcb

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('Node terms'),
10
  'icon' => 'icon_node.png',
11
  'description' => t('Taxonomy terms of the referenced node.'),
12
  'required context' => new ctools_context_required(t('Node'), 'node'),
13
  'category' => t('Node'),
14
  'defaults' => array(
15
    'vid' => 0,
16
    'term_format' => 'term-links',
17
    'link' => TRUE,
18
    'term_delimiter' => ', ',
19
  ),
20
);
21

    
22
/**
23
 * Render the node_terms content type.
24
 */
25
function ctools_node_terms_content_type_render($subtype, $conf, $panel_args, $context) {
26
  if (empty($context) || empty($context->data)) {
27
    return;
28
  }
29

    
30
  // Get a shortcut to the node.
31
  $node = $context->data;
32

    
33
  // Load all terms for this node from all vocabularies
34
  $query = db_select('taxonomy_index', 't');
35
  $result = $query
36
    ->fields('t')
37
    ->condition('t.nid', $node->nid)
38
    ->execute();
39

    
40
  $tids = array();
41
  foreach ($result AS $term) {
42
    $tids[] = $term->tid;
43
  }
44

    
45
  // Get the real term objects
46
  $term_objects = taxonomy_term_load_multiple($tids);
47

    
48
  $terms = array();
49

    
50
  if (empty($conf['vid'])) {
51
    // All terms.
52
    foreach ($term_objects AS $term) {
53
      $terms['taxonomy_term_' . $term->tid] = array(
54
        'title' => check_plain($term->name),
55
        'href' => 'taxonomy/term/' . $term->tid,
56
        'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
57
      );
58
    }
59
  }
60
  else {
61
    // They want something special and custom, we'll have to do this ourselves.
62
    foreach ($term_objects as $term) {
63
      if ($term->vid == $conf['vid']) {
64
        if ($conf['term_format'] == 'term-links') {
65
          $terms['taxonomy_term_' . $term->tid] = array(
66
            'title' => $term->name,
67
            'href' => 'taxonomy/term/' . $term->tid,
68
            'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)),
69
          );
70
        }
71
        elseif (empty($conf['link'])) {
72
          $terms[] = check_plain($term->name);
73
        }
74
        else {
75
          $terms[] = l($term->name, 'taxonomy/term/' . $term->tid, array('attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))));
76
        }
77
      }
78
    }
79
  }
80

    
81
  $formatted_terms = '';
82
  switch ($conf['term_format']) {
83
    case 'term-links':
84
      drupal_alter('link', $terms, $node);
85
      $formatted_terms = theme('links', array('links' => $terms));
86
      break;
87

    
88
    case 'ul':
89
      $formatted_terms = theme('item_list', array('items' => $terms));
90
      break;
91

    
92
    case 'inline-delimited':
93
      $delimiter = isset($conf['term_delimiter']) ? $conf['term_delimiter'] : ', ';
94
      $processed_terms = array();
95
      foreach ($terms as $key => $term) {
96
        if (is_string($term)) {
97
          $processed_terms[$key] = $term;
98
        }
99
        else {
100
          $terms[$key] = l($term['title'], $term['href'], $term);
101
        }
102
      }
103

    
104
      $formatted_terms = implode($delimiter, $processed_terms);
105
      break;
106
  }
107

    
108
  // Build the content type block.
109
  $block = new stdClass();
110
  $block->module  = 'node_terms';
111
  $block->delta   = $node->nid;
112
  $block->title   = t('Terms');
113
  $block->content = $formatted_terms;
114

    
115
  return $block;
116
}
117

    
118
/**
119
 * Returns an edit form for node terms display settings.
120
 *
121
 * The first question is if they want to display all terms or restrict it to a
122
 * specific taxonomy vocabulary.
123
 *
124
 * Then, they're presented with a set of radios to find out how they want the
125
 * terms formatted, which can be either be via theme('links'), a regular item
126
 * list (ul), or inline with a delimiter.  Depending on which radio they
127
 * choose, some other settings might appear. If they're doing either the ul or
128
 * inline, we ask if they want the terms to appear as links or not. If they
129
 * want it inline, we ask what delimiter they want to use.
130
 */
131
function ctools_node_terms_content_type_edit_form($form, &$form_state) {
132
  ctools_include('dependent');
133

    
134
  $conf = $form_state['conf'];
135

    
136
  $options = array();
137
  $options[0] = t('- All vocabularies -');
138
  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
139
    $options[$vid] = $vocabulary->name;
140
  }
141
  $form['vid'] = array(
142
    '#title' => t('Vocabulary'),
143
    '#type' => 'select',
144
    '#options' => $options,
145
    '#default_value' => $conf['vid'],
146
    '#description' => t('Optionally restrict the terms to a specific vocabulary, or allow terms from all vocabularies.'),
147
    '#prefix' => '<div class="clearfix">',
148
    '#suffix' => '</div>',
149
  );
150

    
151
  $form['term_format'] = array(
152
    '#type' => 'radios',
153
    '#title' => t('Term formatting'),
154
    '#options' => array(
155
      'term-links' => t("Taxonomy links (uses theme('links'))"),
156
      'ul' => t('Unordered list'),
157
      'inline-delimited' => t('Inline, delimited'),
158
    ),
159
    '#default_value' => $conf['term_format'],
160
    '#prefix' => '<div class="clearfix">',
161
    '#suffix' => '</div>',
162
  );
163

    
164
  $form['link'] = array(
165
    '#title' => t('Link to terms'),
166
    '#type' => 'checkbox',
167
    '#default_value' => $conf['link'],
168
    '#description' => t('Check here to make the terms link to the term paths.'),
169
    '#dependency' => array('radio:term_format' => array('inline-delimited', 'ul')),
170
    '#prefix' => '<div class="clearfix">',
171
    '#suffix' => '</div>',
172
  );
173

    
174
  $form['term_delimiter'] = array(
175
    '#type' => 'textfield',
176
    '#title' => t('Term delimiter'),
177
    '#default_value' => $conf['term_delimiter'],
178
    '#size' => 10,
179
    '#dependency' => array('radio:term_format' => array('inline-delimited')),
180
  );
181
  return $form;
182
}
183

    
184
/**
185
 * Submit handler for the custom type settings form.
186
 */
187
function ctools_node_terms_content_type_edit_form_submit($form, &$form_state) {
188
  // Copy everything from our defaults.
189
  foreach (array_keys($form_state['plugin']['defaults']) as $key) {
190
    $form_state['conf'][$key] = $form_state['values'][$key];
191
  }
192
}
193

    
194
/**
195
 * Returns the administrative title for a type.
196
 */
197
function ctools_node_terms_content_type_admin_title($subtype, $conf, $context) {
198
  $placeholders['@s'] = $context->identifier;
199
  if (!empty($conf['vid'])) {
200
    $vocabulary = taxonomy_vocabulary_load($conf['vid']);
201
    $placeholders['@vocabulary'] = $vocabulary->name;
202
    return t('"@s" terms from @vocabulary', $placeholders);
203
  }
204
  return t('"@s" terms', $placeholders);
205
}