Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / content_types / node_context / node_terms.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('Node terms'),
12
  'icon' => 'icon_node.png',
13
  'description' => t('Taxonomy terms of the referenced node.'),
14
  'required context' => new ctools_context_required(t('Node'), 'node'),
15
  'category' => t('Node'),
16
  'defaults' => array(
17
    'vid' => 0,
18
    'term_format' => 'term-links',
19
    'link' => TRUE,
20
    'term_delimiter' => ', ',
21
  ),
22
);
23

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

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

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

    
42
  $tids = array();
43
  foreach ($result as $term) {
44
    $tids[] = $term->tid;
45
  }
46

    
47
  // Get the real term objects.
48
  $term_objects = taxonomy_term_load_multiple($tids);
49

    
50
  $terms = array();
51

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

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

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

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

    
106
      $formatted_terms = implode($delimiter, $processed_terms);
107
      break;
108
  }
109

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

    
117
  return $block;
118
}
119

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

    
136
  $conf = $form_state['conf'];
137

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

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

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

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

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

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