Révision e4c061ad
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/ctools/plugins/access/entity_field_value.inc | ||
---|---|---|
16 | 16 |
'get child' => 'ctools_entity_field_value_ctools_access_get_child', |
17 | 17 |
'get children' => 'ctools_entity_field_value_ctools_access_get_children', |
18 | 18 |
); |
19 |
function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) { |
|
20 | 19 |
|
20 |
function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) { |
|
21 | 21 |
$plugins = &drupal_static(__FUNCTION__, array()); |
22 | 22 |
if (empty($plugins[$parent . ':' . $child])) { |
23 | 23 |
list($entity_type, $bundle_type, $field_name) = explode(':', $child); |
... | ... | |
48 | 48 |
} |
49 | 49 |
|
50 | 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 |
|
|
52 | 51 |
// check that the entity, bundle and field arrays have a value. |
53 | 52 |
// If not, load theme using machine names. |
54 | 53 |
if (empty($entity)) { |
... | ... | |
86 | 85 |
$instance = $instances[$field_name]; |
87 | 86 |
$field = field_info_field_by_id($instance['field_id']); |
88 | 87 |
foreach ($field['columns'] as $column => $attributes) { |
89 |
$columns[] = _field_sql_storage_columnname($field_name, $column); |
|
88 |
$columns[$column] = _field_sql_storage_columnname($field_name, $column);
|
|
90 | 89 |
} |
91 | 90 |
ctools_include('fields'); |
92 | 91 |
$entity = (object)array( |
93 | 92 |
$entity_info['entity keys']['bundle'] => $bundle_type, |
94 | 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'); |
|
95 | 116 |
$langcode = field_valid_language(NULL); |
96 | 117 |
$form['settings'] += (array) ctools_field_invoke_field($instance, 'form', $entity_type, $entity, $form, $form_state, array('default' => TRUE, 'language' => $langcode)); |
97 | 118 |
// weight is really not important once this is populated and will only interfere with the form layout. |
... | ... | |
99 | 120 |
unset($form['settings'][$element]['#weight']); |
100 | 121 |
} |
101 | 122 |
|
102 |
// Need more logic here to handle compound fields. |
|
103 |
foreach ($columns as $column) { |
|
104 |
if (isset($conf[$column]) && is_array($conf[$column])) { |
|
105 |
foreach ($conf[$column] as $delta => $conf_value) { |
|
106 |
if (is_numeric($delta) && is_array($conf_value)) { |
|
107 |
$form['settings'][$field_name][LANGUAGE_NONE][$delta]['value']['#default_value'] = $conf_value['value']; |
|
108 |
} |
|
109 |
} |
|
110 |
} |
|
111 |
else { |
|
112 |
$form['settings'][$field_name][LANGUAGE_NONE]['#default_value'] = $conf[$column]; |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 | 123 |
return $form; |
117 | 124 |
} |
118 | 125 |
|
119 |
/** |
|
120 |
* Compress the entity bundles allowed to the minimum. |
|
121 |
*/ |
|
122 | 126 |
function ctools_entity_field_value_ctools_access_settings_submit($form, &$form_state) { |
123 | 127 |
$plugin = $form_state['plugin']; |
124 | 128 |
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); |
... | ... | |
128 | 132 |
$instance = $instances[$field_name]; |
129 | 133 |
$field = field_info_field_by_id($instance['field_id']); |
130 | 134 |
foreach ($field['columns'] as $column => $attributes) { |
131 |
$columns[] = _field_sql_storage_columnname($field_name, $column); |
|
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; |
|
132 | 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; |
|
133 | 175 |
foreach ($columns as $column) { |
134 |
$form_state['values']['settings'][$column] = $form_state['input']['settings'][$field_name][$langcode]; |
|
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] : ''; |
|
135 | 197 |
} |
198 |
return $column_items; |
|
136 | 199 |
} |
137 | 200 |
|
138 | 201 |
/** |
139 | 202 |
* Check for access. |
140 | 203 |
*/ |
141 | 204 |
function ctools_entity_field_value_ctools_access_check($conf, $context, $plugin) { |
142 |
if (!isset($context->data)) {
|
|
205 |
if ((!is_object($context)) || (empty($context->data))) {
|
|
143 | 206 |
// If the context doesn't exist -- for example, a newly added entity |
144 | 207 |
// reference is used as a pane visibility criteria -- we deny access. |
145 | 208 |
return FALSE; |
... | ... | |
166 | 229 |
|
167 | 230 |
// Check field value. |
168 | 231 |
foreach ($field_items as $field_value) { |
169 |
foreach ($field_value as $field_column => $value) { |
|
170 |
// Iterate through config values. |
|
171 |
foreach ($conf_value_array as $conf_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) { |
|
172 | 236 |
// Check access only for stored in config column values. |
173 |
if (isset($conf_value[$field_column]) && $value == $conf_value[$field_column]) { |
|
174 |
return TRUE; |
|
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 |
} |
|
175 | 245 |
} |
176 | 246 |
} |
247 |
if ($match) { |
|
248 |
return TRUE; |
|
249 |
} |
|
177 | 250 |
} |
178 | 251 |
} |
252 |
return FALSE; |
|
179 | 253 |
} |
180 | 254 |
} |
181 | 255 |
|
... | ... | |
184 | 258 |
|
185 | 259 |
function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) { |
186 | 260 |
if (!is_array($values) || !isset($values[$langcode])) { |
187 |
return; |
|
261 |
return NULL;
|
|
188 | 262 |
} |
189 | 263 |
$conf_values = array(); |
190 | 264 |
|
... | ... | |
207 | 281 |
$entity = (object)array( |
208 | 282 |
$entity_info['entity keys']['bundle'] => $bundle_type, |
209 | 283 |
); |
210 |
$string = ''; |
|
211 | 284 |
$keys = array(); |
212 |
$values = array(); |
|
285 |
$value_keys = array(); |
|
286 |
$keyed_elements = array(); |
|
213 | 287 |
foreach ($field['columns'] as $column => $attributes) { |
214 | 288 |
$conf_key = _field_sql_storage_columnname($field_name, $column); |
215 |
if (count($field['columns']) > 1) { |
|
216 |
// Add some sort of handling for compound fields |
|
217 |
} |
|
218 |
else { |
|
219 |
if (isset($conf[$conf_key])) { |
|
220 |
$entity->{$field_name}[LANGUAGE_NONE][] = array($column => $conf[$conf_key]); |
|
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]); |
|
221 | 309 |
} |
222 | 310 |
} |
223 |
$string .= " @{$column} equals @{$column}_value"; |
|
311 |
|
|
224 | 312 |
$keys['@' . $column] = $column; |
225 |
$values["@{$column}_value"] = $conf[$conf_key];
|
|
313 |
$value_keys[] = "@{$column}_value";
|
|
226 | 314 |
} |
315 |
$elements = array(); |
|
316 |
$items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array(); |
|
227 | 317 |
$view_mode = 'full'; |
228 |
$null = NULL; |
|
229 |
$options = array('language' => LANGUAGE_NONE); |
|
230 | 318 |
ctools_include('fields'); |
231 |
$display = field_get_display($instance, $view_mode, $entity); |
|
319 |
$display = field_get_display($instance, $view_mode, $entity); |
|
320 |
if (!isset($display['module'])) { |
|
321 |
$display['module'] = $field['module']; |
|
322 |
} |
|
232 | 323 |
if (isset($display['module'])) { |
233 |
$display['type'] = 'list_default'; |
|
234 |
$function = $display['module'] . '_field_formatter_view'; |
|
235 |
$items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array(); |
|
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'; |
|
236 | 362 |
if (function_exists($function)) { |
237 |
$elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $items, $display); |
|
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); |
|
238 | 368 |
} |
239 |
$value_keys = array_keys($values); |
|
240 |
foreach ($value_keys as $key => $value) { |
|
241 |
$values[$value] = isset($elements[$key]['#markup']) ? $elements[$key]['#markup'] : ''; |
|
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 |
} |
|
242 | 375 |
} |
243 | 376 |
} |
244 |
$values = array_merge($keys, $values); |
|
245 |
return t($string, $values); |
|
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)))); |
|
246 | 410 |
} |
247 |
|
Formats disponibles : Unified diff
Weekly update of contrib modules