Projet

Général

Profil

Paste
Télécharger (36,2 ko) Statistiques
| Branche: | Révision:

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

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_form_alter(&$form, $form_state, $form_id) {
342

    
343
  // Switch full view mode.
344
  if (user_access('ds_switch ' . $form['#node']->type)) {
345

    
346
    $view_mode_settings = field_view_mode_settings('node', $form['#node']->type);
347

    
348
    // Get the view modes.
349
    $ds_vm = ds_entity_view_modes('node');
350
    $layouts = array();
351
    $options = array('' => t('Default'));
352
    foreach ($ds_vm as $key => $item) {
353
      $overriden = (!empty($view_mode_settings[$key]['custom_settings']) ? TRUE : FALSE);
354
      if ($overriden) {
355
        $layout = ds_get_layout('node', $form['#node']->type, $key, FALSE);
356
        $layouts[$key] = $layout;
357
        $options[$key] = $item['label'];
358
      }
359
    }
360

    
361
    // Add default layout settings
362
    $layouts[''] = ds_get_layout('node', $form['#node']->type, 'default', FALSE);
363

    
364
    // Only fire if we have more than 1 option.
365
    if (count($options) > 1) {
366
      $node = $form['#node'];
367

    
368
      if (!isset($form['ds_extras'])) {
369
        $form['ds_extras'] = array(
370
          '#type' => 'fieldset',
371
          '#title' => t('Display settings'),
372
          '#collapsible' => TRUE,
373
          '#collapsed' => TRUE,
374
          '#group' => 'additional_settings',
375
          '#weight' => 100,
376
        );
377
      }
378

    
379
      $form['ds_extras']['ds_switch'] = array(
380
        '#type' => 'select',
381
        '#title' => t('View mode'),
382
        '#options' => $options,
383
        '#default_value' => isset($node->ds_switch) ? $node->ds_switch : '',
384
        '#description' => t('Switch to a different view mode to display the full page view of this node.'),
385
        '#weight' => -1,
386
        '#ajax' => array(
387
          'callback' => 'ds_extras_switch_view_mode_preview_callback',
388
          'wrapper' => 'ds_switch_preview_wrapper',
389
        ),
390
      );
391

    
392
      $form['ds_extras']['preview'] = array(
393
        '#type' => 'container',
394
        '#prefix' => '<div id="ds_switch_preview_wrapper">',
395
        '#suffix' => '</div>',
396
      );
397

    
398
      $mode = isset($form_state['input']['ds_switch']) ? $form_state['input']['ds_switch'] : (isset($node->ds_switch) ? $node->ds_switch : '');
399
      $chosen_layout = $layouts[$mode];
400

    
401
      $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
402
      $image = (isset($chosen_layout['image']) && !empty($chosen_layout['image'])) ? $chosen_layout['path'] . '/' . $chosen_layout['layout'] . '.png' : $fallback_image;
403
      if (isset($chosen_layout['panels']) && !empty($chosen_layout['panels']['icon'])) {
404
        $image = $chosen_layout['panels']['path'] . '/' . $chosen_layout['panels']['icon'];
405
      }
406

    
407
      $form['ds_extras']['preview']['image'] = array(
408
        '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
409
      );
410
    }
411
  }
412
}
413

    
414
/**
415
 * Ajax callback for _ds_field_ui_table_layouts_preview().
416
 */
417
function ds_extras_switch_view_mode_preview_callback($form, $form_state) {
418
  return $form['ds_extras']['preview'];
419
}
420

    
421
/**
422
 * Implements hook_block_info().
423
 */
424
function ds_extras_block_info() {
425

    
426
  $region_blocks = variable_get('ds_extras_region_blocks', array());
427

    
428
  if (empty($region_blocks)) {
429
    return array();
430
  }
431

    
432
  foreach ($region_blocks as $key => $block) {
433
    $blocks[$key] = array(
434
      'info' => $block['title'],
435
      'cache' => DRUPAL_NO_CACHE,
436
    );
437
  }
438
  return $blocks;
439
}
440

    
441
/**
442
 * Implements hook_block_view().
443
 */
444
function ds_extras_block_view($delta = '') {
445
  $data = drupal_static('ds_block_region');
446
  $region_blocks = variable_get('ds_extras_region_blocks', array());
447

    
448
  if (!empty($data[$delta])) {
449
    $block = array();
450
    $block['subject'] = $region_blocks[$delta]['title'];
451
    $block['content'] = $data[$delta];
452
    return $block;
453
  }
454
}
455

    
456
/**
457
 * Implements hook_ds_layout_region_alter().
458
 */
459
function ds_extras_ds_layout_region_alter($context, &$region_info) {
460

    
461
  $region_blocks = variable_get('ds_extras_region_blocks', array());
462

    
463
  // Bail out if region_blocks is empty or we are working on default view mode.
464
  if (empty($region_blocks) || $context['view_mode'] == 'default') {
465
    return;
466
  }
467

    
468
  $entity_type = $context['entity_type'];
469
  $bundle = $context['bundle'];
470
  $view_mode = $context['view_mode'];
471

    
472
  foreach ($region_blocks as $block_key => $block) {
473
    if ($block['info'] == "{$entity_type}_{$bundle}_{$view_mode}") {
474
      $region_info['region_options'][$block_key] = $block['title'];
475
      if (isset($region_info['table_regions'])) {
476
        $region_info['table_regions'][$block_key] = array(
477
          'title' => check_plain($block['title']),
478
          'message' => t('No fields are displayed in this region'),
479
        );
480
      }
481
    }
482
  }
483
}
484

    
485
/**
486
 * Implements hook_field_extra_fields().
487
 */
488
function ds_extras_field_extra_fields() {
489
  $extra = array();
490

    
491
  // Register a single field so fields for vd are
492
  // picked up nicely in the display overview form.
493
  if (variable_get('ds_extras_vd')) {
494
    ctools_include('export');
495
    $vd_settings = ctools_export_crud_load_all('ds_vd');
496
    foreach ($vd_settings as $vd) {
497
      $extra['ds_views'][$vd->vd] = array(
498
        'display' => array(
499
          'title' => array(
500
            'label' => t('Title'),
501
            'description' => t('Title'),
502
            'weight' => 10,
503
          ),
504
        ),
505
      );
506
    }
507
  }
508

    
509
  if (variable_get('ds_extras_fields_extra')) {
510
    $fields = explode("\n", variable_get('ds_extras_fields_extra_list', FALSE));
511
    foreach ($fields as $field) {
512
      $field = trim($field);
513
      if (!empty($field)) {
514
        list($entity, $bundle, $field_name) = explode('|', $field);
515
        $extra[check_plain($entity)][check_plain($bundle)]['display'][$field_name] = array(
516
          'label' => drupal_ucfirst(str_replace('_', ' ', check_plain($field_name))),
517
          'description' => drupal_ucfirst(str_replace('_', ' ', check_plain($field_name))),
518
          'weight' => 0,
519
        );
520
      }
521
    }
522
  }
523

    
524
  return $extra;
525
}
526

    
527
/**
528
 * Output flag.
529
 */
530
function ds_extras_flags_add_flag_link($field) {
531
  return flag_create_link($field['properties']['flag'], $field['entity']->nid);
532
}
533

    
534
/**
535
 * Implements hook_preprocess_views_view().
536
 */
537
function ds_extras_preprocess_view_layout(&$variables) {
538

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

    
541
    $variables['elements']['#entity_type'] = $variables['#entity_type'] = 'ds_views';
542
    $variables['elements']['#bundle'] = $variables['#bundle'] = $variables['view']->name . '-' . $variables['view']->current_display;
543
    $variables['elements']['#view_mode'] = 'default';
544

    
545
    $variables['ds_views'] = $variables['view'];
546
    $variables['render_code_fields'] = TRUE;
547
    ds_field_attach_view_alter($variables, array('view_mode' => 'default', 'entity' => $variables['view']));
548

    
549
    // Special case for views function fields.
550
    if (isset($variables['view']->ds_vd)) {
551
      foreach ($variables['view']->ds_vd as $key => $value) {
552
        $variables[$key] = $value;
553
      }
554
    }
555

    
556
    // Don't remove the variables so we don't trigger notices.
557
    $variables['preprocess_keep'] = TRUE;
558
    ds_entity_variables($variables);
559
    if (isset($variables['#attached'])) {
560
      drupal_process_attached($variables);
561
    }
562
  }
563
}
564

    
565
/**
566
 * Function used for theming the views title, user name etc. Note that
567
 * this is a function so it can't be overridden by a phptemplate function.
568
 */
569
function ds_vd_render_title_field($field) {
570
  $output = '';
571
  $formatter = explode('_', $field['formatter']);
572
  $tag = $formatter[2];
573
  $output = '<' . $tag . '>' . $field['entity']->get_title() . '</' . $tag . '>';
574

    
575
  // Views is a special case, we stack title on the entity.
576
  $field['entity']->preprocess_fields[] = 'title';
577
  $field['entity']->ds_vd['title'] = $output;
578
}
579

    
580
/**
581
 * Implements hook_entity_view_alter().
582
 */
583
function ds_extras_entity_view_alter(&$build, $entity_type) {
584
  static $loaded = array();
585

    
586
  // If views and core doesn't send information along on the entity,
587
  // Display Suite doesn't have a context to render the layout.
588
  if (!isset($build['#entity_type']) || !isset($build['#bundle'])) {
589
    return;
590
  }
591

    
592
  $bundle = $build['#bundle'];
593
  $view_mode = $build['#view_mode'];
594
  if ($overridden_view_mode = ds_extras_get_view_mode()) {
595
    $view_mode = $overridden_view_mode;
596
  }
597
  $layout = ds_get_layout($entity_type, $bundle, $view_mode);
598

    
599
  // Page title options.
600
  if (variable_get('ds_extras_hide_page_title', FALSE)) {
601
    $page_title = &drupal_static('ds_page_title');
602
    if (isset($layout['settings']['hide_page_title']) && $layout['settings']['hide_page_title'] == 1 && ds_extras_is_entity_page_view($build, $entity_type)) {
603
      $page_title['title'] = '';
604
    }
605
    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)) {
606
      $contexts = array();
607
      $id = (arg(0) == 'taxonomy') ? arg(2) : arg(1);
608
      $entity = entity_load($entity_type, array($id));
609
      ds_create_entity_context($entity_type, $entity[$id], $contexts);
610
      $title = $layout['settings']['page_option_title'];
611
      $title = filter_xss_admin(ctools_context_keyword_substitute($title, array(), $contexts));
612
      $page_title['title'] = $title;
613
      $page_title['head_title'] = $title;
614
    }
615
  }
616

    
617
  // Disable blocks.
618
  if (isset($layout['settings']['hide_sidebars']) && !isset($loaded[$entity_type][$bundle][$view_mode])) {
619

    
620
    // Store the setting.
621
    $loaded[$entity_type][$bundle][$view_mode] = TRUE;
622

    
623
    // Disable blocks.
624
    if (isset($layout['settings']['hide_sidebars']) && $layout['settings']['hide_sidebars']) {
625
      ctools_set_no_blocks();
626
    }
627
  }
628
}
629

    
630
/**
631
 * Helper function to check if this is a page view.
632
 *
633
 * The page title option adds the ability to hide or override the page title.
634
 * However, it can't happen that an entity bleeds its page title to say a view
635
 * or the frontpage.
636
 *
637
 * See http://drupal.org/node/1526732 and http://drupal.org/node/1446554.
638
 */
639
function ds_extras_is_entity_page_view($build, $entity_type) {
640
  switch ($entity_type) {
641
    case 'node':
642
      return node_is_page($build['#node']);
643
      break;
644
    case 'user':
645
    $page_account = menu_get_object('user');
646
    return (!empty($page_account) ? $page_account->uid == $build['#account']->uid : FALSE);
647
    break;
648
    case 'taxonomy_term':
649
      $page_term = menu_get_object('taxonomy_term', 2);
650
      return (!empty($page_term) ? $page_term->tid == $build['#term']->tid : FALSE);
651
      break;
652
    case 'profile2':
653
      return $build['#view_mode'] == 'page';
654
      break;
655
  }
656

    
657
  return FALSE;
658
}
659

    
660
/**
661
 * Implements hook_ds_field_theme_functions_info().
662
 */
663
function ds_extras_ds_field_theme_functions_info() {
664
  return array(
665
    'theme_field' => t('Drupal default'),
666
    'theme_ds_field_reset' => t('Full Reset'),
667
    'theme_ds_field_minimal' => t('Minimal'),
668
    'theme_ds_field_expert' => t('Expert'),
669
  );
670
}
671

    
672
/**
673
 * Reset all HTML for the field.
674
 */
675
function theme_ds_field_reset($variables) {
676
  $output = '';
677

    
678
  // Render the label.
679
  if (!$variables['label_hidden']) {
680
    $output .= '<div class="label-' . $variables['element']['#label_display'] . '">' . $variables['label'];
681
    if (!variable_get('ft-kill-colon', FALSE)) {
682
      $output .= ':&nbsp;';
683
    }
684
    $output .= '</div>';
685
  }
686

    
687
  // Render the items.
688
  foreach ($variables['items'] as $delta => $item) {
689
    $output .= drupal_render($item);
690
  }
691

    
692
  return $output;
693
}
694

    
695
/**
696
 * Provide minimal HTML for the field.
697
 */
698
function theme_ds_field_minimal($variables) {
699
  $output = '';
700
  $config = $variables['ds-config'];
701
  $classes = isset($config['classes']) ? ' ' . $config['classes'] : '';
702

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

    
706
  // Render the label.
707
  if (!$variables['label_hidden']) {
708
    $output .= '<div class="label-' . $variables['element']['#label_display'] . '">' . $variables['label'];
709
    if (!isset($config['lb-col'])) {
710
      $output .= ':&nbsp;';
711
    }
712
    $output .= '</div>';
713
  }
714

    
715
  // Render the items.
716
  foreach ($variables['items'] as $delta => $item) {
717
    $output .= drupal_render($item);
718
  }
719
  $output .="</div>";
720

    
721
  return $output;
722
}
723

    
724
/**
725
 * Custom output all HTML for the field.
726
 */
727
function theme_ds_field_expert($variables) {
728
  $output = '';
729

    
730
  $config = $variables['ds-config'];
731

    
732
  // Set prefix
733
  if (!empty($config['prefix'])) {
734
    $output .= $config['prefix'];
735
  }
736

    
737
  // Render the label if it's not hidden.
738
  if (!$variables['label_hidden']) {
739
    $styled_label = $variables['label'];
740
    if (!isset($config['lb-col'])) $styled_label .= ':&nbsp;';
741

    
742
    // Style the label it self
743
    $label_wrapper = isset($config['lb-el']) ? $config['lb-el'] : 'div';
744
    $class = array('label-' . $variables['element']['#label_display']);
745
    if (!empty($config['lb-cl'])) $class[] = $config['lb-cl'];
746
    $class = !empty($class) ? ' class="' . implode(' ', $class) . '"' : '';
747
    $attributes = array();
748
    if (!empty($config['lb-at'])) $attributes[] = $config['lb-at'];
749
    if (!empty($config['lb-def-at'])) $attributes[] = $variables['title_attributes'];
750
    $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
751
    $styled_label = '<' . $label_wrapper . $class . $attributes . '>' . $styled_label . '</' . $label_wrapper . '>';
752

    
753
    // Place it inside a wrapper
754
    if (isset($config['lbw'])) {
755
      $class = !empty($config['lbw-cl']) ? ' class="' . $config['lbw-cl'] . '"' : '';
756
      $attributes = !empty($config['lbw-at']) ?  ' ' . $config['lbw-at'] : '';
757
      $styled_label = '<' . $config['lbw-el'] . $class . $attributes . '>' . $styled_label . '</' . $config['lbw-el'] . '>';
758
    }
759

    
760
    $output .= $styled_label;
761
  }
762

    
763
  // Field items wrapper
764
  if (isset($config['fis'])) {
765
    $class = (!empty($config['fis-cl'])) ? ' class="' . token_replace($config['fis-cl'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE)) . '"' : '';
766
    $attributes = array();
767
    if (!empty($config['fis-at'])) $attributes[] = token_replace($config['fis-at'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE));;
768
    if (!empty($config['fis-def-at'])) $attributes[] = $variables['content_attributes'];
769
    $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
770
    $output .= '<' . $config['fis-el'] . $class . $attributes . '>';
771
  }
772

    
773
  // Render items.
774
  foreach ($variables['items'] as $delta => $item) {
775
    // Field item wrapper.
776
    if (isset($config['fi'])) {
777
      $class = array();
778
      if (!empty($config['fi-odd-even'])) {
779
        $class[] = $delta % 2 ? 'even' : 'odd';
780
      }
781
      if (!empty($config['fi-cl'])) {
782
        $class[] = $config['fi-cl'];
783
      }
784
      $class = !empty($class) ? ' class="'. token_replace(implode(' ', $class), array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE)) .'"' : '';
785
      $attributes = array();
786
      if (!empty($config['fi-at'])) {
787
        $attributes[] = token_replace($config['fi-at'], array($variables['element']['#entity_type'] => $variables['element']['#object']), array('clear' => TRUE));
788
      }
789
      if (!empty($config['fi-def-at'])) {
790
        $attributes[] = $variables['item_attributes'][$delta];
791
      }
792
      $attributes = (!empty($attributes)) ? ' ' . implode(' ', $attributes) : '';
793
      $output .= '<' . $config['fi-el'] . $class . $attributes .'>';
794
    }
795

    
796
    // Render field content.
797
    $output .= drupal_render($item);
798

    
799
    // Closing field item wrapper.
800
    if (isset($config['fi'])) {
801
      $output .= '</' . $config['fi-el'] . '>';
802
    }
803
  }
804

    
805
  // Closing field items wrapper.
806
  if (isset($config['fis'])) {
807
    $output .= '</' . $config['fis-el'] . '>';
808
  }
809

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

    
823
  // Set suffix
824
  if (!empty($config['suffix'])) {
825
    $output .= $config['suffix'];
826
  }
827

    
828
  return $output;
829
}
830

    
831
/**
832
 * Implements hook_ds_field_settings_alter().
833
 */
834
function ds_extras_ds_field_settings_alter(&$field_settings, $form, &$form_state) {
835

    
836
  $fields = $form_state['values']['fields'];
837
  $default_field_function = variable_get('ft-default', 'theme_field');
838

    
839
  $wrappers = array(
840
    'ow' => t('Wrapper'),
841
    'fis' => t('Field items'),
842
    'fi' => t('Field item')
843
  );
844

    
845
  foreach ($fields as $key => $field) {
846

    
847
    // Make sure we need to save anything for this field.
848
    if (_ds_field_valid($key, $field, $form_state)) {
849
      continue;
850
    }
851

    
852
    // Get the values.
853
    $values = isset($form_state['formatter_settings'][$key]['ft']) ? $form_state['formatter_settings'][$key]['ft'] : array();
854
    if (empty($values)) {
855
      continue;
856
    }
857
    $field_settings[$key]['formatter_settings']['ft'] = array();
858

    
859
    // Theme output function.
860
    $function = isset($values['func']) ? $values['func'] : $default_field_function;
861
    if ($function != $default_field_function) {
862
      $field_settings[$key]['formatter_settings']['ft']['func'] = $function;
863
    }
864

    
865
    // Field classes.
866
    if ($function != 'theme_ds_field_expert' && $function != 'theme_ds_field_reset' && isset($values['classes'])) {
867
      $classes = is_array($values['classes']) ? implode(' ', $values['classes']) : $values['classes'];
868
      if (!empty($classes)) {
869
        $field_settings[$key]['formatter_settings']['ft']['classes'] = $classes;
870
      }
871
    }
872

    
873
    // Label.
874
    if (isset($form_state['values']['fields'][$key]['label'])) {
875
      if (!empty($values['lb'])) {
876
        $field_settings[$key]['formatter_settings']['ft']['lb'] = $values['lb'];
877
      }
878
      if (!(empty($values['lb-el'])) && $function == 'theme_ds_field_expert') {
879
        $field_settings[$key]['formatter_settings']['ft']['lb-el'] = check_plain($values['lb-el']);
880
      }
881
      if (!(empty($values['lb-cl'])) && $function == 'theme_ds_field_expert') {
882
        $field_settings[$key]['formatter_settings']['ft']['lb-cl'] = check_plain($values['lb-cl']);
883
      }
884
      if (!(empty($values['lb-at'])) && $function == 'theme_ds_field_expert') {
885
       $field_settings[$key]['formatter_settings']['ft']['lb-at'] = filter_xss($values['lb-at']);
886
      }
887
      if (!(empty($values['lb-def-at'])) && $function == 'theme_ds_field_expert') {
888
       $field_settings[$key]['formatter_settings']['ft']['lb-def-at'] = TRUE;
889
      }
890
      if (!(empty($values['lb-col']))) {
891
        $field_settings[$key]['formatter_settings']['ft']['lb-col'] = TRUE;
892
      }
893

    
894
      // label wrapper
895
      if (!empty($values['lbw'])) {
896
        $field_settings[$key]['formatter_settings']['ft']['lbw'] = $values['lbw'];
897
      }
898
      if (!(empty($values['lbw-el'])) && $function == 'theme_ds_field_expert') {
899
        $field_settings[$key]['formatter_settings']['ft']['lbw-el'] = check_plain($values['lbw-el']);
900
      }
901
      if (!(empty($values['lbw-cl'])) && $function == 'theme_ds_field_expert') {
902
        $field_settings[$key]['formatter_settings']['ft']['lbw-cl'] = check_plain($values['lbw-cl']);
903
      }
904
      if (!(empty($values['lbw-at'])) && $function == 'theme_ds_field_expert') {
905
       $field_settings[$key]['formatter_settings']['ft']['lbw-at'] = filter_xss($values['lbw-at']);
906
      }
907
    }
908

    
909
    if (!(empty($values['prefix']))) {
910
      $field_settings[$key]['formatter_settings']['ft']['prefix'] = $values['prefix'];
911
    }
912
    if (!(empty($values['suffix']))) {
913
      $field_settings[$key]['formatter_settings']['ft']['suffix'] = $values['suffix'];
914
    }
915

    
916
    // Custom field configuration.
917
    if ($function == 'theme_ds_field_expert') {
918
      foreach ($wrappers as $wrapper_key => $title) {
919
        if (!empty($values[$wrapper_key])) {
920
          // Enable.
921
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key] = TRUE;
922
          // Element.
923
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-el'] = !(empty($values[$wrapper_key . '-el'])) ? check_plain($values[$wrapper_key . '-el']) : 'div';
924
          // Classes.
925
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-cl'] = !(empty($values[$wrapper_key . '-cl'])) ? check_plain($values[$wrapper_key . '-cl']) : '';
926
          // Default Classes.
927
          if (in_array($wrapper_key, array('ow', 'lb'))) {
928
            $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-def-cl'] = !(empty($values[$wrapper_key . '-def-cl'])) ? TRUE : FALSE;
929
          }
930
          // Attributes.
931
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-at'] = !(empty($values[$wrapper_key . '-at'])) ? filter_xss($values[$wrapper_key . '-at']) : '';
932
          // Default attributes.
933
          $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-def-at'] = !(empty($values[$wrapper_key . '-def-at'])) ? TRUE : FALSE;
934
          // Odd even class.
935
          if ($wrapper_key == 'fi') {
936
            $field_settings[$key]['formatter_settings']['ft'][$wrapper_key . '-odd-even'] = !(empty($values[$wrapper_key . '-odd-even'])) ? TRUE : FALSE;
937
          }
938
        }
939
      }
940
    }
941
  }
942
}
943

    
944
/**
945
 * Implements hook_preprocess_field().
946
 */
947
function ds_extras_preprocess_field(&$variables) {
948

    
949
  // We need to be sure this field is in a layout which is rendered by DS.
950
  if (!ds_get_layout($variables['element']['#entity_type'], $variables['element']['#bundle'], $variables['element']['#view_mode'])) {
951
    return;
952
  }
953

    
954
  $entity_type = $variables['element']['#entity_type'];
955
  $bundle = $variables['element']['#bundle'];
956
  $view_mode = $variables['element']['#view_mode'];
957

    
958
  $config = array();
959
  static $field_settings = array();
960
  if (!isset($field_settings[$entity_type][$bundle][$view_mode])) {
961
    $layout = ds_get_layout($entity_type, $bundle, $view_mode);
962
    $field_settings[$entity_type][$bundle][$view_mode] = ds_get_field_settings($entity_type, $bundle, $view_mode);
963
  }
964

    
965
  // Get the field name and field instance info - if available.
966
  $field_name = $variables['element']['#field_name'];
967
  $field_instance_info = field_info_instance($entity_type, $field_name, $bundle);
968

    
969
  // Check if this field has custom output settings.
970
  $variables['ds-config'] = array();
971
  if (isset($field_settings[$entity_type][$bundle][$view_mode][$field_name]['formatter_settings']['ft'])) {
972
    $config = $field_settings[$entity_type][$bundle][$view_mode][$field_name]['formatter_settings']['ft'];
973
    $variables['ds-config'] = $config;
974
  }
975

    
976
  // CSS classes
977
  if (isset($config['classes'])) {
978
    $variables['classes_array'][] = $config['classes'];
979
  }
980

    
981
  // Alter the label if configured.
982
  if (!$variables['label_hidden']) {
983
    if (isset($config['lb'])) {
984
      $variables['label'] = t(check_plain($config['lb']));
985
    }
986
  }
987

    
988
  // Determine the field template. In case it's something different
989
  // than theme_field, we'll add that function as a suggestion.
990
  if (isset($config['func']) && $config['func'] != 'theme_field') {
991
    $variables['ds-config'] = $config;
992
    $variables['theme_hook_suggestions'] = array();
993
    // Either it uses the function.
994
    $variables['theme_hook_suggestions'][] = $config['func'];
995
    // Or the template file(s).
996
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $config['func']);
997
    $variables['theme_hook_suggestions'][] = $suggestion;
998
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
999
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1000
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1001
  }
1002
  // Check if we have a default field template on instance level.
1003
  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') {
1004
    $variables['theme_hook_suggestions'] = array();
1005
    // Either it uses the function.
1006
    $variables['theme_hook_suggestions'][] = $field_instance_info['ds_extras_field_template'];
1007
    // Or the template file(s).
1008
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_instance_info['ds_extras_field_template']);
1009
    $variables['theme_hook_suggestions'][] = $suggestion;
1010
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1011
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1012
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1013
  }
1014
  // Use Display Suite Extras Default theming function.
1015
  elseif (!isset($config['func']) || empty($config['func'])) {
1016
    $field_theme_function = variable_get('ft-default', 'theme_field');
1017
    if ($field_theme_function != 'theme_field') {
1018
      $variables['theme_hook_suggestions'] = array();
1019
      $variables['ds-config'] = $config;
1020
      // Either it uses the function.
1021
      $variables['theme_hook_suggestions'][] = $field_theme_function;
1022
      // Or the template file(s).
1023
      $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_theme_function);
1024
      $variables['theme_hook_suggestions'][] = $suggestion;
1025
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
1026
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
1027
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
1028
    }
1029
  }
1030

    
1031
  // Sanitize the output of field templates
1032
  $fields = array(
1033
    'prefix',
1034
    'suffix',
1035
  );
1036

    
1037
  foreach ($fields as $field) {
1038
    if (isset($variables['ds-config'][$field])) {
1039
      $variables['ds-config'][$field] = filter_xss($variables['ds-config'][$field]);
1040
    }
1041
  }
1042
}