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 @ 6e3ce7c2

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
  // Single value item.
149
  if (!is_array($form_items)) {
150
    foreach ($columns as $column) {
151
      $items[0][$column] = $form_items;
152
    }
153
    return $items;
154
  }
155

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

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

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

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

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

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

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

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

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

    
257
  return FALSE;
258
}
259

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

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

    
270
  return $conf_values;
271
}
272

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

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

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

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

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

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

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

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