Projet

Général

Profil

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

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

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 funtion 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
        $chosen_layout = (isset($layouts[$mode])) ? $layouts[$mode] : 'default';
429

    
430
        $image = (isset($chosen_layout['image']) && !empty($chosen_layout['image'])) ? $chosen_layout['path'] . '/' . $chosen_layout['layout'] . '.png' : $fallback_image;
431
        if (isset($chosen_layout['panels']) && !empty($chosen_layout['panels']['icon'])) {
432
          $image = $chosen_layout['panels']['path'] . '/' . $chosen_layout['panels']['icon'];
433
        }
434
      }
435
      else {
436
        $image = $fallback_image;
437
      }
438

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

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

    
452
  $view_mode_settings = field_view_mode_settings($type, $bundle);
453
  $ds_vm = ds_entity_view_modes($type);
454

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

    
462
  return $view_modes;
463
}
464

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

    
472
/**
473
 * Implements hook_block_info().
474
 */
475
function ds_extras_block_info() {
476

    
477
  $region_blocks = variable_get('ds_extras_region_blocks', array());
478

    
479
  if (empty($region_blocks)) {
480
    return array();
481
  }
482

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

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

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

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

    
512
  $region_blocks = variable_get('ds_extras_region_blocks', array());
513

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

    
519
  $entity_type = $context['entity_type'];
520
  $bundle = $context['bundle'];
521
  $view_mode = $context['view_mode'];
522

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

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

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

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

    
575
  return $extra;
576
}
577

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
710
  return FALSE;
711
}
712

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

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

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

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

    
745
  return $output;
746
}
747

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

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

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

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

    
774
  return $output;
775
}
776

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

    
783
  $config = $variables['ds-config'];
784

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

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

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

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

    
813
    $output .= $styled_label;
814
  }
815

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

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

    
857
    // Render field content.
858
    $output .= drupal_render($item);
859

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

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

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

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

    
890
  return $output;
891
}
892

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

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

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

    
907
  foreach ($fields as $key => $field) {
908

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

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

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

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

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

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

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

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

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

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

    
1017
  $entity_type = $variables['element']['#entity_type'];
1018
  $bundle = $variables['element']['#bundle'];
1019
  $view_mode = $variables['element']['#view_mode'];
1020

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

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

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

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

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

    
1051
  // Determine the field template. In case it's something different
1052
  // than theme_field, we'll add that function as a suggestion.
1053
  if (isset($config['func']) && $config['func'] != 'theme_field') {
1054
    $variables['ds-config'] = $config;
1055
    $variables['theme_hook_suggestions'] = array();
1056
    // Either it uses the function.
1057
    $variables['theme_hook_suggestions'][] = $config['func'];
1058
    // Or the template file(s).
1059
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $config['func']);
1060
    $variables['theme_hook_suggestions'][] = $suggestion;
1061
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1062
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1063
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1064
  }
1065
  // Check if we have a default field template on instance level.
1066
  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') {
1067
    $variables['theme_hook_suggestions'] = array();
1068
    // Either it uses the function.
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'][] = $field_theme_function;
1085
      // Or the template file(s).
1086
      $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_theme_function);
1087
      $variables['theme_hook_suggestions'][] = $suggestion;
1088
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1089
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1090
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1091
    }
1092
  }
1093

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

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