Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ds / ds.module @ 6e9292aa

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
    // Inconsitency in taxonomy term naming.
612
    $object = $vars['elements']['#entity_type'];
613
    if ($vars['elements']['#entity_type'] == 'taxonomy_term') {
614
      $object = 'term';
615
    }
616
    if (isset($vars[$object]->preprocess_fields)) {
617
      foreach ($vars[$object]->preprocess_fields as $field) {
618

    
619
        // Process RDF if the module is enabled before moving preprocess fields.
620
        if (module_exists('rdf')) {
621
          rdf_process($vars, $field);
622
          // Remove it so we can unset the field later on.
623
          unset($vars['rdf_template_variable_attributes_array'][$field]);
624
        }
625

    
626
        // Move the field to content so it renders, remove it
627
        // because we don't need it anymore.
628
        if (isset($vars[$field])) {
629
          $vars[$render_container][$field] = array('#markup' => $vars[$field]);
630
          if (!isset($vars['preprocess_keep'])) {
631
            unset($vars[$field]);
632
          }
633
        }
634
      }
635
    }
636

    
637
    // Check if this is a flexible panels layout.
638
    if (!empty($layout['flexible'])) {
639
      ctools_include('plugins', 'panels');
640
      $vars['css_id'] = $vars['settings'] = $vars['display'] =  $vars['renderer'] = '';
641
      $vars['layout'] = panels_get_layout($layout['panels']['name']);
642
      $vars['theme_hook_suggestion'] = 'panels_flexible';
643
    }
644
    else {
645
      // Template layout.
646
      if (!isset($vars['classes_array'])) {
647
        $vars['classes_array'] = array();
648
      }
649

    
650
      // Add view-mode-{name} to classes.
651
      if (!in_array('view-mode-' . $vars['elements']['#view_mode'], $vars['classes_array'])) {
652
        $vars['classes_array'][] = 'view-mode-' . $vars['elements']['#view_mode'];
653
      }
654

    
655
      // In case this is a panels layout, use panels info array.
656
      if (isset($layout['module']) && $layout['module'] == 'panels') {
657
        $layout['layout'] = $layout['panels']['theme'];
658
      }
659

    
660
      $bundle = strtr($vars['elements']['#bundle'], '-', '_');
661
      $vars['theme_hook_suggestions'][] = $layout['layout'];
662
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'];
663
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#view_mode'];
664
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle;
665
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle . '_' . $vars['elements']['#view_mode'];
666
    }
667

    
668
    // If the layout has wrapper class lets add it.
669
    if (isset($layout['settings']['classes']['layout_class'])) {
670
      foreach ($layout['settings']['classes']['layout_class'] as $layout_class) {
671
        $vars['classes_array'][] = $layout_class;
672
      }
673
    }
674

    
675
    // Create region variables based on the layout settings.
676
    foreach ($layout['regions'] as $region_name => $region) {
677

    
678
      // Create the region content.
679
      $layout_render_array[$region_name] = array();
680
      if (isset($layout['settings']['regions'][$region_name])) {
681
        foreach ($layout['settings']['regions'][$region_name] as $key => $field) {
682
          // Make sure the field exists.
683
          if (!isset($vars[$render_container][$field])) {
684
            continue;
685
          }
686
          if (!isset($vars[$render_container][$field]['#weight'])) {
687
            $vars[$render_container][$field]['#weight'] = $key;
688
          }
689
          $layout_render_array[$region_name][$key] = $vars[$render_container][$field];
690
        }
691
      }
692

    
693
      // Add extras classes to the region.
694
      if (empty($layout['flexible'])) {
695
        $vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
696

    
697
        // Token support for region classes.
698
        if (module_exists('token') && isset($var[$object])) {
699
          $vars[$region_name . '_classes'] = token_replace($vars[$region_name . '_classes'], array($object => $vars[$object]), array('clear' => TRUE, 'sanitize' => TRUE));
700
        }
701
      }
702
      // Add a wrapper to the region.
703
      if (empty($layout['flexible'])) {
704
        $vars[$region_name . '_wrapper'] = isset($layout['settings']['wrappers'][$region_name]) ? $layout['settings']['wrappers'][$region_name] : 'div';
705
      }
706
    }
707

    
708
    // Let other modules know we have rendered.
709
    $vars['rendered_by_ds'] = TRUE;
710

    
711
    // Add a layout wrapper.
712
    $vars['layout_wrapper'] = isset($layout['settings']['layout_wrapper']) ? $layout['settings']['layout_wrapper'] : 'div';
713

    
714
    // Add layout attributes if any.
715
    $vars['layout_attributes'] = '';
716
    if (!empty($layout['settings']['layout_attributes'])) {
717
      if (isset($vars[$object])) {
718
        $vars['layout_attributes'] .= ' ' . token_replace($layout['settings']['layout_attributes'], array($object => $vars[$object]), array('clear' => TRUE, 'sanitize' => TRUE));
719
      }
720
      else {
721
        $vars['layout_attributes'] .= ' ' . $layout['settings']['layout_attributes'];
722
      }
723
    }
724
    // Merge in other attributes which were passed to the template.
725
    if (!empty($layout['settings']['layout_attributes_merge'])) {
726
      // Handle classes separately.
727
      if (isset($vars['attributes_array']['class'])) {
728
        $vars['classes_array'] += $vars['attributes_array']['class'];
729
        unset($vars['attributes_array']['class']);
730
      }
731
      $vars['layout_attributes'] .= ' ' . drupal_attributes($vars['attributes_array']);
732
    }
733

    
734
    // Token support for layout classes.
735
    if (module_exists('token') && isset($var[$object])) {
736
      foreach ($vars['classes_array'] as &$class) {
737
        $class = token_replace($class, array($object => $vars[$object]), array('clear' => TRUE, 'sanitize' => TRUE));
738
      }
739
    }
740

    
741
    // Add an onclick attribute on the wrapper.
742
    if (!empty($layout['settings']['layout_link_attribute'])) {
743
      $url = '';
744
      switch ($layout['settings']['layout_link_attribute']) {
745
        case 'content':
746
          if ($object == 'user') {
747
            $uri = entity_uri($vars['elements']['#entity_type'], $vars['elements']['#account']);
748
          }
749
          else {
750
            $uri = entity_uri($vars['elements']['#entity_type'], $vars[$object]);
751
          }
752
          if (!empty($uri)) {
753
            $url = $uri['path'];
754
          }
755
          break;
756
        case 'custom':
757
          $url = $layout['settings']['layout_link_custom'];
758
          break;
759
        case 'tokens':
760
          $url = token_replace($layout['settings']['layout_link_custom'], array($vars['elements']['#entity_type'] => $vars[$object]), array('clear' => TRUE));
761
          break;
762
      }
763

    
764
      if (!empty($url)) {
765
        $vars['layout_attributes'] .= ' onclick="location.href=\'' . url($url) . '\'"';
766
      }
767
    }
768

    
769
    // Let other modules alter the ds array before creating the region variables.
770
    $context = array('entity' => isset($vars[$object]) ? $vars[$object] : (isset($vars['elements']['#' . $object]) ? $vars['elements']['#' . $object] : ''), 'entity_type' => $vars['elements']['#entity_type'], 'bundle' => $vars['elements']['#bundle'], 'view_mode' => $vars['elements']['#view_mode']);
771
    drupal_alter('ds_pre_render', $layout_render_array, $context, $vars);
772
    foreach ($layout_render_array as $region_name => $content) {
773
      // In case this is a panels layout, add the region to the $content variable.
774
      if (isset($layout['module']) && $layout['module'] == 'panels') {
775
        $vars['content'][$region_name] = drupal_render($content);
776
      }
777
      else {
778
        $vars[$region_name] = drupal_render($content);
779
      }
780
    }
781
  }
782
}
783

    
784
/**
785
 * Create entity context.
786
 */
787
function ds_create_entity_context($entity_type, $entity, &$contexts, $context_arguments = array()) {
788
  ctools_include('context');
789
  if (empty($context_arguments)) {
790
    $context_arguments = array(array(
791
      'keyword' => $entity_type,
792
      'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
793
      'id' => 1,
794
      'name' => 'entity_id:' . $entity_type,
795
      'settings' => array(),
796
    ));
797
  }
798
  ctools_context_get_context_from_arguments($context_arguments, $contexts, array($entity));
799
}
800

    
801
/**
802
 * Render a code field.
803
 */
804
function ds_render_code_field($field) {
805
  if (isset($field['properties']['code'])) {
806
    $value = $field['properties']['code']['value'];
807
    // Token support - check on token property so we don't run every single field through token.
808
    if (isset($field['properties']['use_token']) && $field['properties']['use_token']) {
809
      $value = token_replace($value, array($field['entity_type'] => $field['entity']), array('clear' => TRUE));
810
    }
811
    $format = (isset($field['properties']['code']['format'])) ? $field['properties']['code']['format'] : 'plain_text';
812
    if ($format == 'ds_code' && module_exists('ds_format')) {
813
      $value = ds_format_php_eval($value, $field['entity'], isset($field['build']) ? $field['build'] : array());
814
    }
815
    else {
816
      $value = check_markup($value, $format);
817
    }
818
    return $value;
819
  }
820
}
821

    
822
/**
823
 * Render a CTools field.
824
 */
825
function ds_render_ctools_field($field) {
826
  if (isset($field['formatter_settings']['ctools'])) {
827

    
828
    // Extreme use case where a taxonomy_term object is not
829
    // loaded on the entity and triggers notices if a view is embedded
830
    // with taxonomy term fields from the same object.
831
    // see http://drupal.org/node/1238132 - To reproduce:
832
    // 1) add 2 taxonomy field instances on a bundle
833
    // 2) configure a ds layout showing only one
834
    // 3) embed a view with the 2 taxonomies as fields.
835
    if (isset($field['formatter_settings']['load_terms']) && $field['formatter_settings']['load_terms']) {
836
      static $terms_loaded = array();
837
      if (isset($field['entity']->language)) {
838
        $language = $field['entity']->language;
839
      }
840
      else {
841
        $language = LANGUAGE_NONE;
842
      }
843
      $instances = field_info_instances($field['entity_type'], $field['bundle']);
844
      foreach ($instances as $key => $instance) {
845
        $info = field_info_field($key);
846
        if ($info['module'] == 'taxonomy') {
847
          if (empty($field['entity']->{$key})) {
848
            continue;
849
          }
850
          if (!isset($field['entity']->{$key}[$language])) {
851
            $language = LANGUAGE_NONE;
852
          }
853
          foreach ($field['entity']->{$key}[$language] as $tkey => $item) {
854
            if (isset($item['tid']) && !isset($item['taxonomy_term'])) {
855
              if (!isset($terms_loaded[$item['tid']])) {
856
                $term = taxonomy_term_load($item['tid']);
857
                if (!is_object($term)) {
858
                  // This term is missing in the database.
859
                  continue;
860
                }
861
                $terms_loaded[$item['tid']] = $term;
862
              }
863
              $field['entity']->{$key}[$language][$tkey]['taxonomy_term'] = $terms_loaded[$item['tid']];
864
            }
865
          }
866
        }
867
      }
868
    }
869

    
870
    ctools_include('content');
871
    ctools_include('context');
872

    
873
    // Get variables.
874
    $show_title = $field['formatter_settings']['show_title'];
875
    $title_wrapper = trim($field['formatter_settings']['title_wrapper']);
876
    $ctools = unserialize($field['formatter_settings']['ctools']);
877
    $type = $ctools['type'];
878
    $subtype = $ctools['subtype'];
879
    $conf = $ctools['conf'];
880
    $entity_type = $field['entity_type'];
881
    $keywords = $arguments = $contexts = array();
882

    
883
    // Create entity context.
884
    ds_create_entity_context($entity_type, $field['entity'], $contexts);
885

    
886
    // Build the content.
887
    $data = ctools_content_render($type, $subtype, $conf, $keywords, $arguments, $contexts);
888
    // Return content.
889
    if (!empty($data->content)) {
890
      $content = '';
891
      if ($show_title) {
892
        if (empty($title_wrapper)) $title_wrapper = 'div';
893
        $content .= '<' . check_plain($title_wrapper) . ' class="title">' . $data->title . '</' . check_plain($title_wrapper) . '>';
894
      }
895
      if (is_array($data->content)) {
896
        $content .= drupal_render($data->content);
897
      }
898
      else {
899
        $content .= $data->content;
900
      }
901
      return $content;
902
    }
903
  }
904
}
905

    
906
/**
907
 * Render a block field.
908
 */
909
function ds_render_block_field($field) {
910
  // Invoke the block_view hook of the module.
911
  list($module, $delta) = explode('|', $field['properties']['block']);
912
  $block = module_invoke($module, 'block_view', $delta);
913

    
914
  // Get contextual links.
915
  $contextual_links = array();
916
  $contextual = module_exists('contextual') && user_access('access contextual links');
917
  if ($contextual) {
918
    if (isset($block['content']) && is_array($block['content']) && isset($block['content']['#contextual_links'])) {
919
      $contextual_links = $block['content']['#contextual_links'];
920
    }
921
  }
922

    
923
  // Render the block content.
924
  if (isset($block['content']) && is_array($block['content'])) {
925
    $block['content'] = drupal_render($block['content']);
926
  }
927

    
928
  // Go through in case we have actual content.
929
  if (!empty($block['content'])) {
930

    
931
    // Make sure subject is set.
932
    if (!isset($block['subject'])) {
933
      $block['subject'] = '';
934
    }
935

    
936
    global $theme_key;
937
    if (module_exists('block')) {
938
      $full_block = db_query("SELECT * FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme", array(':module' => $module, ':delta' => $delta, ':theme' => $theme_key))->fetchObject();
939
    }
940
    if (!empty($full_block)) {
941
      if ($full_block->title == '<none>') {
942
        $block['subject'] = '';
943
      }
944
      elseif (!empty($full_block->title)) {
945
        $block['subject'] = $full_block->title;
946
      }
947
    }
948

    
949
    // i18n support.
950
    if (function_exists('i18n_block_block_view_alter')) {
951

    
952
      // Check language visibility.
953
      global $language;
954
      static $block_languages = FALSE;
955
      if (!$block_languages) {
956
        $block_languages = array();
957
        $result = db_query('SELECT module, delta, language FROM {i18n_block_language}');
958
        foreach ($result as $record) {
959
          $block_languages[$record->module][$record->delta][$record->language] = TRUE;
960
        }
961
      }
962
      if (isset($block_languages[$module][$delta]) && !isset($block_languages[$module][$delta][$language->language])) {
963
        return;
964
      }
965

    
966
      // Translate.
967
      if (!empty($full_block->i18n_mode)) {
968
        i18n_block_block_view_alter($block, $full_block);
969
        if (!empty($block['title'])) {
970
          $block['subject'] = $block['title'];
971
        }
972
      }
973
    }
974

    
975
    $block = (object) $block;
976
    switch ($field['properties']['block_render']) {
977
      case DS_BLOCK_TEMPLATE:
978
        $block->region = NULL;
979
        $block->module = $module;
980
        $block->delta = $delta;
981
        $elements = array('elements' => array('#block' => $block, '#children' => $block->content));
982
        // Add contextual links.
983
        if ($contextual) {
984
          $elements['elements'] += array('#contextual_links' => array_merge($contextual_links, array('block' => array('admin/structure/block/manage', array($block->module, $block->delta)))));
985
        }
986
        return theme('block', $elements);
987
        break;
988
      case DS_BLOCK_TITLE_CONTENT:
989
        return '<h2 class="block-title">' . $block->subject . '</h2>' . $block->content;
990
        break;
991
      case DS_BLOCK_CONTENT:
992
        return $block->content;
993
        break;
994
    }
995
  }
996
}
997

    
998
/**
999
 * Render a field.
1000
 */
1001
function ds_render_field($field) {
1002
  $title_field = FALSE;
1003

    
1004
  $output = '';
1005
  $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
1006
  $settings += $field['properties']['default'];
1007

    
1008
  // Basic string.
1009
  if (isset($settings['link text'])) {
1010
    $output = t($settings['link text']);
1011
  }
1012
  elseif (isset($field['properties']['entity_render_key']) && isset($field['entity']->{$field['properties']['entity_render_key']})) {
1013
    if ($field['entity_type'] == 'user' && $field['properties']['entity_render_key'] == 'name') {
1014
      $output = format_username($field['entity']);
1015
    }
1016
    else {
1017
      $title_field = $field['properties']['entity_render_key'] == 'title' && $field['entity_type'] == 'node';
1018
      $output = $field['entity']->{$field['properties']['entity_render_key']};
1019
    }
1020
  }
1021

    
1022
  if (empty($output)) {
1023
    return;
1024
  }
1025

    
1026
  // Link.
1027
  if ($settings['link']) {
1028
    if (isset($field['entity']->uri)) {
1029
      $uri_info = $field['entity']->uri;
1030
    }
1031
    else {
1032
      $uri_info = entity_uri($field['entity_type'], $field['entity']);
1033
    }
1034
    $output = l($output, $uri_info['path'], $uri_info['options']);
1035
    if ($title_field) {
1036
      $output = ds_edit_support('title', $output, $field);
1037
    }
1038
  }
1039
  else {
1040
    $output = check_plain($output);
1041
    if ($title_field) {
1042
      $output = ds_edit_support('title', $output, $field);
1043
    }
1044
  }
1045

    
1046
  // Wrapper and class.
1047
  if (!empty($settings['wrapper'])) {
1048
    $wrapper = check_plain($settings['wrapper']);
1049
    $class = (!empty($settings['class'])) ? ' class="' . check_plain($settings['class']) . '"' : '';
1050
    $output = '<' . $wrapper . $class . '>' . $output . '</' . $wrapper . '>';
1051
  }
1052

    
1053
  return $output;
1054
}
1055

    
1056
/**
1057
 * Support for edit module.
1058
 *
1059
 * @param $field_name
1060
 *   The name of the field.
1061
 * @param $output
1062
 *   The output of the field.
1063
 * @param $field
1064
 *   The complete field array.
1065
 *
1066
 * @return
1067
 *   The field ready for edit module or the same value in case
1068
 *   the edit module is not enabled.
1069
 */
1070
function ds_edit_support($field_name, $output, $field) {
1071

    
1072
  if (module_exists('edit')) {
1073
    $edit_id = "node/" . $field['entity']->nid . "/" . $field_name . "/" . $field['entity']->language . "/" . $field['view_mode'];
1074
    $output = edit_wrap_pseudofield($output, $edit_id);
1075
  }
1076

    
1077
  return $output;
1078
}
1079

    
1080
/**
1081
 * Render an author field.
1082
 */
1083
function ds_render_author_field($field) {
1084

    
1085
  // Users without a user name are anonymous users. These are never linked.
1086
  if (empty($field['entity']->name)) {
1087
    $output = check_plain(variable_get('anonymous', t('Anonymous')));
1088
  }
1089

    
1090
  if ($field['formatter'] == 'author') {
1091
    $output = format_username($field['entity']);
1092
  }
1093

    
1094
  if ($field['formatter'] == 'author_linked') {
1095
    $output = theme('username', array('account' => $field['entity']));
1096
  }
1097

    
1098
  return ds_edit_support('author', $output, $field);
1099
}
1100

    
1101
/**
1102
 * Render a markup field.
1103
 */
1104
function ds_render_markup($field) {
1105
  if (isset($field['entity']->{$field['properties']['key']})) {
1106
    // Check for format, and let's take filtered_html as a sane default.
1107
    $format = isset($field['entity']->{$field['properties']['format']}) ? $field['entity']->{$field['properties']['format']} : 'filtered_html';
1108
    return check_markup($field['entity']->{$field['properties']['key']}, $format, '', TRUE);;
1109
  }
1110
}
1111

    
1112
/**
1113
 * Return the picture.
1114
 */
1115
function ds_return_picture($entity) {
1116

    
1117
  // Gravatar support.
1118
  if (module_exists('gravatar')) {
1119
    $entity = _gravatar_load_account($entity);
1120
    $entity->picture = _gravatar_get_account_user_picture($entity);
1121
  }
1122

    
1123
  if (!empty($entity->picture)) {
1124
    if (is_numeric($entity->picture)) {
1125
      return file_load($entity->picture);
1126
    }
1127
    else {
1128
      return $entity->picture;
1129
    }
1130
  }
1131
  elseif (variable_get('user_picture_default', '')) {
1132
    return variable_get('user_picture_default', '');
1133
  }
1134
}
1135

    
1136
/**
1137
 * Render a user picture.
1138
 */
1139
function ds_render_user_picture($field) {
1140
  $picture = ds_return_picture($field['entity']);
1141

    
1142
  if (!empty($picture)) {
1143
    $filepath = (isset($picture->uri)) ? $picture->uri : $picture;
1144
    $name = format_username($field['entity']);
1145
    $alt = t("@user's picture", array('@user' => $name));
1146
    $vars = array('path' => $filepath, 'alt' => $alt, 'title' => $alt);
1147

    
1148
    // If the image does not have a valid Drupal scheme (for eg. HTTP),
1149
    // don't load image styles.
1150
    if (module_exists('image') && file_valid_uri($filepath)) {
1151
      $vars['style_name'] = str_replace('ds_picture_', '', $field['formatter']);
1152
      $image = theme('image_style', $vars);
1153
    }
1154
    else {
1155
      $image = theme('image', $vars);
1156
    }
1157

    
1158
    if (!empty($field['entity']->uid) && user_access('access user profiles')) {
1159
      return l($image, 'user/' . $field['entity']->uid, array('html' => TRUE));
1160
    }
1161
    else {
1162
      return $image;
1163
    }
1164
  }
1165
}
1166

    
1167
/**
1168
 * Render a date field.
1169
 */
1170
function ds_render_date_field($field) {
1171
  $date_format = str_replace('ds_post_date_', '', $field['formatter']);
1172
  return ds_edit_support($field['properties']['entity_render_key'], format_date($field['entity']->{$field['properties']['entity_render_key']}, $date_format), $field);
1173
}
1174

    
1175
/**
1176
 * Render a "Submitted by"-line.
1177
 */
1178
function ds_render_submitted_by($field) {
1179
  $account = user_load($field['entity']->uid);
1180
  switch ($field['formatter']) {
1181
    case 'ds_time_ago':
1182
      $interval = REQUEST_TIME - $field['entity']->created;
1183
      return t('Submitted !interval ago by !user.', array('!interval' => format_interval($interval), '!user' => theme('username', array('account' => $account))));
1184
    default:
1185
      $date_format = str_replace('ds_post_date_', '', $field['formatter']);
1186
      return t('Submitted by !user on !date.', array('!user' => theme('username', array('account' => $account)), '!date' => format_date($field['entity']->created, $date_format)));
1187
  }
1188
}
1189

    
1190
/**
1191
 * Implements hook_field_formatter_info().
1192
 */
1193
function ds_field_formatter_info() {
1194

    
1195
  $formatters = array();
1196
  if (module_exists('taxonomy')) {
1197
    $formatters['ds_taxonomy_view_mode'] = array(
1198
      'label'       => t('Rendered taxonomy term'),
1199
      'description' => t('Display the referenced term in a specific view mode'),
1200
      'field types' => array('taxonomy_term_reference'),
1201
      'settings'    => array(
1202
        'taxonomy_term_reference_view_mode' => 'full',
1203
        'use_content_language' => TRUE,
1204
      ),
1205
    );
1206
    $formatters['ds_taxonomy_separator'] = array(
1207
      'label'       => t('Separated'),
1208
      'description' => t('Display the referenced term with a separator.'),
1209
      'field types' => array('taxonomy_term_reference'),
1210
      'settings'    => array(
1211
        'taxonomy_term_link' => TRUE,
1212
        'taxonomy_term_separator' => ', ',
1213
      ),
1214
    );
1215

    
1216
    if (module_exists('i18n_taxonomy')) {
1217
      $formatters['ds_taxonomy_separator_localized'] = array(
1218
        'label'       => t('Separated (localized)'),
1219
        'description' => t('Display the referenced term with a separator. Use this with the "localize" translation mode for vocabularies.'),
1220
        'field types' => array('taxonomy_term_reference'),
1221
        'settings'    => array(
1222
          'taxonomy_term_link' => TRUE,
1223
          'taxonomy_term_separator' => ', ',
1224
        ),
1225
      );
1226
    }
1227
  }
1228

    
1229
  return $formatters;
1230
}
1231

    
1232
/**
1233
 * Implements hook_field_formatter_view().
1234
 */
1235
function ds_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
1236
  global $language;
1237
  $element = array();
1238

    
1239
  if ($display['type'] === 'ds_taxonomy_view_mode') {
1240
    $view_mode = $display['settings']['taxonomy_term_reference_view_mode'];
1241
    foreach ($items as $delta => $item) {
1242
      if ($item['tid'] == 'autocreate') {
1243
        // We don't necessarily have a link when this is
1244
        // an autocreated term, usually in preview.
1245
        // So just send the term as check plained markup.
1246
        $build['#markup'] = check_plain($item['name']);
1247
      }
1248
      else {
1249
        $term = taxonomy_term_load($item['tid']);
1250
        if (module_exists('i18n_taxonomy')) {
1251
          $term = i18n_taxonomy_localize_terms($term);
1252
        }
1253
        if (!is_object($term)) {
1254
          // This term is missing in the database.
1255
          continue;
1256
        }
1257
        $settings = $display['settings'];
1258
        if ($settings['use_content_language'] && !empty($GLOBALS['language_content']->language)) {
1259
          $langcode = $GLOBALS['language_content']->language;
1260
        }
1261
        if (!empty($term)) {
1262
          $build = taxonomy_term_view($term, $view_mode, $langcode);
1263
        }
1264
        else {
1265
          $build['#markup'] = '';
1266
        }
1267
      }
1268
      $element[$delta] = $build;
1269
    }
1270
  }
1271

    
1272
  if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
1273
    $linked = $display['settings']['taxonomy_term_link'];
1274

    
1275
    $terms = array();
1276
    foreach ($items as $delta => $item) {
1277
      if ($item['tid'] == 'autocreate') {
1278
        // We don't necessarily have a link when this is
1279
        // an autocreated term, usually in preview.
1280
        // So just send the term as check plained markup.
1281
        $item_display = check_plain($item['name']);
1282
      }
1283
      else {
1284
        $term = taxonomy_term_load($item['tid']);
1285
        if (module_exists('i18n_taxonomy')) {
1286
          $term = i18n_taxonomy_localize_terms($term);
1287
        }
1288
        if (!is_object($term)) {
1289
          // This term is missing in the database.
1290
          continue;
1291
        }
1292
        if ($display['type'] == 'ds_taxonomy_separator_localized' && function_exists('i18n_taxonomy_term_name')) {
1293
          $term->name = i18n_taxonomy_term_name($term, $language->language);
1294
        }
1295
        $item_display = check_plain($term->name);
1296
        if ($linked) {
1297
          $uri = entity_uri('taxonomy_term', $term);
1298
          $item_display = l($term->name, $uri['path'], $uri['options']);
1299
        }
1300
      }
1301
      $terms[] = $item_display;
1302
    }
1303

    
1304
    if (!empty($terms)) {
1305
      $output = '';
1306
      $separator = $display['settings']['taxonomy_term_separator'];
1307
      $output = implode($separator, $terms);
1308
      $element[0] = array('#markup' => $output);
1309
    }
1310
  }
1311

    
1312
  return $element;
1313
}
1314

    
1315
/**
1316
 * Implements hook_contextual_links_view_alter().
1317
 */
1318
function ds_contextual_links_view_alter(&$element, $items) {
1319

    
1320
  if (!empty($element['#element']['#entity_type']) && !empty($element['#element']['#bundle']) && module_exists('field_ui') && user_access('administer content types')) {
1321
    $entity_type = $element['#element']['#entity_type'];
1322
    $bundle = $element['#element']['#bundle'];
1323
    $view_mode = isset($element['#element']['#view_mode']) ? $element['#element']['#view_mode'] : 'default';
1324

    
1325
    // Get the manage display URI.
1326
    $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
1327

    
1328
    // Check view mode settings.
1329
    $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
1330
    $overridden = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE);
1331

    
1332
    if (!$overridden) {
1333
      $admin_path .= '/display';
1334
    }
1335
    else {
1336
      $admin_path .= '/display/' . $view_mode;
1337
    }
1338

    
1339
    $element['#links']['manage-display'] = array(
1340
      'title' => t('Manage display'),
1341
      'href' => $admin_path,
1342
      'query' => drupal_get_destination(),
1343
    );
1344
  }
1345
}