Projet

Général

Profil

Paste
Télécharger (43,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ds / ds.module @ bf6fb0ee

1
<?php
2

    
3
/**
4
 * @file
5
 * Display Suite core functions.
6
 */
7

    
8
/**
9
 * Constants for field types.
10
 */
11
define('DS_FIELD_TYPE_THEME', 1);
12
define('DS_FIELD_TYPE_FUNCTION', 2);
13
define('DS_FIELD_TYPE_PREPROCESS', 3);
14
define('DS_FIELD_TYPE_IGNORE', 4);
15
define('DS_FIELD_TYPE_CODE', 5);
16
define('DS_FIELD_TYPE_BLOCK', 6);
17
define('DS_FIELD_TYPE_CTOOLS', 7);
18

    
19
/**
20
 * Constants for block fields rendering.
21
 */
22
define('DS_BLOCK_TEMPLATE', 1);
23
define('DS_BLOCK_TITLE_CONTENT', 2);
24
define('DS_BLOCK_CONTENT', 3);
25

    
26
/**
27
 * Implements hook_permission().
28
 */
29
function ds_permission() {
30
  return array(
31
    'admin_display_suite' => array(
32
      'title' => t('Administer Display Suite'),
33
      'description' => t('General permission for Display Suite settings pages.')
34
    ),
35
  );
36
}
37

    
38
/**
39
 * Implements hook_hook_info().
40
 */
41
function ds_hook_info() {
42
  $hooks['ds_fields_info'] = array(
43
    'group' => 'ds_fields_info',
44
  );
45
  return $hooks;
46
}
47

    
48
/**
49
 * Implements hook_menu().
50
 */
51
function ds_menu() {
52
  module_load_include('inc', 'ds', 'includes/ds.registry');
53
  return _ds_menu();
54
}
55

    
56
/**
57
 * Implements hook_menu_alter().
58
 */
59
function ds_menu_alter(&$items) {
60
  module_load_include('inc', 'ds', 'includes/ds.registry');
61
  return _ds_menu_alter($items);
62
}
63

    
64
/**
65
 * Implements hook_theme().
66
 */
67
function ds_theme() {
68
  module_load_include('inc', 'ds', 'includes/ds.registry');
69
  return _ds_theme();
70
}
71

    
72
/**
73
 * Implements hook_ds_layout_info().
74
 */
75
function ds_ds_layout_info() {
76
  module_load_include('inc', 'ds', 'includes/ds.registry');
77
  return _ds_ds_layout_info();
78
}
79

    
80
/**
81
 * Implements hook_ctools_plugin_api().
82
 */
83
function ds_ctools_plugin_api($owner, $api) {
84
  if ($owner == 'ds' && ($api == 'ds' || $api == 'plugins')) {
85
    return array('version' => 1);
86
  }
87
}
88

    
89
/**
90
 * Implements hook_ctools_plugin_directory().
91
 */
92
function ds_ctools_plugin_directory($owner, $plugin_type) {
93
  if ($owner == 'ctools' && $plugin_type == 'content_types') {
94
    return 'plugins/' . $plugin_type;
95
  }
96
}
97

    
98
/**
99
 * Implements hook_views_api().
100
 */
101
function ds_views_api() {
102
  return array('api' => 3);
103
}
104

    
105
/**
106
 * Implements hook_node_type_update().
107
 */
108
function ds_node_type_update($info) {
109
  if (!empty($info->old_type) && $info->old_type != $info->type) {
110
    module_load_include('inc', 'ds', 'includes/ds.registry');
111
    _ds_entity_type_update('node', $info, 'update');
112
  }
113
}
114

    
115
/**
116
 * Implements hook_node_type_delete().
117
 */
118
function ds_node_type_delete($info) {
119
  module_load_include('inc', 'ds', 'includes/ds.registry');
120
  _ds_entity_type_update('node', $info, 'delete');
121
}
122

    
123
/**
124
 * Implements hook_theme_registry_alter().
125
 */
126
function ds_theme_registry_alter(&$theme_registry) {
127
  module_load_include('inc', 'ds', 'includes/ds.registry');
128
  _ds_theme_registry_alter($theme_registry);
129
}
130

    
131
/**
132
 * Implements hook_entity_info_alter().
133
 */
134
function ds_entity_info_alter(&$entity_info) {
135
  module_load_include('inc', 'ds', 'includes/ds.registry');
136
  _ds_entity_info_alter($entity_info);
137
}
138

    
139
/**
140
 * Implements hook_form_FORM_ID_alter().
141
 */
142
function ds_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
143
  form_load_include($form_state, 'inc', 'ds', 'includes/ds.field_ui');
144
  // Also load admin on behalf of DS extras when enabled.
145
  if (module_exists('ds_extras')) {
146
    form_load_include($form_state, 'inc', 'ds_extras', 'includes/ds_extras.admin');
147
  }
148
  ds_field_ui_fields_layouts($form, $form_state);
149
}
150

    
151
/**
152
 * Implements hook_module_implements_alter().
153
 */
154
function ds_module_implements_alter(&$implementations, $hook) {
155
  // node_field_display_module_alter() disables all labels on all fields
156
  // when the view mode is 'search_index'. If you set display modes for
157
  // this view mode by hand, then the hook isn't needed. Since this
158
  // may be called hundreds of times on some pages, it's worth disabling it.
159
  // See http://drupal.org/node/834278
160
  // This code is also in Performance hacks module, but it's not bad to
161
  // disable this too in Display Suite by default.
162
  if ($hook == 'field_display_node_alter') {
163
    unset($implementations['node']);
164
  }
165
}
166

    
167
/**
168
 * Implements hook_features_api().
169
 */
170
function ds_features_api() {
171
  module_load_include('inc', 'ds', 'includes/ds.registry');
172
  return _ds_features_api();
173
}
174

    
175
/**
176
 * Function to check if we go on with Display Suite.
177
 */
178
function ds_kill_switch() {
179
  if (variable_get('ds_disable', FALSE)) {
180
    return TRUE;
181
  }
182
  return FALSE;
183
}
184

    
185
/**
186
 * Get entity view modes.
187
 *
188
 * @param $entity_type
189
 *   The name of the entity type.
190
 */
191
function ds_entity_view_modes($entity_type = NULL) {
192
  if (!empty($entity_type)) {
193
    switch ($entity_type) {
194
      // For taxonomy terms the base table and the entity type are different
195
      case 'taxonomy_term_data':
196
        $entity_info = entity_get_info('taxonomy_term');
197
        break;
198
      default:
199
        $entity_info = entity_get_info($entity_type);
200
        break;
201
    }
202
    return $entity_info['view modes'];
203
  }
204
}
205

    
206
/**
207
 * Get Display Suite layouts.
208
 */
209
function ds_get_layout_info() {
210
  static $layouts = FALSE;
211

    
212
  if (!$layouts) {
213
    $errors = array();
214

    
215
    $layouts = module_invoke_all('ds_layout_info');
216

    
217
    // Give modules a chance to alter the layouts information.
218
    drupal_alter('ds_layout_info', $layouts);
219

    
220
    // Check that there is no 'content' region, but ignore panel layouts.
221
    // Because when entities are rendered, the field items are stored into a
222
    // 'content' key so fields would be overwritten before they're all moved.
223
    foreach ($layouts as $key => $info) {
224
      if (isset($info['panels'])) {
225
        continue;
226
      }
227
      if (isset($info['regions']['content'])) {
228
        $errors[] = $key;
229
      }
230
    }
231
    if (!empty($errors)) {
232
      drupal_set_message(t('Following layouts have a "content" region key which is invalid: %layouts.', array('%layouts' => implode(', ', $errors))), 'error');
233
    }
234
  }
235

    
236
  return $layouts;
237
}
238

    
239
/**
240
 * Get a layout for a given entity.
241
 *
242
 * @param $entity_type
243
 *   The name of the entity.
244
 * @param $bundle
245
 *   The name of the bundle.
246
 * @param $view_mode
247
 *   The name of the view mode.
248
 * @param $fallback
249
 *   Whether to fallback to default or not.
250
 *
251
 * @return $layout
252
 *   Array of layout variables for the regions.
253
 */
254
function ds_get_layout($entity_type, $bundle, $view_mode, $fallback = TRUE) {
255

    
256
  static $layouts = array();
257
  $layout_key = $entity_type . '_' . $bundle . '_' . $view_mode;
258

    
259
  if (!isset($layouts[$layout_key])) {
260

    
261
    $entity_info = entity_get_info();
262

    
263
    $overridden = TRUE;
264
    if ($view_mode != 'form') {
265
      $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
266
      $overridden = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE);
267
    }
268
    // Check if a layout is configured.
269
    if (isset($entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode]) && ($overridden || $view_mode == 'default')) {
270
      $layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode];
271
      $layouts[$layout_key]['view_mode'] = $view_mode;
272
    }
273

    
274
    // In case $view_mode is not found, check if we have a default layout,
275
    // but only if the view mode settings aren't overridden for this view mode.
276
    if ($view_mode != 'default' && !$overridden && $fallback && isset($entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'])) {
277
      $layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'];
278
      $layouts[$layout_key]['view_mode'] = 'default';
279
    }
280

    
281
    // Register the false return value as well.
282
    if (!isset($layouts[$layout_key])) {
283
      $layouts[$layout_key] = FALSE;
284
    }
285
  }
286

    
287
  return $layouts[$layout_key];
288
}
289

    
290
/**
291
 * Get all fields.
292
 *
293
 * @param $entity_type
294
 *   The name of the entity.
295
 * @param $cache
296
 *   Whether we need to get the fields from cache or not.
297
 * @return
298
 *   Collection of fields.
299
 */
300
function ds_get_fields($entity_type, $cache = TRUE) {
301
  global $language;
302

    
303
  static $static_fields, $fields_cached = array();
304
  static $loaded = FALSE;
305
  // Load the ds file that handles ds call of hook_ds_fields_info, otherwise it
306
  // doesn't get loaded
307
  module_load_include('inc', 'ds', 'ds.ds_fields_info');
308

    
309
  // Get fields from cache.
310
  if (!$loaded) {
311
    $loaded = TRUE;
312
    if ($cache && $cached_fields = cache_get('ds_fields:' . $language->language)) {
313
      $fields_cached = $static_fields = $cached_fields->data;
314
    }
315
  }
316

    
317
  if (!isset($static_fields[$entity_type])) {
318

    
319
    // Do we have them cached or not ?
320
    if (!isset($fields_cached[$entity_type])) {
321

    
322
      // Get all fields for this entity type.
323
      $fields = array();
324
      foreach (module_implements('ds_fields_info') as $module) {
325
        $function = $module . '_ds_fields_info';
326
        $all_fields = $function($entity_type);
327
        if (!empty($all_fields)) {
328
          foreach ($all_fields as $key => $field_results) {
329
            if ($key === $entity_type) {
330
              // Add module key to field definition.
331
              foreach ($field_results as $f => $d) {
332
                $field_results[$f]['module'] = $module;
333
              }
334
              $fields = array_merge($field_results, $fields);
335
            }
336
          }
337
        }
338
      }
339

    
340
      // Give modules a change to alter fields.
341
      drupal_alter('ds_fields_info', $fields, $entity_type);
342

    
343
      $fields_cached[$entity_type] = $fields;
344

    
345
      // Cache the fields.
346
      if ($cache) {
347
        cache_set('ds_fields:' . $language->language, $fields_cached, 'cache');
348
      }
349
    }
350
    else {
351
      $fields = $fields_cached[$entity_type];
352
    }
353

    
354
    // Store the fields statically.
355
    $static_fields[$entity_type] = $fields;
356
  }
357

    
358
  return isset($static_fields[$entity_type]) ? $static_fields[$entity_type] : array();
359
}
360

    
361
/**
362
 * Get the field settings.
363
 *
364
 * @param $entity_type
365
 *   The name of the entity.
366
 * @param $bundle
367
 *   The name of bundle (ie, page or story for node types, profile for users)
368
 * @param $view_mode
369
 *   The name of view mode.
370
 */
371
function ds_get_field_settings($entity_type, $bundle, $view_mode, $default = TRUE) {
372
  static $field_settings = NULL;
373

    
374
  if (!isset($field_settings)) {
375
    if ($cache = cache_get('ds_field_settings')) {
376
      $field_settings = $cache->data;
377
    }
378
    else {
379
      ctools_include('export');
380
      $ds_field_settings = ctools_export_crud_load_all('ds_field_settings');
381
      foreach ($ds_field_settings as $layout => $layout_settings) {
382
        // Do not store configuration when the field settings is disabled.
383
        if (!empty($layout_settings->disabled)) {
384
          continue;
385
        }
386
        // Do not store configuration if settings key is not set.
387
        if (!isset($layout_settings->settings)) {
388
          continue;
389
        }
390
        foreach ($layout_settings->settings as $field => $settings) {
391
          $field_settings[$layout_settings->entity_type][$layout_settings->bundle][$layout_settings->view_mode][$field] = $settings;
392
        }
393
      }
394
      cache_set('ds_field_settings', $field_settings, 'cache');
395
    }
396
  }
397

    
398
  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());
399
}
400

    
401
/**
402
 * Get the value for a Display Suite field.
403
 *
404
 * @param $key
405
 *   The key of the field.
406
 * @param $field
407
 *   The configuration of a DS field.
408
 * @param $entity
409
 *   The current entity.
410
 * @param $entity_type
411
 *   The name of the entity.
412
 * @param $bundle
413
 *   The name of the bundle.
414
 * @param $view_mode
415
 *   The name of the view mode.
416
 * @param $build
417
 *   The current built of the entity.
418
 * @return $markup
419
 *   The markup of the field used for output.
420
 */
421
function ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build = array()) {
422

    
423
  $field['field_name'] = $key;
424
  $field['entity'] = $entity;
425
  $field['entity_type'] = $entity_type;
426
  $field['bundle'] = $bundle;
427
  $field['view_mode'] = $view_mode;
428
  $field['build'] = $build;
429

    
430
  // Special case for ds_views which can handle custom fields now.
431
  if ($field['field_type'] != DS_FIELD_TYPE_PREPROCESS && $entity_type == 'ds_views') {
432
    $entity->preprocess_fields[] = $key;
433
  }
434

    
435
  switch ($field['field_type']) {
436

    
437
    case DS_FIELD_TYPE_PREPROCESS:
438
      $entity->preprocess_fields[] = $key;
439
      break;
440

    
441
    case DS_FIELD_TYPE_FUNCTION:
442
      if (isset($field['file'])) {
443
        include_once $field['file'];
444
      }
445
      return $field['function']($field);
446

    
447
    case DS_FIELD_TYPE_THEME:
448
      $format = (isset($field['formatter'])) ? $field['formatter'] : key($field['properties']['formatters']);
449
      return theme($format, $field);
450

    
451
    case DS_FIELD_TYPE_CODE:
452
      return ds_render_code_field($field);
453

    
454
    case DS_FIELD_TYPE_CTOOLS:
455
      return ds_render_ctools_field($field);
456

    
457
    case DS_FIELD_TYPE_BLOCK:
458
      return ds_render_block_field($field);
459
  }
460
}
461

    
462
/**
463
 * Implements hook_field_attach_view_alter().
464
 */
465
function ds_field_attach_view_alter(&$build, $context) {
466
  static $loaded_css = array();
467

    
468
  // Global kill switch. In some edge cases, a view might
469
  // be inserted into the view of an entity, in which the
470
  // same entity is available as well. This is simply not
471
  // possible, so you can temporarily disable DS completely
472
  // by setting this variable, either from code or via
473
  // the UI through admin/structure/ds/
474
  if (ds_kill_switch()) {
475
    return;
476
  }
477

    
478
  // If views and core doesn't send information along on the entity,
479
  // Display Suite doesn't have a context to render the fields.
480
  if (!isset($build['#entity_type']) && !isset($build['#bundle'])) {
481
    return;
482
  }
483

    
484
  // If no layout is configured, stop as well.
485
  if (!ds_get_layout($build['#entity_type'], $build['#bundle'], $context['view_mode'])) {
486
    return;
487
  }
488

    
489
  $entity_type = $build['#entity_type'];
490
  $bundle = $build['#bundle'];
491
  $view_mode = $context['view_mode'];
492
  $entity = $context['entity'];
493
  $layout = ds_get_layout($entity_type, $bundle, $view_mode);
494

    
495
  // Check on field/delta limit.
496
  if (isset($layout['settings']['limit'])) {
497
    foreach ($layout['settings']['limit'] as $field => $limit) {
498
      if (isset($build[$field])) {
499
        if ($limit === 'delta' && isset($entity->ds_delta) && isset($entity->ds_delta[$field])) {
500
          $delta = $entity->ds_delta[$field];
501
          foreach ($build[$field]['#items'] as $key => $item) {
502
            if ($key != $delta) {
503
              unset($build[$field][$key]);
504
            }
505
          }
506
        }
507
        else {
508
          $count = count($build[$field]['#items']);
509
          if ($count > $limit) {
510
            $build[$field]['#items'] = array_slice($build[$field]['#items'], 0, $limit);
511
          }
512
        }
513
      }
514
    }
515
  }
516

    
517
  // Add Display Suite display fields.
518
  $fields = ds_get_fields($entity_type);
519
  $field_values = ds_get_field_settings($entity_type, $bundle, $layout['view_mode']);
520

    
521
  foreach ($field_values as $key => $field) {
522

    
523
    // Ignore if this field is not a DS field.
524
    if (!isset($fields[$key])) {
525
      continue;
526
    }
527

    
528
    $field = $fields[$key];
529
    if (isset($field_values[$key]['format'])) {
530
      $field['formatter'] = $field_values[$key]['format'];
531
    }
532

    
533
    if (isset($field_values[$key]['formatter_settings'])) {
534
      $field['formatter_settings'] = $field_values[$key]['formatter_settings'];
535
    }
536
    $field_value = ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build);
537

    
538
    // Title label.
539
    if ($key == 'title' && $entity_type == 'node') {
540
      $node_type = node_type_get_type($entity);
541
      $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;
542
    }
543

    
544
    if (!empty($field_value) || (string) $field_value === '0') {
545

    
546
      // Special case for views.
547
      if (!empty($build['render_code_fields'])) {
548
        $build[$key] = $field_value;
549
      }
550
      else {
551
        $build[$key] = array(
552
          '#theme' => 'field',
553
          '#field_type' => 'ds',
554
          '#skip_edit' => TRUE,
555
          '#title' => $field['title'],
556
          '#weight' => isset($field_values[$key]['weight']) ? $field_values[$key]['weight'] : 0,
557
          '#label_display' => isset($field_values[$key]['label']) ? $field_values[$key]['label'] : 'inline',
558
          '#field_name' => $key,
559
          '#bundle' => $bundle,
560
          '#object' => $entity,
561
          '#entity_type' => $entity_type,
562
          '#view_mode' => $view_mode,
563
          '#access' => (variable_get('ds_extras_field_permissions', FALSE) && function_exists('ds_extras_ds_field_access')) ? ds_extras_ds_field_access($key, $entity_type) : TRUE,
564
          '#items' => array(
565
            0 => array(
566
              'value' => $field_value,
567
            ),
568
          ),
569
          0 => array(
570
            '#markup' => $field_value,
571
          ),
572
        );
573
      }
574
    }
575
  }
576

    
577
  $disable_css = FALSE;
578
  if (!empty($layout['settings']['layout_disable_css'])) {
579
    $disable_css = TRUE;
580
  }
581

    
582
  // Add path to css file for this layout and disable block regions if necessary.
583
  if (!$disable_css && isset($layout['css']) && !isset($loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'])) {
584
    // Register css file.
585
    $loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'] = TRUE;
586
    // Add css file.
587
    if (isset($layout['module']) && $layout['module'] == 'panels') {
588
      $build['#attached']['css'][] = $layout['path'] . '/' . $layout['panels']['css'];
589
    }
590
    else {
591
      $build['#attached']['css'][] = $layout['path'] . '/' . $layout['layout'] . '.css';
592
    }
593
  }
594
}
595

    
596
/**
597
 * Add variables to an entity.
598
 *
599
 * This function is added in ds_theme_registry_alter().
600
 */
601
function ds_entity_variables(&$vars) {
602
  if (isset($vars['elements']) && isset($vars['elements']['#bundle']) && $layout = ds_get_layout($vars['elements']['#entity_type'], $vars['elements']['#bundle'], $vars['elements']['#view_mode'])) {
603

    
604
    $render_container = 'content';
605
    // User uses user_profile as container.
606
    if ($vars['elements']['#entity_type'] == 'user') {
607
      $render_container = 'user_profile';
608
    }
609

    
610
    // Move any preprocess fields to render container.
611
    // Inconsistency in taxonomy term naming.
612
    $entity_type = $vars['elements']['#entity_type'];
613
    if ($vars['elements']['#entity_type'] == 'taxonomy_term') {
614
      $entity_type = 'term';
615
    }
616

    
617
    // Get entity id and bundle
618
    $id = NULL;
619
    $bundle = $vars['elements']['#bundle'];
620
    $entity = isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : NULL);
621
    list($id,, $bundle) = entity_extract_ids($entity_type, $entity);
622

    
623
    if (isset($vars[$entity_type]->preprocess_fields)) {
624
      foreach ($vars[$entity_type]->preprocess_fields as $field) {
625

    
626
        // Process RDF if the module is enabled before moving preprocess fields.
627
        if (module_exists('rdf')) {
628
          rdf_process($vars, $field);
629
          // Remove it so we can unset the field later on.
630
          unset($vars['rdf_template_variable_attributes_array'][$field]);
631
        }
632

    
633
        // Move the field to content so it renders, remove it
634
        // because we don't need it anymore.
635
        if (isset($vars[$field])) {
636
          $vars[$render_container][$field] = array('#markup' => $vars[$field]);
637
          if (!isset($vars['preprocess_keep'])) {
638
            unset($vars[$field]);
639
          }
640
        }
641
      }
642
    }
643

    
644
    // Check if this is a flexible panels layout.
645
    if (!empty($layout['flexible'])) {
646
      ctools_include('plugins', 'panels');
647
      $vars['css_id'] = $vars['settings'] = $vars['display'] =  $vars['renderer'] = '';
648
      $vars['layout'] = panels_get_layout($layout['panels']['name']);
649
      $vars['theme_hook_suggestion'] = 'panels_flexible';
650
    }
651
    else {
652
      // Template layout.
653
      if (!isset($vars['classes_array'])) {
654
        $vars['classes_array'] = array();
655
      }
656

    
657
      // Add view-mode-{name} to classes.
658
      if (!in_array('view-mode-' . $vars['elements']['#view_mode'], $vars['classes_array'])) {
659
        $vars['classes_array'][] = 'view-mode-' . $vars['elements']['#view_mode'];
660
      }
661

    
662
      // In case this is a panels layout, use panels info array.
663
      if (isset($layout['module']) && $layout['module'] == 'panels') {
664
        $layout['layout'] = $layout['panels']['theme'];
665
      }
666

    
667
      $bundle = strtr($bundle, '-', '_');
668
      $vars['theme_hook_suggestions'][] = $layout['layout'];
669
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'];
670
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#view_mode'];
671
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle;
672
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle . '_' . $vars['elements']['#view_mode'];
673
      if (!empty($id)) {
674
        $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '__' . $id;
675
      }
676
    }
677

    
678
    // If the layout has wrapper class lets add it.
679
    if (isset($layout['settings']['classes']['layout_class'])) {
680
      foreach ($layout['settings']['classes']['layout_class'] as $layout_class) {
681
        $vars['classes_array'][] = $layout_class;
682
      }
683
    }
684

    
685
    $layout_render_array = array();
686
    // Create region variables based on the layout settings.
687
    foreach ($layout['regions'] as $region_name => $region) {
688

    
689
      // Create the region content.
690
      $layout_render_array[$region_name] = array();
691
      if (isset($layout['settings']['regions'][$region_name])) {
692
        foreach ($layout['settings']['regions'][$region_name] as $key => $field) {
693
          // Make sure the field exists.
694
          if (!isset($vars[$render_container][$field])) {
695
            continue;
696
          }
697
          if (!isset($vars[$render_container][$field]['#weight'])) {
698
            $vars[$render_container][$field]['#weight'] = $key;
699
          }
700
          $layout_render_array[$region_name][$key] = $vars[$render_container][$field];
701
        }
702
      }
703

    
704
      // Add extras classes to the region.
705
      if (empty($layout['flexible'])) {
706
        $vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
707

    
708
        // Token support for region classes.
709
        if (module_exists('token') && isset($vars[$entity_type])) {
710
          $vars[$region_name . '_classes'] = token_replace($vars[$region_name . '_classes'], array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
711
        }
712
      }
713
      // Add a wrapper to the region.
714
      if (empty($layout['flexible'])) {
715
        $vars[$region_name . '_wrapper'] = isset($layout['settings']['wrappers'][$region_name]) ? $layout['settings']['wrappers'][$region_name] : 'div';
716
      }
717
    }
718

    
719
    // Let other modules know we have rendered.
720
    $vars['rendered_by_ds'] = TRUE;
721

    
722
    // Add a layout wrapper.
723
    $vars['layout_wrapper'] = isset($layout['settings']['layout_wrapper']) ? $layout['settings']['layout_wrapper'] : 'div';
724

    
725
    // Add layout attributes if any.
726
    $vars['layout_attributes'] = '';
727
    if (!empty($layout['settings']['layout_attributes'])) {
728
      if (isset($vars[$entity_type])) {
729
        $vars['layout_attributes'] .= ' ' . token_replace($layout['settings']['layout_attributes'], array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
730
      }
731
      else {
732
        $vars['layout_attributes'] .= ' ' . $layout['settings']['layout_attributes'];
733
      }
734
    }
735
    // Merge in other attributes which were passed to the template.
736
    if (!empty($layout['settings']['layout_attributes_merge'])) {
737
      // Handle classes separately.
738
      if (isset($vars['attributes_array']['class'])) {
739
        $vars['classes_array'] = array_unique(array_merge($vars['classes_array'], $vars['attributes_array']['class']));
740
        unset($vars['attributes_array']['class']);
741
      }
742
      $vars['layout_attributes'] .= ' ' . drupal_attributes($vars['attributes_array']);
743
    }
744

    
745
    // Token support for layout classes.
746
    if (module_exists('token') && isset($vars[$entity_type])) {
747
      foreach ($vars['classes_array'] as &$class) {
748
        $class = token_replace($class, array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
749
      }
750
    }
751

    
752
    // Add an onclick attribute on the wrapper.
753
    if (!empty($layout['settings']['layout_link_attribute'])) {
754
      $url = '';
755
      switch ($layout['settings']['layout_link_attribute']) {
756
        case 'content':
757
          if ($entity_type == 'user') {
758
            $uri = entity_uri($vars['elements']['#entity_type'], $vars['elements']['#account']);
759
          }
760
          else {
761
            $uri = entity_uri($vars['elements']['#entity_type'], $vars[$entity_type]);
762
          }
763
          if (!empty($uri)) {
764
            $url = $uri['path'];
765
          }
766
          break;
767
        case 'custom':
768
          $url = $layout['settings']['layout_link_custom'];
769
          break;
770
        case 'tokens':
771
          $url = token_replace($layout['settings']['layout_link_custom'], array($vars['elements']['#entity_type'] => $vars[$entity_type]), array('clear' => TRUE));
772
          break;
773
      }
774

    
775
      if (!empty($url)) {
776
        $vars['layout_attributes'] .= ' onclick="location.href=\'' . url($url) . '\'"';
777
      }
778
    }
779

    
780
    // Set field weights for all fields, including pre-process.
781
    foreach ($layout_render_array as $region => &$fields) {
782
      foreach ($fields as $field_key => &$field) {
783
        $field['#weight'] = $field_key;
784
      }
785
    }
786

    
787
    // Let other modules alter the ds array before creating the region variables.
788
    $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
    drupal_alter('ds_pre_render', $layout_render_array, $context, $vars);
790
    foreach ($layout_render_array as $region_name => $content) {
791
      // In case this is a panels layout, add the region to the $content variable.
792
      if (isset($layout['module']) && $layout['module'] == 'panels') {
793
        $vars['content'][$region_name] = drupal_render($content);
794
      }
795
      else {
796
        $vars[$region_name] = drupal_render($content);
797
      }
798
    }
799
  }
800
}
801

    
802
/**
803
 * Create entity context.
804
 */
805
function ds_create_entity_context($entity_type, $entity, &$contexts, $context_arguments = array()) {
806
  ctools_include('context');
807
  if (empty($context_arguments)) {
808
    $context_arguments = array(array(
809
      'keyword' => $entity_type,
810
      'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
811
      'id' => 1,
812
      'name' => 'entity_id:' . $entity_type,
813
      'settings' => array(),
814
    ));
815
  }
816
  ctools_context_get_context_from_arguments($context_arguments, $contexts, array($entity));
817
}
818

    
819
/**
820
 * Render a code field.
821
 */
822
function ds_render_code_field($field) {
823
  if (isset($field['properties']['code'])) {
824
    $value = $field['properties']['code']['value'];
825
    // Token support - check on token property so we don't run every single field through token.
826
    if (isset($field['properties']['use_token']) && $field['properties']['use_token']) {
827
      $value = token_replace($value, array($field['entity_type'] => $field['entity']), array('clear' => TRUE));
828
    }
829
    $format = (isset($field['properties']['code']['format'])) ? $field['properties']['code']['format'] : 'plain_text';
830
    if ($format == 'ds_code' && module_exists('ds_format')) {
831
      $value = ds_format_php_eval($value, $field['entity'], isset($field['build']) ? $field['build'] : array());
832
    }
833
    else {
834
      $value = check_markup($value, $format);
835
    }
836
    return $value;
837
  }
838
}
839

    
840
/**
841
 * Render a CTools field.
842
 */
843
function ds_render_ctools_field($field) {
844
  if (isset($field['formatter_settings']['ctools'])) {
845

    
846
    // Extreme use case where a taxonomy_term object is not
847
    // loaded on the entity and triggers notices if a view is embedded
848
    // with taxonomy term fields from the same object.
849
    // see http://drupal.org/node/1238132 - To reproduce:
850
    // 1) add 2 taxonomy field instances on a bundle
851
    // 2) configure a ds layout showing only one
852
    // 3) embed a view with the 2 taxonomies as fields.
853
    if (isset($field['formatter_settings']['load_terms']) && $field['formatter_settings']['load_terms']) {
854
      static $terms_loaded = array();
855
      if (isset($field['entity']->language)) {
856
        $language = $field['entity']->language;
857
      }
858
      else {
859
        $language = LANGUAGE_NONE;
860
      }
861
      $instances = field_info_instances($field['entity_type'], $field['bundle']);
862
      foreach ($instances as $key => $instance) {
863
        $info = field_info_field($key);
864
        if ($info['module'] == 'taxonomy') {
865
          if (empty($field['entity']->{$key})) {
866
            continue;
867
          }
868
          if (!isset($field['entity']->{$key}[$language])) {
869
            $language = LANGUAGE_NONE;
870
          }
871
          foreach ($field['entity']->{$key}[$language] as $tkey => $item) {
872
            if (isset($item['tid']) && !isset($item['taxonomy_term'])) {
873
              if (!isset($terms_loaded[$item['tid']])) {
874
                $term = taxonomy_term_load($item['tid']);
875
                if (!is_object($term)) {
876
                  // This term is missing in the database.
877
                  continue;
878
                }
879
                $terms_loaded[$item['tid']] = $term;
880
              }
881
              $field['entity']->{$key}[$language][$tkey]['taxonomy_term'] = $terms_loaded[$item['tid']];
882
            }
883
          }
884
        }
885
      }
886
    }
887

    
888
    ctools_include('content');
889
    ctools_include('context');
890

    
891
    // Get variables.
892
    $show_title = $field['formatter_settings']['show_title'];
893
    $title_wrapper = trim($field['formatter_settings']['title_wrapper']);
894
    $ctools = unserialize($field['formatter_settings']['ctools']);
895
    $type = $ctools['type'];
896
    $subtype = $ctools['subtype'];
897
    $conf = $ctools['conf'];
898
    $entity_type = $field['entity_type'];
899
    $keywords = $arguments = $contexts = array();
900

    
901
    // Create entity context.
902
    ds_create_entity_context($entity_type, $field['entity'], $contexts);
903

    
904
    // Build the content.
905
    $data = ctools_content_render($type, $subtype, $conf, $keywords, $arguments, $contexts);
906
    // Return content.
907
    if (!empty($data->content)) {
908
      $content = '';
909
      if ($show_title) {
910
        if (empty($title_wrapper)) $title_wrapper = 'div';
911
        $content .= '<' . check_plain($title_wrapper) . ' class="title">' . $data->title . '</' . check_plain($title_wrapper) . '>';
912
      }
913
      if (is_array($data->content)) {
914
        $content .= drupal_render($data->content);
915
      }
916
      else {
917
        $content .= $data->content;
918
      }
919
      return $content;
920
    }
921
  }
922
}
923

    
924
/**
925
 * Render a block field.
926
 */
927
function ds_render_block_field($field) {
928
  global $theme_key;
929
  list($module, $delta) = explode('|', $field['properties']['block']);
930

    
931
  // Load the block
932
  $result = db_select('block', 'b')
933
    ->fields('b')->condition('b.theme', $theme_key)->condition('b.module', $module)->condition('b.delta', $delta)->addTag('block_load')->addTag('translatable')->execute();
934
  $block_info = $result->fetchAllAssoc('bid');
935

    
936
  // Enable the block
937
  if ($block_info[key($block_info)]->status == 0) {
938
    $block_info[key($block_info)]->status = 1;
939
  }
940

    
941
  // Process the block if it respects visibility settings
942
  if (isset($field['properties']['block_visibility']) && $field['properties']['block_visibility']) {
943
    drupal_alter('block_list', $block_info);
944
  }
945

    
946
  // Simulate _block_load_blocks() return, containing only our block.
947
  $block = array_shift($block_info);
948

    
949
  // Render the block field
950
  if (isset($block)) {
951
    $key = $module . '_' . $delta;
952
    if (module_exists('block')) {
953
      $region_blocks = _block_render_blocks(array($key => $block));
954
      switch ($field['properties']['block_render']) {
955
        case DS_BLOCK_TEMPLATE:
956
          $renderable_block = _block_get_renderable_array($region_blocks);
957
          return drupal_render($renderable_block);
958
          break;
959
        case DS_BLOCK_TITLE_CONTENT:
960
          if (isset($block->subject) && isset($block->content) && $block->content) {
961
            return '<h2 class="block-title">' . $block->subject . '</h2>' . drupal_render($block->content);
962
          }
963
          break;
964
        case DS_BLOCK_CONTENT:
965
          if (isset($block->content) && $block->content) {
966
            return drupal_render($block->content);
967
          }
968
          break;
969
      }
970
    }
971
  }
972
}
973

    
974
/**
975
 * Render a field.
976
 */
977
function ds_render_field($field) {
978
  $title_field = FALSE;
979

    
980
  $output = '';
981
  $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
982
  $settings += $field['properties']['default'];
983

    
984
  // Basic string.
985
  if (isset($settings['link text'])) {
986
    $output = t($settings['link text']);
987
  }
988
  elseif (isset($field['properties']['entity_render_key']) && isset($field['entity']->{$field['properties']['entity_render_key']})) {
989
    if ($field['entity_type'] == 'user' && $field['properties']['entity_render_key'] == 'name') {
990
      $output = format_username($field['entity']);
991
    }
992
    else {
993
      $title_field = $field['properties']['entity_render_key'] == 'title' && $field['entity_type'] == 'node';
994
      $output = $field['entity']->{$field['properties']['entity_render_key']};
995
    }
996
  }
997

    
998
  if (empty($output)) {
999
    return;
1000
  }
1001

    
1002
  // Link.
1003
  if ($settings['link']) {
1004
    if (isset($field['entity']->uri)) {
1005
      $uri_info = $field['entity']->uri;
1006
    }
1007
    else {
1008
      $uri_info = entity_uri($field['entity_type'], $field['entity']);
1009
    }
1010
    if (isset($settings['link class'])) {
1011
      $uri_info['options']['attributes']['class'][] = $settings['link class'];
1012
    }
1013
    $output = l($output, $uri_info['path'], $uri_info['options']);
1014
    if ($title_field) {
1015
      $output = ds_edit_support('title', $output, $field);
1016
    }
1017
  }
1018
  else {
1019
    $output = check_plain($output);
1020
    if ($title_field) {
1021
      $output = ds_edit_support('title', $output, $field);
1022
    }
1023
  }
1024

    
1025
  // Wrapper and class.
1026
  if (!empty($settings['wrapper'])) {
1027
    $wrapper = check_plain($settings['wrapper']);
1028
    $class = (!empty($settings['class'])) ? ' class="' . check_plain($settings['class']) . '"' : '';
1029
    $output = '<' . $wrapper . $class . '>' . $output . '</' . $wrapper . '>';
1030
  }
1031

    
1032
  return $output;
1033
}
1034

    
1035
/**
1036
 * Support for edit module.
1037
 *
1038
 * @param $field_name
1039
 *   The name of the field.
1040
 * @param $output
1041
 *   The output of the field.
1042
 * @param $field
1043
 *   The complete field array.
1044
 *
1045
 * @return
1046
 *   The field ready for edit module or the same value in case
1047
 *   the edit module is not enabled.
1048
 */
1049
function ds_edit_support($field_name, $output, $field) {
1050

    
1051
  if (module_exists('edit')) {
1052
    $edit_id = "node/" . $field['entity']->nid . "/" . $field_name . "/" . $field['entity']->language . "/" . $field['view_mode'];
1053
    $output = edit_wrap_pseudofield($output, $edit_id);
1054
  }
1055

    
1056
  return $output;
1057
}
1058

    
1059
/**
1060
 * Render an author field.
1061
 */
1062
function ds_render_author_field($field) {
1063

    
1064
  // Users without a user name are anonymous users. These are never linked.
1065
  if (empty($field['entity']->name)) {
1066
    $output = check_plain(variable_get('anonymous', t('Anonymous')));
1067
  }
1068

    
1069
  if ($field['formatter'] == 'author') {
1070
    $output = format_username($field['entity']);
1071
  }
1072

    
1073
  if ($field['formatter'] == 'author_linked') {
1074
    $output = theme('username', array('account' => $field['entity']));
1075
  }
1076

    
1077
  return ds_edit_support('author', $output, $field);
1078
}
1079

    
1080
/**
1081
 * Render a markup field.
1082
 */
1083
function ds_render_markup($field) {
1084
  if (isset($field['entity']->{$field['properties']['key']})) {
1085
    // Check for format, and let's take filtered_html as a sane default.
1086
    $format = isset($field['entity']->{$field['properties']['format']}) ? $field['entity']->{$field['properties']['format']} : 'filtered_html';
1087
    return check_markup($field['entity']->{$field['properties']['key']}, $format, '', TRUE);;
1088
  }
1089
}
1090

    
1091
/**
1092
 * Return the picture.
1093
 */
1094
function ds_return_picture($entity) {
1095

    
1096
  // Gravatar support.
1097
  if (module_exists('gravatar')) {
1098
    $entity = _gravatar_load_account($entity);
1099
    $entity->picture = _gravatar_get_account_user_picture($entity);
1100
  }
1101

    
1102
  if (!empty($entity->picture)) {
1103
    if (is_numeric($entity->picture)) {
1104
      return file_load($entity->picture);
1105
    }
1106
    else {
1107
      return $entity->picture;
1108
    }
1109
  }
1110
  elseif (variable_get('user_picture_default', '')) {
1111
    return variable_get('user_picture_default', '');
1112
  }
1113
}
1114

    
1115
/**
1116
 * Render a user picture.
1117
 */
1118
function ds_render_user_picture($field) {
1119
  $picture = ds_return_picture($field['entity']);
1120

    
1121
  if (!empty($picture)) {
1122
    $filepath = (isset($picture->uri)) ? $picture->uri : $picture;
1123
    $name = format_username($field['entity']);
1124
    $alt = t("@user's picture", array('@user' => $name));
1125
    $vars = array('path' => $filepath, 'alt' => $alt, 'title' => $alt);
1126

    
1127
    // If the image does not have a valid Drupal scheme (for eg. HTTP),
1128
    // don't load image styles.
1129
    if (module_exists('image') && file_valid_uri($filepath)) {
1130
      $vars['style_name'] = str_replace('ds_picture_', '', $field['formatter']);
1131
      $image = theme('image_style', $vars);
1132
    }
1133
    else {
1134
      $image = theme('image', $vars);
1135
    }
1136

    
1137
    if (!empty($field['entity']->uid) && user_access('access user profiles')) {
1138
      return l($image, 'user/' . $field['entity']->uid, array('html' => TRUE));
1139
    }
1140
    else {
1141
      return $image;
1142
    }
1143
  }
1144
}
1145

    
1146
/**
1147
 * Render a date field.
1148
 */
1149
function ds_render_date_field($field) {
1150
  $date_format = str_replace('ds_post_date_', '', $field['formatter']);
1151
  return ds_edit_support($field['properties']['entity_render_key'], format_date($field['entity']->{$field['properties']['entity_render_key']}, $date_format), $field);
1152
}
1153

    
1154
/**
1155
 * Render a "Submitted by"-line.
1156
 */
1157
function ds_render_submitted_by($field) {
1158
  $account = user_load($field['entity']->uid);
1159
  switch ($field['formatter']) {
1160
    case 'ds_time_ago':
1161
      $interval = REQUEST_TIME - $field['entity']->created;
1162
      return t('Submitted !interval ago by !user.', array('!interval' => format_interval($interval), '!user' => theme('username', array('account' => $account))));
1163
    default:
1164
      $date_format = str_replace('ds_post_date_', '', $field['formatter']);
1165
      return t('Submitted by !user on !date.', array('!user' => theme('username', array('account' => $account)), '!date' => format_date($field['entity']->created, $date_format)));
1166
  }
1167
}
1168

    
1169
/**
1170
 * Implements hook_field_formatter_info().
1171
 */
1172
function ds_field_formatter_info() {
1173

    
1174
  $formatters = array();
1175
  if (module_exists('taxonomy')) {
1176
    $formatters['ds_taxonomy_view_mode'] = array(
1177
      'label'       => t('Rendered taxonomy term'),
1178
      'description' => t('Display the referenced term in a specific view mode'),
1179
      'field types' => array('taxonomy_term_reference'),
1180
      'settings'    => array(
1181
        'taxonomy_term_reference_view_mode' => 'full',
1182
        'use_content_language' => TRUE,
1183
      ),
1184
    );
1185
    $formatters['ds_taxonomy_separator'] = array(
1186
      'label'       => t('Separated'),
1187
      'description' => t('Display the referenced term with a separator.'),
1188
      'field types' => array('taxonomy_term_reference'),
1189
      'settings'    => array(
1190
        'taxonomy_term_link' => TRUE,
1191
        'taxonomy_term_separator' => ', ',
1192
      ),
1193
    );
1194

    
1195
    if (module_exists('i18n_taxonomy')) {
1196
      $formatters['ds_taxonomy_separator_localized'] = array(
1197
        'label'       => t('Separated (localized)'),
1198
        'description' => t('Display the referenced term with a separator. Use this with the "localize" translation mode for vocabularies.'),
1199
        'field types' => array('taxonomy_term_reference'),
1200
        'settings'    => array(
1201
          'taxonomy_term_link' => TRUE,
1202
          'taxonomy_term_separator' => ', ',
1203
        ),
1204
      );
1205
    }
1206
  }
1207

    
1208
  return $formatters;
1209
}
1210

    
1211
/**
1212
 * Implements hook_field_formatter_view().
1213
 */
1214
function ds_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
1215
  global $language;
1216
  $element = array();
1217

    
1218
  if ($display['type'] === 'ds_taxonomy_view_mode') {
1219
    $view_mode = $display['settings']['taxonomy_term_reference_view_mode'];
1220
    foreach ($items as $delta => $item) {
1221
      if ($item['tid'] == 'autocreate') {
1222
        // We don't necessarily have a link when this is
1223
        // an autocreated term, usually in preview.
1224
        // So just send the term as check plained markup.
1225
        $build['#markup'] = check_plain($item['name']);
1226
      }
1227
      else {
1228
        $term = taxonomy_term_load($item['tid']);
1229
        if (module_exists('i18n_taxonomy')) {
1230
          $term = i18n_taxonomy_localize_terms($term);
1231
        }
1232
        if (!is_object($term)) {
1233
          // This term is missing in the database.
1234
          continue;
1235
        }
1236
        $settings = $display['settings'];
1237
        if ($settings['use_content_language'] && !empty($GLOBALS['language_content']->language)) {
1238
          $langcode = $GLOBALS['language_content']->language;
1239
        }
1240
        if (!empty($term)) {
1241
          $build = taxonomy_term_view($term, $view_mode, $langcode);
1242
        }
1243
        else {
1244
          $build['#markup'] = '';
1245
        }
1246
      }
1247
      $element[$delta] = $build;
1248
    }
1249
  }
1250

    
1251
  if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
1252
    $linked = $display['settings']['taxonomy_term_link'];
1253

    
1254
    $terms = array();
1255
    foreach ($items as $delta => $item) {
1256
      if ($item['tid'] == 'autocreate') {
1257
        // We don't necessarily have a link when this is
1258
        // an autocreated term, usually in preview.
1259
        // So just send the term as check plained markup.
1260
        $item_display = check_plain($item['name']);
1261
      }
1262
      else {
1263
        $term = taxonomy_term_load($item['tid']);
1264
        if (module_exists('i18n_taxonomy')) {
1265
          $term = i18n_taxonomy_localize_terms($term);
1266
        }
1267
        if (!is_object($term)) {
1268
          // This term is missing in the database.
1269
          continue;
1270
        }
1271
        if ($display['type'] == 'ds_taxonomy_separator_localized' && function_exists('i18n_taxonomy_term_name')) {
1272
          $term->name = i18n_taxonomy_term_name($term, $language->language);
1273
        }
1274
        $item_display = check_plain($term->name);
1275
        if ($linked) {
1276
          $uri = entity_uri('taxonomy_term', $term);
1277
          $item_display = l($term->name, $uri['path'], $uri['options']);
1278
        }
1279
      }
1280
      $terms[] = $item_display;
1281
    }
1282

    
1283
    if (!empty($terms)) {
1284
      $output = '';
1285
      $separator = $display['settings']['taxonomy_term_separator'];
1286
      $output = implode($separator, $terms);
1287
      $element[0] = array('#markup' => $output);
1288
    }
1289
  }
1290

    
1291
  return $element;
1292
}
1293

    
1294
/**
1295
 * Implements hook_contextual_links_view_alter().
1296
 */
1297
function ds_contextual_links_view_alter(&$element, $items) {
1298

    
1299
  if (!empty($element['#element']['#entity_type']) && !empty($element['#element']['#bundle']) && module_exists('field_ui') && user_access('administer content types')) {
1300
    $entity_type = $element['#element']['#entity_type'];
1301
    $bundle = $element['#element']['#bundle'];
1302
    $view_mode = isset($element['#element']['#view_mode']) ? $element['#element']['#view_mode'] : 'default';
1303

    
1304
    // Get the manage display URI.
1305
    $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
1306

    
1307
    // Check view mode settings.
1308
    $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
1309
    $overridden = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE);
1310

    
1311
    if (!$overridden) {
1312
      $admin_path .= '/display';
1313
    }
1314
    else {
1315
      $admin_path .= '/display/' . $view_mode;
1316
    }
1317

    
1318
    $element['#links']['manage-display'] = array(
1319
      'title' => t('Manage display'),
1320
      'href' => $admin_path,
1321
      'query' => drupal_get_destination(),
1322
    );
1323
  }
1324
}