Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* @file
* Display Suite core functions.
*/
/**
* Constants for field types.
*/
define('DS_FIELD_TYPE_THEME', 1);
define('DS_FIELD_TYPE_FUNCTION', 2);
define('DS_FIELD_TYPE_PREPROCESS', 3);
define('DS_FIELD_TYPE_IGNORE', 4);
define('DS_FIELD_TYPE_CODE', 5);
define('DS_FIELD_TYPE_BLOCK', 6);
define('DS_FIELD_TYPE_CTOOLS', 7);
/**
* Constants for block fields rendering.
*/
define('DS_BLOCK_TEMPLATE', 1);
define('DS_BLOCK_TITLE_CONTENT', 2);
define('DS_BLOCK_CONTENT', 3);
/**
* Implements hook_permission().
*/
function ds_permission() {
return array(
'admin_display_suite' => array(
'title' => t('Administer Display Suite'),
'description' => t('General permission for Display Suite settings pages.')
),
);
}
/**
* Implements hook_hook_info().
*/
function ds_hook_info() {
$hooks['ds_fields_info'] = array(
'group' => 'ds_fields_info',
);
return $hooks;
}
/**
* Implements hook_menu().
*/
function ds_menu() {
module_load_include('inc', 'ds', 'includes/ds.registry');
return _ds_menu();
}
/**
* Implements hook_menu_alter().
*/
function ds_menu_alter(&$items) {
module_load_include('inc', 'ds', 'includes/ds.registry');
return _ds_menu_alter($items);
}
/**
* Implements hook_theme().
*/
function ds_theme() {
module_load_include('inc', 'ds', 'includes/ds.registry');
return _ds_theme();
}
/**
* Implements hook_ds_layout_info().
*/
function ds_ds_layout_info() {
module_load_include('inc', 'ds', 'includes/ds.registry');
return _ds_ds_layout_info();
}
/**
* Implements hook_ctools_plugin_api().
*/
function ds_ctools_plugin_api($owner, $api) {
if ($owner == 'ds' && ($api == 'ds' || $api == 'plugins')) {
return array('version' => 1);
}
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ds_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/' . $plugin_type;
}
}
/**
* Implements hook_views_api().
*/
function ds_views_api() {
return array('api' => 3);
}
/**
* Implements hook_node_type_update().
*/
function ds_node_type_update($info) {
if (!empty($info->old_type) && $info->old_type != $info->type) {
module_load_include('inc', 'ds', 'includes/ds.registry');
_ds_entity_type_update('node', $info, 'update');
}
}
/**
* Implements hook_node_type_delete().
*/
function ds_node_type_delete($info) {
module_load_include('inc', 'ds', 'includes/ds.registry');
_ds_entity_type_update('node', $info, 'delete');
}
/**
* Implements hook_theme_registry_alter().
*/
function ds_theme_registry_alter(&$theme_registry) {
module_load_include('inc', 'ds', 'includes/ds.registry');
_ds_theme_registry_alter($theme_registry);
}
/**
* Implements hook_entity_info_alter().
*/
function ds_entity_info_alter(&$entity_info) {
module_load_include('inc', 'ds', 'includes/ds.registry');
_ds_entity_info_alter($entity_info);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ds_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
form_load_include($form_state, 'inc', 'ds', 'includes/ds.field_ui');
// Also load admin on behalf of DS extras when enabled.
if (module_exists('ds_extras')) {
form_load_include($form_state, 'inc', 'ds_extras', 'includes/ds_extras.admin');
}
ds_field_ui_fields_layouts($form, $form_state);
}
/**
* Implements hook_module_implements_alter().
*/
function ds_module_implements_alter(&$implementations, $hook) {
// node_field_display_module_alter() disables all labels on all fields
// when the view mode is 'search_index'. If you set display modes for
// this view mode by hand, then the hook isn't needed. Since this
// may be called hundreds of times on some pages, it's worth disabling it.
// See http://drupal.org/node/834278
// This code is also in Performance hacks module, but it's not bad to
// disable this too in Display Suite by default.
if ($hook == 'field_display_node_alter') {
unset($implementations['node']);
}
}
/**
* Implements hook_features_api().
*/
function ds_features_api() {
module_load_include('inc', 'ds', 'includes/ds.registry');
return _ds_features_api();
}
/**
* Function to check if we go on with Display Suite.
*/
function ds_kill_switch() {
if (variable_get('ds_disable', FALSE)) {
return TRUE;
}
return FALSE;
}
/**
* Get entity view modes.
*
* @param $entity_type
* The name of the entity type.
*/
function ds_entity_view_modes($entity_type = NULL) {
if (!empty($entity_type)) {
switch ($entity_type) {
// For taxonomy terms the base table and the entity type are different
case 'taxonomy_term_data':
$entity_info = entity_get_info('taxonomy_term');
break;
default:
$entity_info = entity_get_info($entity_type);
break;
}
return $entity_info['view modes'];
}
}
/**
* Get Display Suite layouts.
*/
function ds_get_layout_info() {
static $layouts = FALSE;
if (!$layouts) {
$errors = array();
$layouts = module_invoke_all('ds_layout_info');
// Give modules a chance to alter the layouts information.
drupal_alter('ds_layout_info', $layouts);
// Check that there is no 'content' region, but ignore panel layouts.
// Because when entities are rendered, the field items are stored into a
// 'content' key so fields would be overwritten before they're all moved.
foreach ($layouts as $key => $info) {
if (isset($info['panels'])) {
continue;
}
if (isset($info['regions']['content'])) {
$errors[] = $key;
}
}
if (!empty($errors)) {
drupal_set_message(t('Following layouts have a "content" region key which is invalid: %layouts.', array('%layouts' => implode(', ', $errors))), 'error');
}
}
return $layouts;
}
/**
* Get a layout for a given entity.
*
* @param $entity_type
* The name of the entity.
* @param $bundle
* The name of the bundle.
* @param $view_mode
* The name of the view mode.
* @param $fallback
* Whether to fallback to default or not.
*
* @return $layout
* Array of layout variables for the regions.
*/
function ds_get_layout($entity_type, $bundle, $view_mode, $fallback = TRUE) {
static $layouts = array();
$layout_key = $entity_type . '_' . $bundle . '_' . $view_mode;
if (!isset($layouts[$layout_key])) {
$entity_info = entity_get_info();
$overridden = TRUE;
if ($view_mode != 'form') {
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
$overridden = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE);
}
// Check if a layout is configured.
if (isset($entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode]) && ($overridden || $view_mode == 'default')) {
$layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode];
$layouts[$layout_key]['view_mode'] = $view_mode;
}
// In case $view_mode is not found, check if we have a default layout,
// but only if the view mode settings aren't overridden for this view mode.
if ($view_mode != 'default' && !$overridden && $fallback && isset($entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'])) {
$layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'];
$layouts[$layout_key]['view_mode'] = 'default';
}
// Register the false return value as well.
if (!isset($layouts[$layout_key])) {
$layouts[$layout_key] = FALSE;
}
}
return $layouts[$layout_key];
}
/**
* Get all fields.
*
* @param $entity_type
* The name of the entity.
* @param $cache
* Whether we need to get the fields from cache or not.
* @return
* Collection of fields.
*/
function ds_get_fields($entity_type, $cache = TRUE) {
global $language;
static $static_fields, $fields_cached = array();
static $loaded = FALSE;
// Load the ds file that handles ds call of hook_ds_fields_info, otherwise it
// doesn't get loaded
module_load_include('inc', 'ds', 'ds.ds_fields_info');
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Get fields from cache.
if (!$loaded) {
$loaded = TRUE;
if ($cache && $cached_fields = cache_get('ds_fields:' . $language->language)) {
$fields_cached = $static_fields = $cached_fields->data;
}
}
if (!isset($static_fields[$entity_type])) {
// Do we have them cached or not ?
if (!isset($fields_cached[$entity_type])) {
// Get all fields for this entity type.
$fields = array();
foreach (module_implements('ds_fields_info') as $module) {
$function = $module . '_ds_fields_info';
$all_fields = $function($entity_type);
if (!empty($all_fields)) {
foreach ($all_fields as $key => $field_results) {
if ($key === $entity_type) {
// Add module key to field definition.
foreach ($field_results as $f => $d) {
$field_results[$f]['module'] = $module;
}
$fields = array_merge($field_results, $fields);
}
}
}
}
// Give modules a change to alter fields.
drupal_alter('ds_fields_info', $fields, $entity_type);
$fields_cached[$entity_type] = $fields;
// Cache the fields.
if ($cache) {
cache_set('ds_fields:' . $language->language, $fields_cached, 'cache');
}
}
else {
$fields = $fields_cached[$entity_type];
}
// Store the fields statically.
$static_fields[$entity_type] = $fields;
}
return isset($static_fields[$entity_type]) ? $static_fields[$entity_type] : array();
}
/**
* Get the field settings.
*
* @param $entity_type
* The name of the entity.
* @param $bundle
* The name of bundle (ie, page or story for node types, profile for users)
* @param $view_mode
* The name of view mode.
*/
function ds_get_field_settings($entity_type, $bundle, $view_mode, $default = TRUE) {
static $field_settings = NULL;
if (!isset($field_settings)) {
if ($cache = cache_get('ds_field_settings')) {
$field_settings = $cache->data;
}
else {
ctools_include('export');
$ds_field_settings = ctools_export_crud_load_all('ds_field_settings');
foreach ($ds_field_settings as $layout => $layout_settings) {
// Do not store configuration when the field settings is disabled.
if (!empty($layout_settings->disabled)) {
continue;
}
// Do not store configuration if settings key is not set.
if (!isset($layout_settings->settings)) {
continue;
}
foreach ($layout_settings->settings as $field => $settings) {
$field_settings[$layout_settings->entity_type][$layout_settings->bundle][$layout_settings->view_mode][$field] = $settings;
}
}
cache_set('ds_field_settings', $field_settings, 'cache');
}
}
return (isset($field_settings[$entity_type][$bundle][$view_mode])) ? $field_settings[$entity_type][$bundle][$view_mode] : (isset($field_settings[$entity_type][$bundle]['default']) && $default ? $field_settings[$entity_type][$bundle]['default'] : array());
}
/**
* Get the value for a Display Suite field.
*
* @param $key
* The key of the field.
* @param $field
* The configuration of a DS field.
* @param $entity
* The current entity.
* @param $entity_type
* The name of the entity.
* @param $bundle
* The name of the bundle.
* @param $view_mode
* The name of the view mode.
* @param $build
* The current built of the entity.
* @return $markup
* The markup of the field used for output.
*/
function ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build = array()) {
$field['field_name'] = $key;
$field['entity'] = $entity;
$field['entity_type'] = $entity_type;
$field['bundle'] = $bundle;
$field['view_mode'] = $view_mode;
$field['build'] = $build;
// Special case for ds_views which can handle custom fields now.
if ($field['field_type'] != DS_FIELD_TYPE_PREPROCESS && $entity_type == 'ds_views') {
$entity->preprocess_fields[] = $key;
}
switch ($field['field_type']) {
case DS_FIELD_TYPE_PREPROCESS:
$entity->preprocess_fields[] = $key;
break;
case DS_FIELD_TYPE_FUNCTION:
if (isset($field['file'])) {
include_once $field['file'];
}
return $field['function']($field);
case DS_FIELD_TYPE_THEME:
$format = (isset($field['formatter'])) ? $field['formatter'] : key($field['properties']['formatters']);
return theme($format, $field);
case DS_FIELD_TYPE_CODE:
return ds_render_code_field($field);
case DS_FIELD_TYPE_CTOOLS:
return ds_render_ctools_field($field);
case DS_FIELD_TYPE_BLOCK:
return ds_render_block_field($field);
}
}
/**
* Implements hook_field_attach_view_alter().
*/
function ds_field_attach_view_alter(&$build, $context) {
static $loaded_css = array();
// Global kill switch. In some edge cases, a view might
// be inserted into the view of an entity, in which the
// same entity is available as well. This is simply not
// possible, so you can temporarily disable DS completely
// by setting this variable, either from code or via
// the UI through admin/structure/ds/
if (ds_kill_switch()) {
return;
}
// If views and core doesn't send information along on the entity,
// Display Suite doesn't have a context to render the fields.
if (!isset($build['#entity_type']) && !isset($build['#bundle'])) {
return;
}
// If no layout is configured, stop as well.
if (!ds_get_layout($build['#entity_type'], $build['#bundle'], $context['view_mode'])) {
return;
}
$entity_type = $build['#entity_type'];
$bundle = $build['#bundle'];
$view_mode = $context['view_mode'];
$entity = $context['entity'];
$layout = ds_get_layout($entity_type, $bundle, $view_mode);
// Check on field/delta limit.
if (isset($layout['settings']['limit'])) {
foreach ($layout['settings']['limit'] as $field => $limit) {
if (isset($build[$field])) {
if ($limit === 'delta' && isset($entity->ds_delta) && isset($entity->ds_delta[$field])) {
$delta = $entity->ds_delta[$field];
foreach ($build[$field]['#items'] as $key => $item) {
if ($key != $delta) {
unset($build[$field][$key]);
}
}
}
else {
$count = count($build[$field]['#items']);
if ($count > $limit) {
$build[$field]['#items'] = array_slice($build[$field]['#items'], 0, $limit);
}
}
}
}
}
// Add Display Suite display fields.
$fields = ds_get_fields($entity_type);
$field_values = ds_get_field_settings($entity_type, $bundle, $layout['view_mode']);
foreach ($field_values as $key => $field) {
// Ignore if this field is not a DS field.
if (!isset($fields[$key])) {
continue;
}
$field = $fields[$key];
if (isset($field_values[$key]['format'])) {
$field['formatter'] = $field_values[$key]['format'];
}
if (isset($field_values[$key]['formatter_settings'])) {
$field['formatter_settings'] = $field_values[$key]['formatter_settings'];
}
$field_value = ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build);
// Title label.
if ($key == 'title' && $entity_type == 'node') {
$node_type = node_type_get_type($entity);
$field['title'] = function_exists('i18n_node_translate_type') ? i18n_node_translate_type($node_type->type, 'title_label', $node_type->title_label) : $node_type->title_label;
}
if (!empty($field_value) || (string) $field_value === '0') {
// Special case for views.
if (!empty($build['render_code_fields'])) {
$build[$key] = $field_value;
}
else {
$build[$key] = array(
'#theme' => 'field',
'#field_type' => 'ds',
'#skip_edit' => TRUE,
'#title' => $field['title'],
'#weight' => isset($field_values[$key]['weight']) ? $field_values[$key]['weight'] : 0,
'#label_display' => isset($field_values[$key]['label']) ? $field_values[$key]['label'] : 'inline',
'#field_name' => $key,
'#bundle' => $bundle,
'#object' => $entity,
'#entity_type' => $entity_type,
'#view_mode' => $view_mode,
'#access' => (variable_get('ds_extras_field_permissions', FALSE) && function_exists('ds_extras_ds_field_access')) ? ds_extras_ds_field_access($key, $entity_type) : TRUE,
'#items' => array(
0 => array(
'value' => $field_value,
),
),
0 => array(
'#markup' => $field_value,
),
);
}
}
}
$disable_css = FALSE;
if (!empty($layout['settings']['layout_disable_css'])) {
$disable_css = TRUE;
}
// Add path to css file for this layout and disable block regions if necessary.
if (!$disable_css && isset($layout['css']) && !isset($loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'])) {
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Register css file.
$loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'] = TRUE;
// Add css file.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$build['#attached']['css'][] = $layout['path'] . '/' . $layout['panels']['css'];
}
else {
$build['#attached']['css'][] = $layout['path'] . '/' . $layout['layout'] . '.css';
}
}
}
/**
* Add variables to an entity.
*
* This function is added in ds_theme_registry_alter().
*/
function ds_entity_variables(&$vars) {
if (isset($vars['elements']) && isset($vars['elements']['#bundle']) && $layout = ds_get_layout($vars['elements']['#entity_type'], $vars['elements']['#bundle'], $vars['elements']['#view_mode'])) {
$render_container = 'content';
// User uses user_profile as container.
if ($vars['elements']['#entity_type'] == 'user') {
$render_container = 'user_profile';
}
// Move any preprocess fields to render container.
// Inconsistency in taxonomy term naming.
$entity_type = $vars['elements']['#entity_type'];
// Get entity id and bundle
$id = NULL;
$bundle = $vars['elements']['#bundle'];
$entity = isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : NULL);
list($id,, $bundle) = entity_extract_ids($entity_type, $entity);
if (isset($vars[$entity_type]->preprocess_fields)) {
foreach ($vars[$entity_type]->preprocess_fields as $field) {
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Process RDF if the module is enabled before moving preprocess fields.
if (module_exists('rdf')) {
rdf_process($vars, $field);
// Remove it so we can unset the field later on.
unset($vars['rdf_template_variable_attributes_array'][$field]);
}
// Move the field to content so it renders, remove it
// because we don't need it anymore.
if (isset($vars[$field])) {
$vars[$render_container][$field] = array('#markup' => $vars[$field]);
if (!isset($vars['preprocess_keep'])) {
unset($vars[$field]);
}
}
}
}
// Check if this is a flexible panels layout.
if (!empty($layout['flexible'])) {
ctools_include('plugins', 'panels');
$vars['css_id'] = $vars['settings'] = $vars['display'] = $vars['renderer'] = '';
$vars['layout'] = panels_get_layout($layout['panels']['name']);
$vars['theme_hook_suggestion'] = 'panels_flexible';
}
else {
// Template layout.
if (!isset($vars['classes_array'])) {
$vars['classes_array'] = array();
}
// Add view-mode-{name} to classes.
if (!in_array('view-mode-' . $vars['elements']['#view_mode'], $vars['classes_array'])) {
$vars['classes_array'][] = 'view-mode-' . $vars['elements']['#view_mode'];
}
// In case this is a panels layout, use panels info array.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$layout['layout'] = $layout['panels']['theme'];
}
$vars['theme_hook_suggestions'][] = $layout['layout'];
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'];
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#view_mode'];
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle;
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle . '_' . $vars['elements']['#view_mode'];
if (!empty($id)) {
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '__' . $id;
}
}
// If the layout has wrapper class lets add it.
if (isset($layout['settings']['classes']['layout_class'])) {
foreach ($layout['settings']['classes']['layout_class'] as $layout_class) {
$vars['classes_array'][] = $layout_class;
}
}
// Create region variables based on the layout settings.
foreach ($layout['regions'] as $region_name => $region) {
// Create the region content.
$layout_render_array[$region_name] = array();
if (isset($layout['settings']['regions'][$region_name])) {
foreach ($layout['settings']['regions'][$region_name] as $key => $field) {
// Make sure the field exists.
if (!isset($vars[$render_container][$field])) {
continue;
}
if (!isset($vars[$render_container][$field]['#weight'])) {
$vars[$render_container][$field]['#weight'] = $key;
}
$layout_render_array[$region_name][$key] = $vars[$render_container][$field];
}
}
// Add extras classes to the region.
if (empty($layout['flexible'])) {
$vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
// Token support for region classes.
if (module_exists('token') && isset($vars[$entity_type])) {
$vars[$region_name . '_classes'] = token_replace($vars[$region_name . '_classes'], array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
}
}
// Add a wrapper to the region.
if (empty($layout['flexible'])) {
$vars[$region_name . '_wrapper'] = isset($layout['settings']['wrappers'][$region_name]) ? $layout['settings']['wrappers'][$region_name] : 'div';
}
}
// Let other modules know we have rendered.
$vars['rendered_by_ds'] = TRUE;
// Add a layout wrapper.
$vars['layout_wrapper'] = isset($layout['settings']['layout_wrapper']) ? $layout['settings']['layout_wrapper'] : 'div';
// Add layout attributes if any.
$vars['layout_attributes'] = '';
if (!empty($layout['settings']['layout_attributes'])) {
if (isset($vars[$entity_type])) {
$vars['layout_attributes'] .= ' ' . token_replace($layout['settings']['layout_attributes'], array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
}
else {
$vars['layout_attributes'] .= ' ' . $layout['settings']['layout_attributes'];
}
}
// Merge in other attributes which were passed to the template.
if (!empty($layout['settings']['layout_attributes_merge'])) {
// Handle classes separately.
if (isset($vars['attributes_array']['class'])) {
$vars['classes_array'] = array_unique(array_merge($vars['classes_array'], $vars['attributes_array']['class']));
unset($vars['attributes_array']['class']);
}
$vars['layout_attributes'] .= ' ' . drupal_attributes($vars['attributes_array']);
}
// Token support for layout classes.
$class = token_replace($class, array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
}
}
// Add an onclick attribute on the wrapper.
if (!empty($layout['settings']['layout_link_attribute'])) {
$url = '';
switch ($layout['settings']['layout_link_attribute']) {
case 'content':
$uri = entity_uri($vars['elements']['#entity_type'], $vars['elements']['#account']);
}
else {
$uri = entity_uri($vars['elements']['#entity_type'], $vars[$entity_type]);
}
if (!empty($uri)) {
$url = $uri['path'];
}
break;
case 'custom':
$url = $layout['settings']['layout_link_custom'];
break;
case 'tokens':
$url = token_replace($layout['settings']['layout_link_custom'], array($vars['elements']['#entity_type'] => $vars[$entity_type]), array('clear' => TRUE));
break;
}
if (!empty($url)) {
$vars['layout_attributes'] .= ' onclick="location.href=\'' . url($url) . '\'"';
}
}
// Set field weights for all fields, including pre-process.
foreach ($layout_render_array as $region => &$fields) {
foreach ($fields as $field_key => &$field) {
$field['#weight'] = $field_key;
}
}
// Let other modules alter the ds array before creating the region variables.
$context = array('entity' => isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : ''), 'entity_type' => $vars['elements']['#entity_type'], 'bundle' => $vars['elements']['#bundle'], 'view_mode' => $vars['elements']['#view_mode']);
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
drupal_alter('ds_pre_render', $layout_render_array, $context, $vars);
foreach ($layout_render_array as $region_name => $content) {
// In case this is a panels layout, add the region to the $content variable.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$vars['content'][$region_name] = drupal_render($content);
}
else {
$vars[$region_name] = drupal_render($content);
}
}
}
}
/**
* Create entity context.
*/
function ds_create_entity_context($entity_type, $entity, &$contexts, $context_arguments = array()) {
ctools_include('context');
if (empty($context_arguments)) {
$context_arguments = array(array(
'keyword' => $entity_type,
'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
'id' => 1,
'name' => 'entity_id:' . $entity_type,
'settings' => array(),
));
}
ctools_context_get_context_from_arguments($context_arguments, $contexts, array($entity));
}
/**
* Render a code field.
*/
function ds_render_code_field($field) {
if (isset($field['properties']['code'])) {
$value = $field['properties']['code']['value'];
// Token support - check on token property so we don't run every single field through token.
if (isset($field['properties']['use_token']) && $field['properties']['use_token']) {
$value = token_replace($value, array($field['entity_type'] => $field['entity']), array('clear' => TRUE));
}
$format = (isset($field['properties']['code']['format'])) ? $field['properties']['code']['format'] : 'plain_text';
if ($format == 'ds_code' && module_exists('ds_format')) {
$value = ds_format_php_eval($value, $field['entity'], isset($field['build']) ? $field['build'] : array());
}
return $value;
}
}
/**
* Render a CTools field.
*/
function ds_render_ctools_field($field) {
if (isset($field['formatter_settings']['ctools'])) {
// Extreme use case where a taxonomy_term object is not
// loaded on the entity and triggers notices if a view is embedded
// with taxonomy term fields from the same object.
// see http://drupal.org/node/1238132 - To reproduce:
// 1) add 2 taxonomy field instances on a bundle
// 2) configure a ds layout showing only one
// 3) embed a view with the 2 taxonomies as fields.
if (isset($field['formatter_settings']['load_terms']) && $field['formatter_settings']['load_terms']) {
static $terms_loaded = array();
if (isset($field['entity']->language)) {
$language = $field['entity']->language;
}
else {
$language = LANGUAGE_NONE;
}
$instances = field_info_instances($field['entity_type'], $field['bundle']);
foreach ($instances as $key => $instance) {
$info = field_info_field($key);
if ($info['module'] == 'taxonomy') {
if (empty($field['entity']->{$key})) {
continue;
}
if (!isset($field['entity']->{$key}[$language])) {
$language = LANGUAGE_NONE;
}
foreach ($field['entity']->{$key}[$language] as $tkey => $item) {
if (isset($item['tid']) && !isset($item['taxonomy_term'])) {
if (!isset($terms_loaded[$item['tid']])) {
$term = taxonomy_term_load($item['tid']);
if (!is_object($term)) {
// This term is missing in the database.
continue;
}
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
}
$field['entity']->{$key}[$language][$tkey]['taxonomy_term'] = $terms_loaded[$item['tid']];
}
}
}
}
}
ctools_include('content');
ctools_include('context');
// Get variables.
$show_title = $field['formatter_settings']['show_title'];
$title_wrapper = trim($field['formatter_settings']['title_wrapper']);
$ctools = unserialize($field['formatter_settings']['ctools']);
$type = $ctools['type'];
$subtype = $ctools['subtype'];
$conf = $ctools['conf'];
$entity_type = $field['entity_type'];
$keywords = $arguments = $contexts = array();
// Create entity context.
ds_create_entity_context($entity_type, $field['entity'], $contexts);
// Build the content.
$data = ctools_content_render($type, $subtype, $conf, $keywords, $arguments, $contexts);
// Return content.
if (!empty($data->content)) {
$content = '';
if ($show_title) {
if (empty($title_wrapper)) $title_wrapper = 'div';
$content .= '<' . check_plain($title_wrapper) . ' class="title">' . $data->title . '</' . check_plain($title_wrapper) . '>';
}
if (is_array($data->content)) {
$content .= drupal_render($data->content);
}
else {
$content .= $data->content;
}
return $content;
}
}
}
/**
* Render a block field.
*/
function ds_render_block_field($field) {
list($module, $delta) = explode('|', $field['properties']['block']);
// Load the block
$result = db_select('block', 'b')
->fields('b')->condition('b.theme', $theme_key)->condition('b.module', $module)->condition('b.delta', $delta)->addTag('block_load')->addTag('translatable')->execute();
$block_info = $result->fetchAllAssoc('bid');
// Enable the block
if ($block_info[key($block_info)]->status == 0) {
$block_info[key($block_info)]->status = 1;
// Process the block if it respects visibility settings
if (isset($field['properties']['block_visibility']) && $field['properties']['block_visibility']) {
drupal_alter('block_list', $block_info);
}
// Simulate _block_load_blocks() return, containing only our block.
$block = array_shift($block_info);
// Render the block field
if (isset($block)) {
$key = $module . '_' . $delta;
$region_blocks = _block_render_blocks(array($key => $block));
switch ($field['properties']['block_render']) {
case DS_BLOCK_TEMPLATE:
$renderable_block = _block_get_renderable_array($region_blocks);
return drupal_render($renderable_block);
break;
case DS_BLOCK_TITLE_CONTENT:
if (isset($block->subject) && isset($block->content) && $block->content) {
return '<h2 class="block-title">' . $block->subject . '</h2>' . drupal_render($block->content);
if (isset($block->content) && $block->content) {
return drupal_render($block->content);
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
}
}
}
/**
* Render a field.
*/
function ds_render_field($field) {
$title_field = FALSE;
$output = '';
$settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
$settings += $field['properties']['default'];
// Basic string.
if (isset($settings['link text'])) {
$output = t($settings['link text']);
}
elseif (isset($field['properties']['entity_render_key']) && isset($field['entity']->{$field['properties']['entity_render_key']})) {
if ($field['entity_type'] == 'user' && $field['properties']['entity_render_key'] == 'name') {
$output = format_username($field['entity']);
}
else {
$title_field = $field['properties']['entity_render_key'] == 'title' && $field['entity_type'] == 'node';
$output = $field['entity']->{$field['properties']['entity_render_key']};
}
}
if (empty($output)) {
return;
}