Projet

Général

Profil

Paste
Télécharger (4,78 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / term.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * @file
5
 *
6
 * Plugin to provide a term context
7
 */
8

    
9
/**
10
 * Plugins are described by creating a $plugin array which will be used
11
 * by the system that includes this file.
12
 */
13
$plugin = array(
14
  'title' => t("Taxonomy term"),
15
  'description' => t('A single taxonomy term object.'),
16
  'context' => 'ctools_context_create_term',
17
  'edit form' => 'ctools_context_term_settings_form',
18
  'defaults' => array(
19
    'vid' => '',
20
    'tid' => '',
21
  ),
22
  'keyword' => 'term',
23
  'context name' => 'term',
24
  'convert list' => array(
25
    'tid' => t('Term ID'),
26
    'name' => t('Term name'),
27
    'name_dashed' => t('Term name, lowercased and spaces converted to dashes'),
28
    'description' => t('Term Description'),
29
    'vid' => t('Vocabulary ID'),
30
  ),
31
  'convert' => 'ctools_context_term_convert',
32
  // This context is deprecated and should not be usable in the UI.
33
  'no ui' => TRUE,
34
  'no required context ui' => TRUE,
35
);
36

    
37
/**
38
 * It's important to remember that $conf is optional here, because contexts
39
 * are not always created from the UI.
40
 */
41
function ctools_context_create_term($empty, $data = NULL, $conf = FALSE) {
42
  $context = new ctools_context('term');
43
  $context->plugin = 'term';
44

    
45
  if ($empty) {
46
    return $context;
47
  }
48

    
49
  if ($conf && isset($data['tid'])) {
50
    $data = taxonomy_term_load($data['tid']);
51
  }
52

    
53
  if (!empty($data)) {
54
    $context->data        = $data;
55
    $context->title       = $data->name;
56
    $context->argument    = $data->tid;
57
    $context->description = $data->description;
58
    return $context;
59
  }
60
}
61

    
62
function ctools_context_term_settings_form($form, &$form_state) {
63
  $conf = $form_state['conf'];
64

    
65
  $form['vid'] = array(
66
    '#title' => t('Vocabulary'),
67
    '#type' => 'select',
68
    '#options' => array(),
69
    '#description' => t('Select the vocabulary for this form.'),
70
    '#id' => 'ctools-select-vid',
71
    '#default_value' => $conf['vid'],
72
  );
73

    
74
  $description = '';
75
  if (!empty($conf['tid'])) {
76
    $info = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $conf['tid']))->fetchObject();
77
    if ($info) {
78
      $description = ' ' . t('Currently set to @term. Enter another term if you wish to change the term.', array('@term' => $info->name));
79
    }
80
  }
81

    
82
  ctools_include('dependent');
83
  $options = array();
84

    
85
  $form['taxonomy']['#tree'] = TRUE;
86

    
87
  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
88
    $options[$vid] = $vocabulary->name;
89
    $form['taxonomy'][$vocabulary->vid] = array(
90
      '#type' => 'textfield',
91
      '#description' => t('Select a term from @vocabulary.', array('@vocabulary' => $vocabulary->name)) . $description,
92
      '#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
93
      '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
94
    );
95

    
96
  }
97

    
98
  $form['vid']['#options'] = $options;
99

    
100
  $form['tid'] = array(
101
    '#type' => 'value',
102
    '#value' => $conf['tid'],
103
  );
104

    
105
  $form['set_identifier'] = array(
106
    '#type' => 'checkbox',
107
    '#default_value' => FALSE,
108
    '#title' => t('Reset identifier to term title'),
109
    '#description' => t('If checked, the identifier will be reset to the term name of the selected term.'),
110
  );
111

    
112
  return $form;
113
}
114

    
115
/**
116
 * Validate a term.
117
 */
118
function ctools_context_term_settings_form_validate($form, &$form_state) {
119
  // Validate the autocomplete
120
  $vid = $form_state['values']['vid'];
121
  if (empty($form_state['values']['tid']) && empty($form_state['values']['taxonomy'][$vid])) {
122
    form_error($form['taxonomy'][$vid], t('You must select a term.'));
123
    return;
124
  }
125

    
126
  if (empty($form_state['values']['taxonomy'][$vid])) {
127
    return;
128
  }
129

    
130
  $term = db_query('SELECT tid FROM {taxonomy_term_data} WHERE LOWER(name) = LOWER(:name) AND vid = :vid', array(':name' => $form_state['values']['taxonomy'][$vid], ':vid' => $vid))->fetchObject();
131

    
132
  if (!$term) {
133
    form_error($form['taxonomy'][$vid], t('Invalid term selected.'));
134
  }
135
  else {
136
    form_set_value($form['tid'], $term->tid, $form_state);
137
  }
138
}
139

    
140
function ctools_context_term_settings_form_submit($form, &$form_state) {
141
  if ($form_state['values']['set_identifier']) {
142
    $term = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE LOWER(tid) = :tid', array(':tid' => $form_state['values']['tid']))->fetchObject();
143
    $form_state['values']['identifier'] = $term->name;
144
  }
145

    
146
  $form_state['conf']['tid'] = $form_state['values']['tid'];
147
  $form_state['conf']['vid'] = $form_state['values']['vid'];
148
}
149

    
150
/**
151
 * Convert a context into a string.
152
 */
153
function ctools_context_term_convert($context, $type) {
154
  switch ($type) {
155
    case 'tid':
156
      return $context->data->tid;
157
    case 'name':
158
      return $context->data->name;
159
    case 'name_dashed':
160
      return drupal_strtolower(str_replace(' ', '-', $context->data->name));
161
    case 'vid':
162
      return $context->data->vid;
163
    case 'description':
164
      return $context->data->description;
165
  }
166
}