Projet

Général

Profil

Paste
Télécharger (9,16 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / entity.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 *
6
 * Plugin to provide a node context. A node context is a node wrapped in a
7
 * context object that can be utilized by anything that accepts contexts.
8
 */
9

    
10
/**
11
 * Plugins are described by creating a $plugin array which will be used
12
 * by the system that includes this file.
13
 */
14
$plugin = array(
15
  'title' => t("Entity"),
16
  'description' => t('Entity object.'),
17
  'context' => 'ctools_context_create_entity',
18
  'edit form' => 'ctools_context_entity_settings_form',
19
  'defaults' => array('entity_id' => ''),
20
  'convert list' => 'ctools_context_entity_convert_list',
21
  'convert' => 'ctools_context_entity_convert',
22
  'placeholder form' => array(
23
    '#type' => 'textfield',
24
    '#description' => t('Enter the ID of an entity for this context.'),
25
  ),
26
  // Tell ctools_context_get_context_from_context() how to interpret its $argument.
27
  'placeholder name' => 'entity_id',
28
  'get child' => 'ctools_context_entity_get_child',
29
  'get children' => 'ctools_context_entity_get_children',
30
);
31

    
32
function ctools_context_entity_get_child($plugin, $parent, $child) {
33
  $plugins = ctools_context_entity_get_children($plugin, $parent);
34
  return $plugins[$parent . ':' . $child];
35
}
36

    
37
function ctools_context_entity_get_children($plugin, $parent) {
38
  $entities = entity_get_info();
39
  $plugins = array();
40
  foreach ($entities as $entity_type => $entity) {
41
    $child_plugin = $plugin;
42
    $child_plugin['title'] = $entity['label'];
43
    $child_plugin['keyword'] = $entity_type;
44
    $child_plugin['context name'] = $entity_type;
45
    $child_plugin['name'] = $parent . ':' . $entity_type;
46
    $child_plugin['description'] = t('Creates @entity context from an entity ID.', array('@entity' => $entity_type));
47
    $child_plugin_id = $parent . ':' . $entity_type;
48
    drupal_alter('ctools_entity_context', $child_plugin, $entity, $child_plugin_id);
49
    $plugins[$child_plugin_id] = $child_plugin;
50
  }
51
  drupal_alter('ctools_entity_contexts', $plugins);
52
  return $plugins;
53
}
54

    
55
/**
56
 * It's important to remember that $conf is optional here, because contexts
57
 * are not always created from the UI.
58
 */
59
function ctools_context_create_entity($empty, $data = NULL, $conf = FALSE, $plugin) {
60
  $entity_type = $plugin['keyword'];
61
  $entity = entity_get_info($entity_type);
62
  $context = new ctools_context(array('entity:' . $entity_type, 'entity', $entity_type));
63
  $context->plugin = $plugin['name'];
64
  $context->keyword = $entity_type;
65

    
66
  if ($empty) {
67
    return $context;
68
  }
69

    
70
  // Attempt to retain compatibility with broken id:
71
  if (is_array($data) && !isset($data['entity_id']) && isset($data['id'])) {
72
    $id = $data['id'];
73
  }
74
  elseif (is_array($data) && isset($data['entity_id'])) {
75
    $id = $data['entity_id'];
76
  }
77
  elseif (is_object($data)) {
78
    $ids = entity_extract_ids($entity_type, $data);
79
    $id = $ids[0];
80
  }
81
  elseif (is_numeric($data)) {
82
    $id = $data;
83
    $data = entity_load($entity_type, array($id));
84
    $data = !empty($data[$id]) ? $data[$id] : FALSE;
85
  }
86

    
87
  if (is_array($data)) {
88
    $data = entity_load($entity_type, array($id));
89
    $data = !empty($data[$id]) ? $data[$id] : FALSE;
90
  }
91

    
92
  if (!empty($data)) {
93
    $context->data     = $data;
94
    if (!empty($entity['entity keys']['label'])) {
95
      $context->title    = $data->{$entity['entity keys']['label']};
96
    }
97
    $context->argument = $id;
98

    
99
    if ($entity['entity keys']['bundle']) {
100
      $context->restrictions['type'] = array($data->{$entity['entity keys']['bundle']});
101
    }
102
    return $context;
103
  }
104
}
105

    
106
function ctools_context_entity_settings_form($form, &$form_state) {
107
  $conf = &$form_state['conf'];
108
  $plugin = &$form_state['plugin'];
109

    
110
  $form['entity'] = array(
111
    '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
112
    '#type' => 'textfield',
113
    '#maxlength' => 512,
114
    '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
115
    '#weight' => -10,
116
  );
117

    
118
  if (!empty($conf['entity_id'])) {
119
    $info = entity_load($plugin['keyword'], array($conf['entity_id']));
120
    $info = $info[$conf['entity_id']];
121
    if ($info) {
122
      $entity = entity_get_info($plugin['keyword']);
123
      $uri = entity_uri($plugin['keyword'], $info);
124
      if (is_array($uri) && $entity['entity keys']['label']) {
125
        $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
126
      }
127
      elseif (is_array($uri)) {
128
        $link = l(t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
129
      }
130
      elseif ($entity['entity keys']['label']) {
131
        $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), file_create_url($uri), array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
132
      }
133
      else {
134
        $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
135
      }
136
      $form['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
137
    }
138
  }
139

    
140
  $form['entity_id'] = array(
141
    '#type' => 'value',
142
    '#value' => $conf['entity_id'],
143
  );
144

    
145
  $form['entity_type'] = array(
146
    '#type' => 'value',
147
    '#value' => $plugin['keyword'],
148
  );
149

    
150
  $form['set_identifier'] = array(
151
    '#type' => 'checkbox',
152
    '#default_value' => FALSE,
153
    '#title' => t('Reset identifier to entity label'),
154
    '#description' => t('If checked, the identifier will be reset to the entity label of the selected entity.'),
155
  );
156

    
157
  return $form;
158
}
159

    
160
/**
161
 * Validate a node.
162
 */
163
function ctools_context_entity_settings_form_validate($form, &$form_state) {
164
  // Validate the autocomplete
165
  if (empty($form_state['values']['entity_id']) && empty($form_state['values']['entity'])) {
166
    form_error($form['entity'], t('You must select an entity.'));
167
    return;
168
  }
169

    
170
  if (empty($form_state['values']['entity'])) {
171
    return;
172
  }
173

    
174
  $id          = $form_state['values']['entity'];
175
  $preg_matches = array();
176
  $match        = preg_match('/\[id: (\d+)\]/', $id, $preg_matches);
177
  if (!$match) {
178
    $match = preg_match('/^id: (\d+)/', $id, $preg_matches);
179
  }
180

    
181
  if ($match) {
182
    $id = $preg_matches[1];
183
  }
184
  if (is_numeric($id)) {
185
    $entity = entity_load($form_state['values']['entity_type'], array($id));
186
    $entity = $entity[$id];
187
  }
188
  else {
189
    $entity_info = entity_get_info($form_state['values']['entity_type']);
190
    $field = $entity_info['entity keys']['label'];
191
    $entity = entity_load($form_state['values']['entity_type'], FALSE, array($field => $id));
192
  }
193

    
194
  // Do not allow unpublished nodes to be selected by unprivileged users
195
  // || (empty($node->status) && !(user_access('administer nodes'))) need a new sanity check at some point.
196
  if (!$entity) {
197
    form_error($form['entity'], t('Invalid entity selected.'));
198
  }
199
  else {
200
    $entity_id = entity_extract_ids($form_state['values']['entity_type'], $entity);
201
    form_set_value($form['entity_id'], $entity_id[0], $form_state);
202
  }
203
}
204

    
205
function ctools_context_entity_settings_form_submit($form, &$form_state) {
206
  if ($form_state['values']['set_identifier']) {
207
    $entity_info = entity_get_info($form_state['values']['entity_type']);
208
    $entity = entity_load($form_state['values']['entity_type'], array($form_state['values']['entity_id']));
209
    $entity = $entity[$form_state['values']['entity_id']];
210
    $form_state['values']['identifier'] = $entity->{$entity_info['entity keys']['label']};
211
  }
212

    
213
  // This will either be the value set previously or a value set by the
214
  // validator.
215
  $form_state['conf']['entity_id'] = $form_state['values']['entity_id'];
216
}
217

    
218
/**
219
 * Provide a list of ways that this context can be converted to a string.
220
 */
221
function ctools_context_entity_convert_list($plugin) {
222
  $list = array();
223

    
224
  $entity = entity_get_info($plugin['context name']);
225
  if (isset($entity['token type'])) {
226
    $token = $entity['token type'];
227
  }
228
  else {
229
    $token = $plugin['context name'];
230
  }
231

    
232
  // Hack: we need either token.module or a core fix for this to work right,
233
  // until then, we just muscle it.
234
  if ($token == 'taxonomy_term') {
235
    $token = 'term';
236
  }
237

    
238
  $tokens = token_info();
239
  if (isset($tokens['tokens'][$token])) {
240
    foreach ($tokens['tokens'][$token] as $id => $info) {
241
      if (!isset($list[$id])) {
242
        $list[$id] = $info['name'];
243
      }
244
    }
245
  }
246
  return $list;
247
}
248

    
249
/**
250
 * Convert a context into a string.
251
 */
252
function ctools_context_entity_convert($context, $type, $options = array()) {
253
  $entity_type = $context->type[2];
254
  $entity = entity_get_info($entity_type);
255

    
256
  if (isset($entity['token type'])) {
257
    $token = $entity['token type'];
258
  }
259
  else {
260
    $token = $entity_type;
261
  }
262

    
263
  // Hack: we need either token.module or a core fix for this to work right,
264
  // until then, we just muscle it.
265
  if ($token == 'taxonomy_term') {
266
    $token = 'term';
267
  }
268

    
269
  $tokens = token_info();
270

    
271
  $values = token_generate($token, array($type => $type), array($token => $context->data), $options);
272
  if (isset($values[$type])) {
273
    return $values[$type];
274
  }
275
}