Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Implements hook_menu().
10
 */
11
function ds_extras_menu() {
12
  $items = array();
13

    
14
  $items['admin/structure/ds/list/extras'] = array(
15
    'title' => 'Extras',
16
    'description' => 'Configure extra functionality for Display Suite.',
17
    'page callback' => 'drupal_get_form',
18
    'page arguments' => array('ds_extras_settings'),
19
    'access arguments' => array('admin_display_suite'),
20
    'file' => 'includes/ds_extras.admin.inc',
21
    'type' => MENU_LOCAL_TASK,
22
  );
23

    
24
  if (variable_get('ds_extras_switch_field')) {
25
    $items['ds-switch-view-mode'] = array(
26
      'title' => 'Switch view',
27
      'description' => 'Switches a view mode inline.',
28
      'page callback' => 'ds_switch_view_mode_inline',
29
      'access arguments' => array('access content'),
30
      'file' => 'includes/ds_extras.pages.inc',
31
      'type' => MENU_CALLBACK,
32
    );
33
  }
34

    
35
  if (variable_get('ds_extras_vd', FALSE) && module_exists('field_ui') && module_exists('views')) {
36
    $items['admin/structure/ds/vd'] = array(
37
      'title' => 'Views displays',
38
      'description' => 'Manage your views templates.',
39
      'page callback' => 'ds_extras_vd_overview',
40
      'file' => 'includes/ds_extras.vd.inc',
41
      'access arguments' => array('admin_display_suite'),
42
      'type' => MENU_LOCAL_TASK,
43
    );
44
    $items['admin/structure/ds/vd/manage'] = array(
45
      'title' => 'Manage layout',
46
      'description' => 'Manage your views templates.',
47
      'page callback' => 'ds_extras_vd_manage',
48
      'file' => 'includes/ds_extras.vd.inc',
49
      'access arguments' => array('admin_display_suite'),
50
      'type' => MENU_LOCAL_TASK,
51
    );
52
  }
53

    
54
  return $items;
55
}
56

    
57
/**
58
 * Implements hook_entity_info().
59
 */
60
function ds_extras_entity_info() {
61
  module_load_include('inc', 'ds_extras', 'includes/ds_extras.registry');
62
  return _ds_extras_entity_info();
63
}
64

    
65
/**
66
 * Implements hook_ds_layout_info_alter().
67
 */
68
function ds_extras_ds_layout_info_alter(&$layouts) {
69
  if (variable_get('ds_extras_hidden_region')) {
70
    foreach ($layouts as $key => $layout) {
71
      $layouts[$key]['regions']['ds_hidden'] = t('Hidden');
72
    }
73
  }
74
}
75

    
76
/**
77
 * Implements hook_permission().
78
 */
79
function ds_extras_permission() {
80
  $permissions = array();
81

    
82
  // Extra check to make sure this doesn't get fired on install.
83
  if (variable_get('ds_extras_switch_view_mode', FALSE)) {
84
    foreach (node_type_get_names() as $key => $name) {
85
      $permissions['ds_switch ' . $key] = array(
86
        'title' => t('Switch view modes on :type', array(':type' => $name))
87
      );
88
    }
89
  }
90

    
91
  if (variable_get('ds_extras_field_permissions', FALSE)) {
92
    $entities = entity_get_info();
93
    foreach ($entities as $entity_type => $info) {
94
      $fields = ds_get_fields($entity_type);
95
      foreach ($fields as $key => $finfo) {
96
        $permissions['view ' . $key . ' on ' . $entity_type] = array(
97
          'title' => t('View !field on !entity_type', array('!field' => $finfo['title'], '!entity_type' => $info['label'])),
98
        );
99
      }
100
    }
101
  }
102

    
103
  return $permissions;
104
}
105

    
106
/**
107
 * Implements hook_admin_paths().
108
 */
109
function ds_extras_admin_paths() {
110
  if (variable_get('node_admin_theme')) {
111
    return array(
112
      'node/*/display' => TRUE,
113
      'user/*/display' => TRUE,
114
      'taxonomy/term/*/display' => TRUE,
115
    );
116
  }
117
}
118

    
119
/**
120
 * Implements hook_menu_alter().
121
 */
122
function ds_extras_menu_alter(&$items) {
123
  module_load_include('inc', 'ds_extras', 'includes/ds_extras.registry');
124
  _ds_extras_menu_alter($items);
125
}
126

    
127
/**
128
 * Implements hook_theme_registry_alter().
129
 */
130
function ds_extras_theme_registry_alter(&$theme_registry) {
131
  module_load_include('inc', 'ds_extras', 'includes/ds_extras.registry');
132
  _ds_extras_theme_registry_alter($theme_registry);
133
}
134

    
135
/**
136
 * Implements hook_module_implements_alter().
137
 */
138
function ds_extras_module_implements_alter(&$implementations, $hook) {
139
  module_load_include('inc', 'ds_extras', 'includes/ds_extras.registry');
140
  _ds_extras_module_implements_alter($implementations, $hook);
141
}
142

    
143
/**
144
 * Implements hook_form_FORM_ID_alter().
145
 */
146
function ds_extras_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
147
  form_load_include($form_state, 'inc', 'ds_extras', 'includes/ds_extras.admin');
148
  ds_extras_field_ui_alter($form, $form_state);
149
}
150

    
151
/**
152
 * Implements hook_ctools_plugin_api().
153
 */
154
function ds_extras_ctools_plugin_api($owner, $api) {
155
  if ($owner == 'ds_extras' && $api == 'ds_extras') {
156
    return array('version' => 1);
157
  }
158
}
159

    
160
/**
161
 * DS fields access.
162
 *
163
 * @param $field
164
 *   The machine name of the field
165
 * @param $entity_type
166
 *   The name of the entity type.
167
 */
168
function ds_extras_ds_field_access($field, $entity_type) {
169

    
170
  if (user_access('view ' . $field . ' on ' . $entity_type)) {
171
    return TRUE;
172
  }
173

    
174
  return FALSE;
175
}
176

    
177
/**
178
 * Implements hook_field_attach_view_alter().
179
 */
180
function ds_extras_field_attach_view_alter(&$build, $context) {
181

    
182
  // If views and core doesn't send information along on the entity,
183
  // Display Suite doesn't have a context to render the fields.
184
  if (!isset($build['#entity_type']) && !isset($build['#bundle'])) {
185
    return;
186
  }
187

    
188
  $block_data = &drupal_static('ds_block_region');
189
  $region_blocks = variable_get('ds_extras_region_blocks', array());
190

    
191
  if (empty($region_blocks)) {
192
    return;
193
  }
194

    
195
  $entity_type = $build['#entity_type'];
196
  $bundle = $build['#bundle'];
197
  $view_mode = $context['view_mode'];
198

    
199
  $properties = array();
200
  foreach (element_properties($build) as $property) {
201
    $properties[$property] = $build[$property];
202
  }
203
  $properties['#view_mode'] = $view_mode;
204

    
205
  if ($layout = ds_get_layout($entity_type, $bundle, $view_mode)) {
206
    foreach ($region_blocks as $block_key => $block) {
207
      if ($block['info'] == "{$entity_type}_{$bundle}_{$view_mode}" && isset($layout['settings']['regions'][$block_key]) && !empty($layout['settings']['regions'][$block_key])) {
208
        foreach ($layout['settings']['regions'][$block_key] as $key => $field) {
209
          if (isset($build[$field])) {
210
            $block_data[$block_key][$field] = $build[$field];
211
            unset($build[$field]);
212
          }
213
        }
214
        if (isset($block_data[$block_key]) && is_array($block_data[$block_key])) {
215
          $block_data[$block_key] += $properties;
216
        }
217
      }
218
    }
219
  }
220
}
221

    
222
/**
223
 * Implements hook_form_FORM_ID_alter().
224
 */
225
function ds_extras_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
226
  $theme_functions = module_invoke_all('ds_field_theme_functions_info');
227
  unset($theme_functions['theme_ds_field_expert']);
228
  $form['instance']['ds_extras_field_template'] = array(
229
    '#type' => 'select',
230
    '#title' => t('Field Template'),
231
    '#default_value' => isset($form['#instance']['ds_extras_field_template']) ? $form['#instance']['ds_extras_field_template'] : '',
232
    '#options' => $theme_functions,
233
    '#description' => t('Choose the default HTML for this field.'),
234
    '#empty_option' => t('Use sitewide default'),
235
  );
236
}
237

    
238
/**
239
 * Utility function to return the view mode for the current entity.
240
 *
241
 * The drupal_static is called in ds_extras_node_show to set
242
 * the current view mode. Through this technique, the hide page
243
 * title functionality can also work across other view modes
244
 * if another one is chosen for the full page of the node.
245
 */
246
function ds_extras_get_view_mode() {
247
  return drupal_static('ds_extras_view_mode');
248
}
249

    
250
/**
251
 * Page title options for a full entity page view.
252
 */
253
function ds_extras_process_page_title(&$variables) {
254
  $page_title = drupal_static('ds_page_title');
255
  if (!empty($page_title)) {
256
    $variables['title'] = $page_title['title'];
257
    if (!empty($page_title['head_title'])) {
258
      drupal_set_title($page_title['head_title']);
259
    }
260
  }
261

    
262
  // Support for Views page title, currently only hiding the title.
263
  if (variable_get('ds_extras_vd', FALSE) && $view = views_get_page_view()) {
264
    if ($layout = ds_get_layout('ds_views', $view->name . '-' . $view->current_display, 'default')) {
265
      if (isset($layout['settings']['hide_page_title']) && $layout['settings']['hide_page_title'] == 1) {
266
        $variables['title'] = '';
267
      }
268
    }
269
  }
270
}
271

    
272
/**
273
 * Implements hook_ds_layout_settings_alter().
274
 */
275
function ds_extras_ds_layout_settings_alter($record, $form_state) {
276
  if (isset($form_state['values']['additional_settings']['ds_page_title']['ds_page_title_options']['page_option_type']) ||
277
      isset($form_state['values']['page_option_type'])) {
278

    
279
    // Save page title view type
280
    if (isset($form_state['values']['additional_settings']['ds_page_title']['ds_page_title_options']['page_option_type'])) {
281
      $record->settings['hide_page_title'] =  $form_state['values']['additional_settings']['ds_page_title']['ds_page_title_options']['page_option_type'];
282
    }
283
    else {
284
      $form_state['values']['page_option_type'];
285
    }
286

    
287
    // Save page title
288
    if (isset($form_state['values']['additional_settings']['ds_page_title']['ds_page_title_options']['page_option_title'])) {
289
      $record->settings['page_option_title'] = $form_state['values']['additional_settings']['ds_page_title']['ds_page_title_options']['page_option_title'];
290
    }
291
    else {
292
      $record->settings['page_option_title'] = $form_state['values']['page_option_title'];
293
    }
294
  }
295
  if (isset($form_state['values']['additional_settings']['hide_sidebars'])) {
296
    $record->settings['hide_sidebars'] = $form_state['values']['additional_settings']['hide_sidebars'];
297
  }
298
}
299

    
300
/**
301
 * Switch view mode field.
302
 */
303
function ds_extras_switch_view_mode_field($field) {
304
  $output = '';
305
  static $added = FALSE;
306

    
307
  if (isset($field['formatter_settings'])) {
308

    
309
    $entity = $field['entity'];
310
    $id = $entity->nid;
311
    $url = $field['entity_type'] . '-' . $field['view_mode'] . '-' . $id . '-';
312
    $switch = array();
313

    
314
    foreach ($field['formatter_settings']['vms'] as $key => $value) {
315
      if (!empty($value)) {
316
        $class = 'switch-' . $key;
317
        if ($key == $field['view_mode']) {
318
          $switch[] = '<span class="' . $class . '">' . check_plain(t($value)) . '</span>';
319
        }
320
        else {
321
          $switch[] = '<span class="' . $class . '"><a href="" class="' . $url . $key . '">' . check_plain(t($value)) . '</a></span>';
322
        }
323
      }
324
    }
325

    
326
    if (!empty($switch)) {
327
      if (!$added) {
328
        $add = TRUE;
329
        drupal_add_js(drupal_get_path('module', 'ds_extras') . '/js/ds_extras.js');
330
      }
331
      $output = '<div class="switch-view-mode-field">' . implode(' ', $switch) . '</div>';
332
    }
333
  }
334

    
335
  return $output;
336
}
337

    
338
/**
339
 * Implements hook_form_FORM_ID_alter().
340
 */
341
function ds_extras_form_node_type_form_alter(&$form, $form_state, $form_id) {
342
  $node_type = $form['#node_type']->type;
343

    
344
  // Get the view modes.
345
  $options = ds_extras_get_bundle_view_modes('node', $node_type);
346

    
347
  // Only fire if we have more than 1 option.
348
  if (count($options) > 1) {
349
    if (!isset($form['ds_extras'])) {
350
      $form['ds_extras'] = array(
351
        '#type' => 'fieldset',
352
        '#title' => t('Display Suite: Extras'), // There's already a 'Display settings' fieldset
353
        '#collapsible' => TRUE,
354
        '#collapsed' => TRUE,
355
        '#group' => 'additional_settings',
356
        '#weight' => 100,
357
      );
358
    }
359

    
360
    $form['ds_extras']['ds_extras_view_modes'] = array(
361
      '#type' => 'checkboxes',
362
      '#title' => t('View modes'),
363
      '#options' => $options,
364
      '#default_value' => variable_get('ds_extras_view_modes_' . $node_type, array_keys($options)),
365
      '#description' => t('Select which view modes will be available to switch to on node edit forms.'),
366
    );
367
  }
368
}
369

    
370
/**
371
 * Implements hook_form_FORM_ID_alter().
372
 */
373
function ds_extras_form_node_form_alter(&$form, $form_state, $form_id) {
374
  $node = $form['#node'];
375

    
376
  // Switch full view mode.
377
  if (user_access('ds_switch ' . $node->type)) {
378

    
379
    // Get the view modes.
380
    $view_modes = ds_extras_get_bundle_view_modes('node', $node->type);
381
    $enabled_vm = variable_get('ds_extras_view_modes_' . $node->type, array_keys($view_modes));
382
    $layouts = array();
383
    $options = array();
384
    foreach ($view_modes as $key => $label) {
385
      if (in_array($key, $enabled_vm)) {
386
        $layout = ds_get_layout('node', $node->type, $key, FALSE);
387
        $layouts[$key] = $layout;
388
        $options[$key] = $label;
389
      }
390
    }
391

    
392
    // Only fire if we have more than 1 option.
393
    if (count($options) > 1) {
394
      if (!isset($form['ds_extras'])) {
395
        $form['ds_extras'] = array(
396
          '#type' => 'fieldset',
397
          '#title' => t('Display settings'),
398
          '#collapsible' => TRUE,
399
          '#collapsed' => TRUE,
400
          '#group' => 'additional_settings',
401
          '#weight' => 100,
402
        );
403
      }
404

    
405
      $form['ds_extras']['ds_switch'] = array(
406
        '#type' => 'select',
407
        '#title' => t('View mode'),
408
        '#options' => $options,
409
        '#default_value' => isset($node->ds_switch) ? $node->ds_switch : 'default',
410
        '#description' => t('Switch to a different view mode to display the full page view of this node.'),
411
        '#weight' => -1,
412
        '#ajax' => array(
413
          'callback' => 'ds_extras_switch_view_mode_preview_callback',
414
          'wrapper' => 'ds_switch_preview_wrapper',
415
        ),
416
      );
417

    
418
      $form['ds_extras']['preview'] = array(
419
        '#type' => 'container',
420
        '#prefix' => '<div id="ds_switch_preview_wrapper">',
421
        '#suffix' => '</div>',
422
      );
423

    
424
      $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
425
      $mode = isset($form_state['input']['ds_switch']) ? $form_state['input']['ds_switch'] : (isset($node->ds_switch) ? $node->ds_switch : 'default');
426
      if (isset($layouts[$mode])) {
427
        $chosen_layout = $layouts[$mode];
428
        $image = (isset($chosen_layout['image']) && !empty($chosen_layout['image'])) ? $chosen_layout['path'] . '/' . $chosen_layout['layout'] . '.png' : $fallback_image;
429
        if (isset($chosen_layout['panels']) && !empty($chosen_layout['panels']['icon'])) {
430
          $image = $chosen_layout['panels']['path'] . '/' . $chosen_layout['panels']['icon'];
431
        }
432
      }
433
      else {
434
        $image = $fallback_image;
435
      }
436

    
437
      $form['ds_extras']['preview']['image'] = array(
438
        '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
439
      );
440
    }
441
  }
442
}
443

    
444
/**
445
 * Get view modes for an entity bundle.
446
 */
447
function ds_extras_get_bundle_view_modes($type, $bundle) {
448
  $view_modes = array('default' => t('Default'));
449

    
450
  $view_mode_settings = field_view_mode_settings($type, $bundle);
451
  $ds_vm = ds_entity_view_modes($type);
452

    
453
  foreach ($ds_vm as $key => $item) {
454
    $overriden = (!empty($view_mode_settings[$key]['custom_settings']) ? TRUE : FALSE);
455
    if ($overriden) {
456
      $view_modes[$key] = $item['label'];
457
    }
458
  }
459

    
460
  return $view_modes;
461
}
462

    
463
/**
464
 * Ajax callback for _ds_field_ui_table_layouts_preview().
465
 */
466
function ds_extras_switch_view_mode_preview_callback($form, $form_state) {
467
  return $form['ds_extras']['preview'];
468
}
469

    
470
/**
471
 * Implements hook_block_info().
472
 */
473
function ds_extras_block_info() {
474

    
475
  $region_blocks = variable_get('ds_extras_region_blocks', array());
476

    
477
  if (empty($region_blocks)) {
478
    return array();
479
  }
480

    
481
  foreach ($region_blocks as $key => $block) {
482
    $blocks[$key] = array(
483
      'info' => $block['title'],
484
      'cache' => DRUPAL_NO_CACHE,
485
    );
486
  }
487
  return $blocks;
488
}
489

    
490
/**
491
 * Implements hook_block_view().
492
 */
493
function ds_extras_block_view($delta = '') {
494
  $data = drupal_static('ds_block_region');
495
  $region_blocks = variable_get('ds_extras_region_blocks', array());
496

    
497
  if (!empty($data[$delta])) {
498
    $block = array();
499
    $block['subject'] = $region_blocks[$delta]['title'];
500
    $block['content'] = $data[$delta];
501
    return $block;
502
  }
503
}
504

    
505
/**
506
 * Implements hook_ds_layout_region_alter().
507
 */
508
function ds_extras_ds_layout_region_alter($context, &$region_info) {
509

    
510
  $region_blocks = variable_get('ds_extras_region_blocks', array());
511

    
512
  // Bail out if region_blocks is empty or we are working on default view mode.
513
  if (empty($region_blocks) || $context['view_mode'] == 'default') {
514
    return;
515
  }
516

    
517
  $entity_type = $context['entity_type'];
518
  $bundle = $context['bundle'];
519
  $view_mode = $context['view_mode'];
520

    
521
  foreach ($region_blocks as $block_key => $block) {
522
    if ($block['info'] == "{$entity_type}_{$bundle}_{$view_mode}") {
523
      $region_info['region_options'][$block_key] = $block['title'];
524
      if (isset($region_info['table_regions'])) {
525
        $region_info['table_regions'][$block_key] = array(
526
          'title' => check_plain($block['title']),
527
          'message' => t('No fields are displayed in this region'),
528
        );
529
      }
530
    }
531
  }
532
}
533

    
534
/**
535
 * Implements hook_field_extra_fields().
536
 */
537
function ds_extras_field_extra_fields() {
538
  $extra = array();
539

    
540
  // Register a single field so fields for vd are
541
  // picked up nicely in the display overview form.
542
  if (variable_get('ds_extras_vd')) {
543
    ctools_include('export');
544
    $vd_settings = ctools_export_crud_load_all('ds_vd');
545
    foreach ($vd_settings as $vd) {
546
      $extra['ds_views'][$vd->vd] = array(
547
        'display' => array(
548
          'title' => array(
549
            'label' => t('Title'),
550
            'description' => t('Title'),
551
            'weight' => 10,
552
          ),
553
        ),
554
      );
555
    }
556
  }
557

    
558
  if (variable_get('ds_extras_fields_extra')) {
559
    $fields = explode("\n", variable_get('ds_extras_fields_extra_list', FALSE));
560
    foreach ($fields as $field) {
561
      $field = trim($field);
562
      if (!empty($field)) {
563
        list($entity, $bundle, $field_name) = explode('|', $field);
564
        $extra[check_plain($entity)][check_plain($bundle)]['display'][$field_name] = array(
565
          'label' => drupal_ucfirst(str_replace('_', ' ', check_plain($field_name))),
566
          'description' => drupal_ucfirst(str_replace('_', ' ', check_plain($field_name))),
567
          'weight' => 0,
568
        );
569
      }
570
    }
571
  }
572

    
573
  return $extra;
574
}
575

    
576
/**
577
 * Output flag.
578
 */
579
function ds_extras_flags_add_flag_link($field) {
580
  return flag_create_link($field['properties']['flag'], $field['entity']->nid);
581
}
582

    
583
/**
584
 * Implements hook_preprocess_views_view().
585
 */
586
function ds_extras_preprocess_view_layout(&$variables) {
587

    
588
  if ($layout = ds_get_layout('ds_views', $variables['view']->name . '-' . $variables['view']->current_display, 'default')) {
589

    
590
    $variables['elements']['#entity_type'] = $variables['#entity_type'] = 'ds_views';
591
    $variables['elements']['#bundle'] = $variables['#bundle'] = $variables['view']->name . '-' . $variables['view']->current_display;
592
    $variables['elements']['#view_mode'] = 'default';
593

    
594
    $variables['ds_views'] = $variables['view'];
595
    $variables['render_code_fields'] = TRUE;
596
    ds_field_attach_view_alter($variables, array('view_mode' => 'default', 'entity' => $variables['view']));
597

    
598
    // Special case for views function fields.
599
    if (isset($variables['view']->ds_vd)) {
600
      foreach ($variables['view']->ds_vd as $key => $value) {
601
        $variables[$key] = $value;
602
      }
603
    }
604

    
605
    // Don't remove the variables so we don't trigger notices.
606
    $variables['preprocess_keep'] = TRUE;
607
    ds_entity_variables($variables);
608
    if (isset($variables['#attached'])) {
609
      drupal_process_attached($variables);
610
    }
611
  }
612
}
613

    
614
/**
615
 * Function used for theming the views title, user name etc. Note that
616
 * this is a function so it can't be overridden by a phptemplate function.
617
 */
618
function ds_vd_render_title_field($field) {
619
  $output = '';
620
  $formatter = explode('_', $field['formatter']);
621
  $tag = $formatter[2];
622
  $output = '<' . $tag . '>' . $field['entity']->get_title() . '</' . $tag . '>';
623

    
624
  // Views is a special case, we stack title on the entity.
625
  $field['entity']->preprocess_fields[] = 'title';
626
  $field['entity']->ds_vd['title'] = $output;
627
}
628

    
629
/**
630
 * Implements hook_entity_view_alter().
631
 */
632
function ds_extras_entity_view_alter(&$build, $entity_type) {
633
  static $loaded = array();
634

    
635
  // If views and core doesn't send information along on the entity,
636
  // Display Suite doesn't have a context to render the layout.
637
  if (!isset($build['#entity_type']) || !isset($build['#bundle'])) {
638
    return;
639
  }
640

    
641
  $bundle = $build['#bundle'];
642
  $view_mode = $build['#view_mode'];
643
  if ($overridden_view_mode = ds_extras_get_view_mode()) {
644
    $view_mode = $overridden_view_mode;
645
  }
646
  $layout = ds_get_layout($entity_type, $bundle, $view_mode);
647

    
648
  // Page title options.
649
  if (variable_get('ds_extras_hide_page_title', FALSE)) {
650
    $page_title = &drupal_static('ds_page_title');
651
    if (isset($layout['settings']['hide_page_title']) && $layout['settings']['hide_page_title'] == 1 && ds_extras_is_entity_page_view($build, $entity_type)) {
652
      $page_title['title'] = '';
653
    }
654
    elseif (isset($layout['settings']['hide_page_title']) && $layout['settings']['hide_page_title'] == 2 && !empty($layout['settings']['page_option_title']) && ds_extras_is_entity_page_view($build, $entity_type)) {
655
      $contexts = array();
656
      $id = (arg(0) == 'taxonomy') ? arg(2) : arg(1);
657
      $entity = entity_load($entity_type, array($id));
658
      if (isset($entity[$id])) {
659
        ds_create_entity_context($entity_type, $entity[$id], $contexts);
660
        $title = $layout['settings']['page_option_title'];
661
        $title = filter_xss_admin(ctools_context_keyword_substitute($title, array(), $contexts));
662
        $page_title['title'] = t($title);
663
        $page_title['head_title'] = t($title);
664
      }
665
    }
666
  }
667

    
668
  // Disable blocks.
669
  if (isset($layout['settings']['hide_sidebars']) && !isset($loaded[$entity_type][$bundle][$view_mode])) {
670

    
671
    // Store the setting.
672
    $loaded[$entity_type][$bundle][$view_mode] = TRUE;
673

    
674
    // Disable blocks.
675
    if (isset($layout['settings']['hide_sidebars']) && $layout['settings']['hide_sidebars']) {
676
      ctools_set_no_blocks();
677
    }
678
  }
679
}
680

    
681
/**
682
 * Helper function to check if this is a page view.
683
 *
684
 * The page title option adds the ability to hide or override the page title.
685
 * However, it can't happen that an entity bleeds its page title to say a view
686
 * or the frontpage.
687
 *
688
 * See http://drupal.org/node/1526732 and http://drupal.org/node/1446554.
689
 */
690
function ds_extras_is_entity_page_view($build, $entity_type) {
691
  switch ($entity_type) {
692
    case 'node':
693
      return node_is_page($build['#node']);
694
      break;
695
    case 'user':
696
    $page_account = menu_get_object('user');
697
    return (!empty($page_account) ? $page_account->uid == $build['#account']->uid : FALSE);
698
    break;
699
    case 'taxonomy_term':
700
      $page_term = menu_get_object('taxonomy_term', 2);
701
      return (!empty($page_term) ? $page_term->tid == $build['#term']->tid : FALSE);
702
      break;
703
    case 'profile2':
704
      return $build['#view_mode'] == 'page';
705
      break;
706
  }
707

    
708
  return FALSE;
709
}
710

    
711
/**
712
 * Implements hook_ds_field_theme_functions_info().
713
 */
714
function ds_extras_ds_field_theme_functions_info() {
715
  return array(
716
    'theme_field' => t('Drupal default'),
717
    'theme_ds_field_reset' => t('Full Reset'),
718
    'theme_ds_field_minimal' => t('Minimal'),
719
    'theme_ds_field_expert' => t('Expert'),
720
  );
721
}
722

    
723
/**
724
 * Reset all HTML for the field.
725
 */
726
function theme_ds_field_reset($variables) {
727
  $output = '';
728

    
729
  // Render the label.
730
  if (!$variables['label_hidden']) {
731
    $output .= '<div class="label-' . $variables['element']['#label_display'] . '">' . $variables['label'];
732
    if (!variable_get('ft-kill-colon', FALSE)) {
733
      $output .= ':&nbsp;';
734
    }
735
    $output .= '</div>';
736
  }
737

    
738
  // Render the items.
739
  foreach ($variables['items'] as $delta => $item) {
740
    $output .= drupal_render($item);
741
  }
742

    
743
  return $output;
744
}
745

    
746
/**
747
 * Provide minimal HTML for the field.
748
 */
749
function theme_ds_field_minimal($variables) {
750
  $output = '';
751
  $config = $variables['ds-config'];
752
  $classes = isset($config['classes']) ? ' ' . $config['classes'] : '';
753

    
754
  // Add a simple wrapper.
755
  $output .= '<div class="field field-name-' . strtr($variables['element']['#field_name'], '_', '-') . $classes . '">';
756

    
757
  // Render the label.
758
  if (!$variables['label_hidden']) {
759
    $output .= '<div class="label-' . $variables['element']['#label_display'] . '">' . $variables['label'];
760
    if (!isset($config['lb-col'])) {
761
      $output .= ':&nbsp;';
762
    }
763
    $output .= '</div>';
764
  }
765

    
766
  // Render the items.
767
  foreach ($variables['items'] as $delta => $item) {
768
    $output .= drupal_render($item);
769
  }
770
  $output .="</div>";
771

    
772
  return $output;
773
}
774

    
775
/**
776
 * Custom output all HTML for the field.
777
 */
778
function theme_ds_field_expert($variables) {
779
  $output = '';
780

    
781
  $config = $variables['ds-config'];
782

    
783
  // Set prefix
784
  if (!empty($config['prefix'])) {
785
    $output .= $config['prefix'];
786
  }
787

    
788
  // Render the label if it's not hidden.
789
  if (!$variables['label_hidden']) {
790
    $styled_label = $variables['label'];
791
    if (!isset($config['lb-col'])) $styled_label .= ':&nbsp;';
792

    
793
    // Style the label it self
794
    $label_wrapper = isset($config['lb-el']) ? $config['lb-el'] : 'div';
795
    $class = array('label-' . $variables['element']['#label_display']);
796
    if (!empty($config['lb-cl'])) $class[] = $config['lb-cl'];
797
    $class = !empty($class) ? ' class="' . implode(' ', $class) . '"' : '';
798
    $attributes = array();
799
    if (!empty($config['lb-at'])) $attributes[] = $config['lb-at'];
800
    if (!empty($config['lb-def-at'])) $attributes[] = $variables['title_attributes'];
801
    $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
802
    $styled_label = '<' . $label_wrapper . $class . $attributes . '>' . $styled_label . '</' . $label_wrapper . '>';
803

    
804
    // Place it inside a wrapper
805
    if (isset($config['lbw'])) {
806
      $class = !empty($config['lbw-cl']) ? ' class="' . $config['lbw-cl'] . '"' : '';
807
      $attributes = !empty($config['lbw-at']) ?  ' ' . $config['lbw-at'] : '';
808
      $styled_label = '<' . $config['lbw-el'] . $class . $attributes . '>' . $styled_label . '</' . $config['lbw-el'] . '>';
809
    }
810

    
811
    $output .= $styled_label;
812
  }
813

    
814
  // Field items wrapper
815
  if (isset($config['fis'])) {
816
    $class = (!empty($config['fis-cl'])) ? ' class="' . token_replace($config['fis-cl'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE)) . '"' : '';
817
    $attributes = array();
818
    if (!empty($config['fis-at'])) $attributes[] = token_replace($config['fis-at'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE));;
819
    if (!empty($config['fis-def-at'])) $attributes[] = $variables['content_attributes'];
820
    $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
821
    $output .= '<' . $config['fis-el'] . $class . $attributes . '>';
822
  }
823

    
824
  // Render items.
825
  $item_count = count($variables['items']);
826
  $item_index = 0;
827
  foreach ($variables['items'] as $delta => $item) {
828
    // Field item wrapper.
829
    if (isset($config['fi'])) {
830
      $class = array();
831
      if (!empty($config['fi-odd-even'])) {
832
        $class[] = $delta % 2 ? 'even' : 'odd';
833
      }
834
      if (!empty($config['fi-cl'])) {
835
        $class[] = $config['fi-cl'];
836
      }
837
      if(!empty($config['fi-first-last']) && $item_index == 0) {
838
        $class[] = "first";
839
      }
840
      if(!empty($config['fi-first-last']) && $item_index == $item_count - 1) {
841
        $class[] = "last";
842
      }
843
      $class = !empty($class) ? ' class="'. token_replace(implode(' ', $class), array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE)) .'"' : '';
844
      $attributes = array();
845
      if (!empty($config['fi-at'])) {
846
        $attributes[] = token_replace($config['fi-at'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE));
847
      }
848
      if (!empty($config['fi-def-at'])) {
849
        $attributes[] = $variables['item_attributes'][$delta];
850
      }
851
      $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
852
      $output .= '<' . $config['fi-el'] . $class . $attributes .'>';
853
    }
854

    
855
    // Render field content.
856
    $output .= drupal_render($item);
857

    
858
    // Closing field item wrapper.
859
    if (isset($config['fi'])) {
860
      $output .= '</' . $config['fi-el'] . '>';
861
    }
862
    ++$item_index;
863
  }
864

    
865
  // Closing field items wrapper.
866
  if (isset($config['fis'])) {
867
    $output .= '</' . $config['fis-el'] . '>';
868
  }
869

    
870
  // Outer wrapper.
871
  if (isset($config['ow'])) {
872
    $class = array();
873
    if (!empty($config['ow-cl'])) $class[] = token_replace($config['ow-cl'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE));
874
    if (!empty($config['ow-def-cl'])) $class[] = $variables['classes'];
875
    $class = (!empty($class)) ? ' class="' . implode(' ', $class) . '"' : '';
876
    $attributes = array();
877
    if (!empty($config['ow-at'])) $attributes[] = token_replace($config['ow-at'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE));;
878
    if (!empty($config['ow-def-at'])) $attributes[] = $variables['attributes'];
879
    $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
880
    $output = '<' . $config['ow-el'] . $class . $attributes . '>' . $output . '</' . $config['ow-el'] . '>';
881
  }
882

    
883
  // Set suffix
884
  if (!empty($config['suffix'])) {
885
    $output .= $config['suffix'];
886
  }
887

    
888
  return $output;
889
}
890

    
891
/**
892
 * Implements hook_ds_field_settings_alter().
893
 */
894
function ds_extras_ds_field_settings_alter(&$field_settings, $form, &$form_state) {
895

    
896
  $fields = $form_state['values']['fields'];
897
  $default_field_function = variable_get('ft-default', 'theme_field');
898

    
899
  $wrappers = array(
900
    'ow' => t('Wrapper'),
901
    'fis' => t('Field items'),
902
    'fi' => t('Field item')
903
  );
904

    
905
  foreach ($fields as $key => $field) {
906

    
907
    // Make sure we need to save anything for this field.
908
    if (_ds_field_valid($key, $field, $form_state)) {
909
      continue;
910
    }
911

    
912
    // Get the values.
913
    $values = isset($form_state['formatter_settings'][$key]['ft']) ? $form_state['formatter_settings'][$key]['ft'] : array();
914
    if (empty($values)) {
915
      continue;
916
    }
917
    $field_settings[$key]['formatter_settings']['ft'] = array();
918

    
919
    // Theme output function.
920
    $function = isset($values['func']) ? $values['func'] : $default_field_function;
921
    if ($function != $default_field_function) {
922
      $field_settings[$key]['formatter_settings']['ft']['func'] = $function;
923
    }
924

    
925
    // Field classes.
926
    if ($function != 'theme_ds_field_expert' && $function != 'theme_ds_field_reset' && isset($values['classes'])) {
927
      $classes = is_array($values['classes']) ? implode(' ', $values['classes']) : $values['classes'];
928
      if (!empty($classes)) {
929
        $field_settings[$key]['formatter_settings']['ft']['classes'] = $classes;
930
      }
931
    }
932

    
933
    // Label.
934
    if (isset($form_state['values']['fields'][$key]['label'])) {
935
      if (!empty($values['lb'])) {
936
        $field_settings[$key]['formatter_settings']['ft']['lb'] = $values['lb'];
937
      }
938
      if (!(empty($values['lb-el'])) && $function == 'theme_ds_field_expert') {
939
        $field_settings[$key]['formatter_settings']['ft']['lb-el'] = check_plain($values['lb-el']);
940
      }
941
      if (!(empty($values['lb-cl'])) && $function == 'theme_ds_field_expert') {
942
        $field_settings[$key]['formatter_settings']['ft']['lb-cl'] = check_plain($values['lb-cl']);
943
      }
944
      if (!(empty($values['lb-at'])) && $function == 'theme_ds_field_expert') {
945
       $field_settings[$key]['formatter_settings']['ft']['lb-at'] = filter_xss($values['lb-at']);
946
      }
947
      if (!(empty($values['lb-def-at'])) && $function == 'theme_ds_field_expert') {
948
       $field_settings[$key]['formatter_settings']['ft']['lb-def-at'] = TRUE;
949
      }
950
      if (!(empty($values['lb-col']))) {
951
        $field_settings[$key]['formatter_settings']['ft']['lb-col'] = TRUE;
952
      }
953

    
954
      // label wrapper
955
      if (!empty($values['lbw'])) {
956
        $field_settings[$key]['formatter_settings']['ft']['lbw'] = $values['lbw'];
957
      }
958
      if (!(empty($values['lbw-el'])) && $function == 'theme_ds_field_expert') {
959
        $field_settings[$key]['formatter_settings']['ft']['lbw-el'] = check_plain($values['lbw-el']);
960
      }
961
      if (!(empty($values['lbw-cl'])) && $function == 'theme_ds_field_expert') {
962
        $field_settings[$key]['formatter_settings']['ft']['lbw-cl'] = check_plain($values['lbw-cl']);
963
      }
964
      if (!(empty($values['lbw-at'])) && $function == 'theme_ds_field_expert') {
965
       $field_settings[$key]['formatter_settings']['ft']['lbw-at'] = filter_xss($values['lbw-at']);
966
      }
967
    }
968

    
969
    if (!(empty($values['prefix']))) {
970
      $field_settings[$key]['formatter_settings']['ft']['prefix'] = $values['prefix'];
971
    }
972
    if (!(empty($values['suffix']))) {
973
      $field_settings[$key]['formatter_settings']['ft']['suffix'] = $values['suffix'];
974
    }
975

    
976
    // Custom field configuration.
977
    if ($function == 'theme_ds_field_expert') {
978
      foreach ($wrappers as $wrapper_key => $title) {
979
        if (!empty($values[$wrapper_key])) {
980
          // Enable.
981
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key] = TRUE;
982
          // Element.
983
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-el'] = !(empty($values[$wrapper_key . '-el'])) ? check_plain($values[$wrapper_key . '-el']) : 'div';
984
          // Classes.
985
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-cl'] = !(empty($values[$wrapper_key . '-cl'])) ? check_plain($values[$wrapper_key . '-cl']) : '';
986
          // Default Classes.
987
          if (in_array($wrapper_key, array('ow', 'lb'))) {
988
            $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-def-cl'] = !(empty($values[$wrapper_key . '-def-cl'])) ? TRUE : FALSE;
989
          }
990
          // Attributes.
991
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-at'] = !(empty($values[$wrapper_key . '-at'])) ? filter_xss($values[$wrapper_key . '-at']) : '';
992
          // Default attributes.
993
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-def-at'] = !(empty($values[$wrapper_key . '-def-at'])) ? TRUE : FALSE;
994
          // Odd even class.
995
          if ($wrapper_key == 'fi') {
996
            $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-odd-even'] = !(empty($values[$wrapper_key . '-odd-even'])) ? TRUE : FALSE;
997
            $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-first-last'] = !(empty($values[$wrapper_key . '-first-last'])) ? TRUE : FALSE;
998
          }
999
        }
1000
      }
1001
    }
1002
  }
1003
}
1004

    
1005
/**
1006
 * Implements hook_preprocess_field().
1007
 */
1008
function ds_extras_preprocess_field(&$variables) {
1009

    
1010
  // We need to be sure this field is in a layout which is rendered by DS.
1011
  if (!ds_get_layout($variables['element']['#entity_type'], $variables['element']['#bundle'], $variables['element']['#view_mode'])) {
1012
    return;
1013
  }
1014

    
1015
  $entity_type = $variables['element']['#entity_type'];
1016
  $bundle = $variables['element']['#bundle'];
1017
  $view_mode = $variables['element']['#view_mode'];
1018

    
1019
  $config = array();
1020
  static $field_settings = array();
1021
  if (!isset($field_settings[$entity_type][$bundle][$view_mode])) {
1022
    $layout = ds_get_layout($entity_type, $bundle, $view_mode);
1023
    $field_settings[$entity_type][$bundle][$view_mode] = ds_get_field_settings($entity_type, $bundle, $view_mode);
1024
  }
1025

    
1026
  // Get the field name and field instance info - if available.
1027
  $field_name = $variables['element']['#field_name'];
1028
  $field_instance_info = field_info_instance($entity_type, $field_name, $bundle);
1029

    
1030
  // Check if this field has custom output settings.
1031
  $variables['ds-config'] = array();
1032
  if (isset($field_settings[$entity_type][$bundle][$view_mode][$field_name]['formatter_settings']['ft'])) {
1033
    $config = $field_settings[$entity_type][$bundle][$view_mode][$field_name]['formatter_settings']['ft'];
1034
    $variables['ds-config'] = $config;
1035
  }
1036

    
1037
  // CSS classes
1038
  if (isset($config['classes'])) {
1039
    $variables['classes_array'][] = $config['classes'];
1040
  }
1041

    
1042
  // Alter the label if configured.
1043
  if (!$variables['label_hidden']) {
1044
    if (isset($config['lb'])) {
1045
      $variables['label'] = t(check_plain($config['lb']));
1046
    }
1047
  }
1048

    
1049
  // Determine the field template. In case it's something different
1050
  // than theme_field, we'll add that function as a suggestion.
1051
  if (isset($config['func']) && $config['func'] != 'theme_field') {
1052
    $variables['ds-config'] = $config;
1053
    $variables['theme_hook_suggestions'] = array();
1054
    // Either it uses the function.
1055
    $variables['theme_hook_suggestions'][] = str_replace('theme_', '', $config['func']);
1056
    $variables['theme_hook_suggestions'][] = $config['func'];
1057
    // Or the template file(s).
1058
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $config['func']);
1059
    $variables['theme_hook_suggestions'][] = $suggestion;
1060
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1061
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1062
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1063
  }
1064
  // Check if we have a default field template on instance level.
1065
  elseif (isset($field_instance_info['ds_extras_field_template']) && !empty($field_instance_info['ds_extras_field_template']) && $field_instance_info['ds_extras_field_template'] != 'theme_field') {
1066
    $variables['theme_hook_suggestions'] = array();
1067
    // Either it uses the function.
1068
    $variables['theme_hook_suggestions'][] = str_replace('theme_', '', $field_instance_info['ds_extras_field_template']);
1069
    $variables['theme_hook_suggestions'][] = $field_instance_info['ds_extras_field_template'];
1070
    // Or the template file(s).
1071
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_instance_info['ds_extras_field_template']);
1072
    $variables['theme_hook_suggestions'][] = $suggestion;
1073
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1074
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1075
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1076
  }
1077
  // Use Display Suite Extras Default theming function.
1078
  elseif (!isset($config['func']) || empty($config['func'])) {
1079
    $field_theme_function = variable_get('ft-default', 'theme_field');
1080
    if ($field_theme_function != 'theme_field') {
1081
      $variables['theme_hook_suggestions'] = array();
1082
      $variables['ds-config'] = $config;
1083
      // Either it uses the function.
1084
      $variables['theme_hook_suggestions'][] = str_replace('theme_', '', $field_theme_function);
1085
      $variables['theme_hook_suggestions'][] = $field_theme_function;
1086
      // Or the template file(s).
1087
      $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_theme_function);
1088
      $variables['theme_hook_suggestions'][] = $suggestion;
1089
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1090
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1091
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1092
    }
1093
  }
1094

    
1095
  // Sanitize the output of field templates
1096
  $fields = array(
1097
    'prefix',
1098
    'suffix',
1099
  );
1100

    
1101
  foreach ($fields as $field) {
1102
    if (isset($variables['ds-config'][$field])) {
1103
      $variables['ds-config'][$field] = filter_xss_admin($variables['ds-config'][$field]);
1104
    }
1105
  }
1106
}