Project

General

Profile

Paste
Download (9.15 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / entity.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide a node context. A node context is a node wrapped in a
6
 * context object that can be utilized by anything that accepts contexts.
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("Entity"),
15
  'description' => t('Entity object.'),
16
  'context' => 'ctools_context_create_entity',
17
  'edit form' => 'ctools_context_entity_settings_form',
18
  'defaults' => array('entity_id' => ''),
19
  'convert list' => 'ctools_context_entity_convert_list',
20
  'convert' => 'ctools_context_entity_convert',
21
  'placeholder form' => array(
22
    '#type' => 'textfield',
23
    '#description' => t('Enter the ID of an entity for this context.'),
24
  ),
25
  // Tell ctools_context_get_context_from_context() how to interpret its $argument.
26
  'placeholder name' => 'entity_id',
27
  'get child' => 'ctools_context_entity_get_child',
28
  'get children' => 'ctools_context_entity_get_children',
29
);
30

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

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

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

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

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

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

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

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

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

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

    
117
  if (!empty($conf['entity_id'])) {
118
    $info = entity_load($plugin['keyword'], array($conf['entity_id']));
119
    $info = $info[$conf['entity_id']];
120
    if ($info) {
121
      $entity = entity_get_info($plugin['keyword']);
122
      $uri = entity_uri($plugin['keyword'], $info);
123
      if (is_array($uri) && $entity['entity keys']['label']) {
124
        $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));
125
      }
126
      elseif (is_array($uri)) {
127
        $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));
128
      }
129
      elseif ($entity['entity keys']['label']) {
130
        $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));
131
      }
132
      else {
133
        $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
134
      }
135
      $form['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
136
    }
137
  }
138

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

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

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

    
156
  return $form;
157
}
158

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
268
  $tokens = token_info();
269

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