Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / access / entity_field_value.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control based upon entity bundle.
6
 */
7

    
8
$plugin = array(
9
  'title' => t("(Custom) Entity: Field Value"),
10
  'description' => t('Control access by entity field value.'),
11
  'callback' => 'ctools_entity_field_value_ctools_access_check',
12
  'default' => array('type' => array()),
13
  'settings form' => 'ctools_entity_field_value_ctools_access_settings',
14
  'settings form submit' => 'ctools_entity_field_value_ctools_access_settings_submit',
15
  'summary' => 'ctools_entity_field_value_ctools_access_summary',
16
  'get child' => 'ctools_entity_field_value_ctools_access_get_child',
17
  'get children' => 'ctools_entity_field_value_ctools_access_get_children',
18
);
19

    
20
function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) {
21
  $plugins = &drupal_static(__FUNCTION__, array());
22
  if (empty($plugins[$parent . ':' . $child])) {
23
    list($entity_type, $bundle_type, $field_name) = explode(':', $child);
24
    $plugins[$parent . ':' . $child] = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name);
25
  }
26

    
27
  return $plugins[$parent . ':' . $child];
28
}
29

    
30
function ctools_entity_field_value_ctools_access_get_children($plugin, $parent) {
31
  $plugins = &drupal_static(__FUNCTION__, array());
32
  if (!empty($plugins)) {
33
    return $plugins;
34
  }
35
  $entities = entity_get_info();
36
  foreach ($entities as $entity_type => $entity) {
37
    foreach ($entity['bundles'] as $bundle_type => $bundle) {
38
      foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) {
39
        if (!isset($plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name])) {
40
          $plugin = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field);
41
          $plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name] = $plugin;
42
        }
43
      }
44
    }
45
  }
46

    
47
  return $plugins;
48
}
49

    
50
function _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity = NULL, $bundle = NULL, $field = NULL) {
51
  // check that the entity, bundle and field arrays have a value.
52
  // If not, load theme using machine names.
53
  if (empty($entity)) {
54
    $entity = entity_get_info($entity_type);
55
  }
56

    
57
  if (empty($bundle)) {
58
    $bundle = $entity['bundles'][$bundle_type];
59
  }
60

    
61
  if (empty($field)) {
62
    $field_instances = field_info_instances($entity_type, $bundle_type);
63
    $field = $field_instances[$field_name];
64
  }
65

    
66
  $plugin['title'] = t('@entity @type: @field Field', array('@entity' => $entity['label'], '@type' => $bundle_type, '@field' => $field['label']));
67
  $plugin['keyword'] = $entity_type;
68
  $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
69
  $plugin['name'] = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name;
70
  $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
71
      'type' => $bundle_type,
72
    ));
73

    
74
  return $plugin;
75
}
76

    
77
/**
78
 * Settings form for the 'by entity_bundle' access plugin
79
 */
80
function ctools_entity_field_value_ctools_access_settings($form, &$form_state, $conf) {
81
  $plugin = $form_state['plugin'];
82
  list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
83
  $entity_info = entity_get_info($entity_type);
84
  $instances   = field_info_instances($entity_type, $bundle_type);
85
  $instance    = $instances[$field_name];
86
  $field       = field_info_field_by_id($instance['field_id']);
87
  foreach ($field['columns'] as $column => $attributes) {
88
    $columns[$column] = _field_sql_storage_columnname($field_name, $column);
89
  }
90
  ctools_include('fields');
91
  $entity = (object)array(
92
    $entity_info['entity keys']['bundle'] => $bundle_type,
93
  );
94

    
95
  foreach ($columns as $column => $sql_column) {
96
    if (isset($conf[$sql_column])) {
97
      if (is_array($conf[$sql_column])) {
98
        foreach ($conf[$sql_column] as $delta => $conf_value) {
99
          if (is_numeric($delta)) {
100
            if (is_array($conf_value)) {
101
              $entity->{$field_name}[LANGUAGE_NONE][$delta][$column] = $conf_value[$column];
102
            }
103
            else {
104
              $entity->{$field_name}[LANGUAGE_NONE][$delta][$column] = $conf_value;
105
            }
106
          }
107
        }
108
      }
109
      else {
110
        $entity->{$field_name}[LANGUAGE_NONE][0][$column] = $conf[$sql_column];
111
      }
112
    }
113
  }
114

    
115
  $form['#parents'] = array('settings');
116
  $langcode = field_valid_language(NULL);
117
  $form['settings'] += (array) ctools_field_invoke_field($instance, 'form', $entity_type, $entity, $form, $form_state, array('default' => TRUE, 'language' => $langcode));
118
  // weight is really not important once this is populated and will only interfere with the form layout.
119
  foreach (element_children($form['settings']) as $element) {
120
    unset($form['settings'][$element]['#weight']);
121
  }
122

    
123
  return $form;
124
}
125

    
126
function ctools_entity_field_value_ctools_access_settings_submit($form, &$form_state) {
127
  $plugin = $form_state['plugin'];
128
  list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
129
  $langcode  = field_valid_language(NULL);
130
  $langcode  = isset($form_state['input']['settings'][$field_name][$langcode]) ? $langcode : LANGUAGE_NONE;
131
  $instances = field_info_instances($entity_type, $bundle_type);
132
  $instance  = $instances[$field_name];
133
  $field     = field_info_field_by_id($instance['field_id']);
134
  foreach ($field['columns'] as $column => $attributes) {
135
    $columns[$column] = _field_sql_storage_columnname($field_name, $column);
136
  }
137
  $items = _ctools_entity_field_value_get_proper_form_items($field, $form_state['values']['settings'][$field_name][$langcode], array_keys($columns));
138
  foreach ($columns as $column => $sql_column) {
139
    $column_items = _ctools_entity_field_value_filter_items_by_column($items, $column);
140
    $form_state['values']['settings'][$sql_column] = $column_items;
141
  }
142
  $form_state['values']['settings'][$field_name][$langcode] = $items;
143
}
144

    
145
function _ctools_entity_field_value_get_proper_form_items($field, $form_items, $columns) {
146
  $items = array();
147

    
148
  if (!is_array($form_items)) { // Single value item.
149
    foreach ($columns as $column) {
150
      $items[0][$column] = $form_items;
151
    }
152
    return $items;
153
  }
154

    
155
  foreach ($form_items as $delta => $value) {
156
    $item = array();
157
    if (is_numeric($delta)) { // Array of field values.
158
      if (!is_array($value)) {  // Single value in array.
159
        foreach ($columns as $column) {
160
          $item[$column] = $value;
161
        }
162
      }
163
      else { // Value has colums.
164
        foreach ($columns as $column) {
165
          $item[$column] = isset($value[$column]) ? $value[$column] : '';
166
        }
167
      }
168
    }
169
    $items[] = $item;
170
  }
171

    
172
  // Check if $form_items is an array of columns.
173
  $item = array();
174
  $has_columns = FALSE;
175
  foreach ($columns as $column) {
176
    if (isset($form_items[$column])) {
177
      $has_columns = TRUE;
178
      $item[$column] =  $form_items[$column];
179
    }
180
    else {
181
      $item[$column] = '';
182
    }
183
  }
184
  if ($has_columns) {
185
    $items[] = $item;
186
  }
187

    
188
  // Remove empty values.
189
  $items = _field_filter_items($field, $items);
190
  return $items;
191
}
192

    
193
function _ctools_entity_field_value_filter_items_by_column($items, $column) {
194
  $column_items = array();
195
  foreach ($items as $delta => $values) {
196
    $column_items[$delta] = isset($values[$column]) ? $values[$column] : '';
197
  }
198
  return $column_items;
199
}
200

    
201
/**
202
 * Check for access.
203
 */
204
function ctools_entity_field_value_ctools_access_check($conf, $context, $plugin) {
205
  if ((!is_object($context)) || (empty($context->data))) {
206
    // If the context doesn't exist -- for example, a newly added entity
207
    // reference is used as a pane visibility criteria -- we deny access.
208
    return FALSE;
209
  }
210

    
211
  list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
212

    
213
  if ($field_items = field_get_items($entity_type, $context->data, $field_name)) {
214
    $langcode = field_language($entity_type, $context->data, $field_name);
215
    // Get field storage columns.
216
    $instance = field_info_instance($entity_type, $field_name, $bundle_type);
217
    $field = field_info_field_by_id($instance['field_id']);
218
    $columns = array();
219
    foreach ($field['columns'] as $column => $attributes) {
220
      $columns[$column] = _field_sql_storage_columnname($field_name, $column);
221
    }
222

    
223
    if (isset($conf[$field_name])) {
224
      // We have settings for this field.
225
      $conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($conf[$field_name], $langcode);
226
      if (empty($conf_value_array)) {
227
        return FALSE;
228
      }
229

    
230
      // Check field value.
231
      foreach ($field_items as $field_value) {
232
        // Iterate through config values.
233
        foreach ($conf_value_array as $conf_value) {
234
          $match = FALSE;
235
          foreach ($field_value as $field_column => $value) {
236
            // Check access only for stored in config column values.
237
            if (isset($conf_value[$field_column])) {
238
              if ($value == $conf_value[$field_column]) {
239
                $match = TRUE;
240
              }
241
              else {
242
                $match = FALSE;
243
                break;
244
              }
245
            }
246
          }
247
          if ($match) {
248
            return TRUE;
249
          }
250
        }
251
      }
252
      return FALSE;
253
    }
254
  }
255

    
256
  return FALSE;
257
}
258

    
259
function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) {
260
  if (!is_array($values) || !isset($values[$langcode])) {
261
    return NULL;
262
  }
263
  $conf_values = array();
264

    
265
  foreach ($values[$langcode] as $delta => $value) {
266
    $conf_values[$delta] = $value;
267
  }
268

    
269
  return $conf_values;
270
}
271

    
272
/**
273
 * Provide a summary description based upon the checked entity_bundle.
274
 */
275
function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) {
276
  list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
277
  $instances   = field_info_instances($entity_type, $bundle_type);
278
  $instance    = $instances[$field_name];
279
  $field       = field_info_field_by_id($instance['field_id']);
280
  $entity_info = entity_get_info($entity_type);
281
  $entity      = (object)array(
282
    $entity_info['entity keys']['bundle'] => $bundle_type,
283
  );
284
  $keys   = array();
285
  $value_keys = array();
286
  $keyed_elements = array();
287
  foreach ($field['columns'] as $column => $attributes) {
288
    $conf_key = _field_sql_storage_columnname($field_name, $column);
289
    $keyed_elements["@{$column}_value"] = array();
290

    
291
    if (isset($conf[$conf_key])) {
292
      if (is_array($conf[$conf_key])) {
293
        $i = 0;
294
        foreach ($conf[$conf_key] as $conf_value) {
295
          if (!is_array($conf_value)) {
296
            $entity->{$field_name}[LANGUAGE_NONE][$i][$column] = $conf_value;
297
            $keyed_elements["@{$column}_value"][$i] = array('#markup' => $conf_value);
298
          }
299
          elseif (isset($conf_value[$column])) {
300
            $entity->{$field_name}[LANGUAGE_NONE][$i][$column] = $conf_value[$column];
301
            $keyed_elements["@{$column}_value"][$i] = array('#markup' => $conf_value[$column]);
302
          }
303
          $i++;
304
        }
305
      }
306
      else {
307
        $entity->{$field_name}[LANGUAGE_NONE][0][$column] = $conf[$conf_key];
308
        $keyed_elements["@{$column}_value"][0] = array('#markup' => $conf[$conf_key]);
309
      }
310
    }
311

    
312
    $keys['@' . $column] = $column;
313
    $value_keys[] = "@{$column}_value";
314
  }
315
  $elements = array();
316
  $items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array();
317
  $view_mode = 'full';
318
  ctools_include('fields');
319
  $display = field_get_display($instance, $view_mode, $entity);
320
  if (!isset($display['module'])) {
321
    $display['module'] = $field['module'];
322
  }
323
  if (isset($display['module'])) {
324
    // Choose simple formatter for well known cases.
325
    switch ($display['module']) {
326
      case 'text':
327
        $display['type'] = 'text_default';
328
        break;
329

    
330
      case 'list':
331
        $display['type'] = 'list_default';
332
        if ($field['type'] == 'list_boolean') {
333
          $allowed_values = list_allowed_values($field, $instance, $entity_type, $entity);
334
          foreach ($items as $item) {
335
            if (isset($allowed_values[$item['value']])) {
336
              if ($allowed_values[$item['value']] == '') {
337
                $display['type'] = 'list_key';
338
                break;
339
              }
340
            }
341
            else {
342
              $display['type'] = 'list_key';
343
            }
344
          }
345
        }
346
        break;
347

    
348
      case 'taxonomy':
349
        $display['type'] = 'taxonomy_term_reference_plain';
350
        break;
351

    
352
      case 'entityreference':
353
        $display['type'] = 'entityreference_label';
354
        break;
355

    
356
      default :
357
        // Use field instance formatter setting.
358
        break;
359
    }
360

    
361
    $function = $display['module'] . '_field_formatter_view';
362
    if (function_exists($function)) {
363
      $entity_group = array(0 => $entity);
364
      $item_group = array(0 => $items);
365
      $instance_group = array(0 => $instance);
366
      field_default_prepare_view($entity_type, $entity_group, $field, $instance_group, LANGUAGE_NONE, $item_group, $display);
367
      $elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $item_group[0], $display);
368
    }
369
  }
370
  if (count($elements) > 0) {
371
    foreach ($field['columns'] as $column => $attributes) {
372
      if (count($field['columns']) == 1) {
373
        $keyed_elements["@{$column}_value"] = $elements;
374
      }
375
    }
376
  }
377
  $values = array();
378
  foreach ($value_keys as $key) {
379
    $output = array();
380
    $elements = $keyed_elements[$key];
381
    if (is_array($elements)) {
382
      foreach ($elements as $element_key => $element) {
383
        if (is_numeric($element_key)) {
384
          $value_str= strip_tags(drupal_render($element));
385
          if (strlen($value_str) > 0) {
386
            $output[] = $value_str;
387
          }
388
        }
389
      }
390
    }
391
    else {
392
      $value_str = strip_tags(drupal_render($elements));
393
      if (strlen($value_str) > 0) {
394
        $output[] = $value_str;
395
      }
396
    }
397
    $value = implode(', ', $output);
398
    if ($value !== '') {
399
      $values[$key] = implode(', ', $output);
400
    }
401
  }
402
  $string = '';
403
  $value_count = count($values);
404
  foreach ($keys as $key_name => $column) {
405
    if (isset($values[$key_name . '_value'])) {
406
      $string .= ($value_count > 1) ? " @{$column} = @{$column}_value" : "@{$column}_value";
407
    }
408
  }
409
  return t('@field is set to "!value"', array('@field' => $instance['label'], '!value' => format_string($string, array_merge($keys, $values))));
410
}