Projet

Général

Profil

Paste
Télécharger (79,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_group / field_group.module @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Fieldgroup module.
6
 *
7
 * For an overview of all php and javascript hooks, see field_group.api.php.
8
 *
9
 */
10

    
11
/**
12
 * Implements hook_menu().
13
 */
14
function field_group_menu() {
15
  $items = array();
16

    
17
  // Ensure the following is not executed until field_bundles is working and
18
  // tables are updated. Needed to avoid errors on initial installation.
19
  if (defined('MAINTENANCE_MODE')) {
20
    return $items;
21
  }
22

    
23
  // Create tabs for all possible bundles.
24
  foreach (entity_get_info() as $entity_type => $entity_info) {
25
    if (isset($entity_info['fieldable']) && $entity_info['fieldable']) {
26
      foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
27
        if (isset($bundle_info['admin'])) {
28
          // Extract path information from the bundle.
29
          $path = $bundle_info['admin']['path'];
30
          // Different bundles can appear on the same path (e.g. %node_type and
31
          // %comment_node_type). To allow field_group_menu_load() to extract the
32
          // actual bundle object from the translated menu router path
33
          // arguments, we need to identify the argument position of the bundle
34
          // name string ('bundle argument') and pass that position to the menu
35
          // loader. The position needs to be casted into a string; otherwise it
36
          // would be replaced with the bundle name string.
37
          if (isset($bundle_info['admin']['bundle argument'])) {
38
            $bundle_arg = $bundle_info['admin']['bundle argument'];
39
            $bundle_pos = (string) $bundle_arg;
40
          }
41
          else {
42
            $bundle_arg = $bundle_name;
43
            $bundle_pos = '0';
44
          }
45

    
46
          // This is the position of the %field_group_menu placeholder in the
47
          // items below.
48
          $group_position = count(explode('/', $path)) + 1;
49

    
50
          // Extract access information, providing defaults.
51
          $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
52
          $access += array(
53
            'access callback' => 'user_access',
54
            'access arguments' => array('administer site configuration'),
55
          );
56

    
57
          $items["$path/groups/%field_group_menu/delete"] = array(
58
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
59
            'title' => 'Delete',
60
            'page callback' => 'drupal_get_form',
61
            'page arguments' => array('field_group_delete_form', $group_position),
62
            'type' => MENU_CALLBACK,
63
            'file' => 'field_group.field_ui.inc',
64
          ) + $access;
65

    
66
          $items["$path/groups/%field_group_menu/enable"] = array(
67
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
68
            'title' => 'Enable',
69
            'page callback' => 'drupal_get_form',
70
            'page arguments' => array('field_group_enable_form', $group_position),
71
            'type' => MENU_CALLBACK,
72
            'file' => 'field_group.field_ui.inc',
73
          ) + $access;
74

    
75
        }
76
      }
77
    }
78
  }
79

    
80
  return $items;
81
}
82

    
83
/**
84
 * Implements hook_permission().
85
 */
86
function field_group_permission() {
87
  return array(
88
    'administer fieldgroups' => array(
89
      'title' => t('Administer fieldgroups'),
90
      'description' => t('Display the administration for fieldgroups.'),
91
    ),
92
  );
93
}
94

    
95
/**
96
 * Menu Wildcard loader function to load group definitions.
97
 *
98
 * @param $group_name
99
 *   The name of the group, as contained in the path.
100
 * @param $entity_type
101
 *   The name of the entity.
102
 * @param $bundle_name
103
 *   The name of the bundle, as contained in the path.
104
 * @param $bundle_pos
105
 *   The position of $bundle_name in $map.
106
 * @param $map
107
 *   The translated menu router path argument map.
108
 */
109
function field_group_menu_load($group_name, $entity_type, $bundle_name, $bundle_pos, $map) {
110

    
111
  if ($bundle_pos > 0) {
112
    $bundle = $map[$bundle_pos];
113
    $bundle_name = field_extract_bundle($entity_type, $bundle);
114
  }
115

    
116
  $args = func_get_args();
117
  $args_pop = array_pop($args);
118
  $mode = array_pop($args_pop);
119

    
120
  $group = field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode);
121

    
122
  return empty($group) ? FALSE : $group;
123
}
124

    
125
/**
126
 * Ctools load callback to load fieldgroup by identifier.
127
 */
128
function field_group_load_field_group_by_identifier($identifier) {
129

    
130
  $parts = explode('|', $identifier);
131
  if (count($parts) != 4) {
132
    return;
133
  }
134

    
135
  return field_group_load_field_group($parts[0], $parts[1], $parts[2], $parts[3]);
136

    
137
}
138

    
139
/**
140
 * Loads a group definition.
141
 *
142
 * @param $group_name
143
 *   The name of the group.
144
 * @param $entity_type
145
 *   The name of the entity.
146
 * @param $bundle_name
147
 *   The name of the bundle.
148
 * @param $mode
149
 *   The view mode to load.
150
 */
151
function field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode) {
152

    
153
  ctools_include('export');
154
  $objects = ctools_export_load_object('field_group', 'conditions', array(
155
    'group_name' => $group_name,
156
    'entity_type' => $entity_type,
157
    'bundle' => $bundle_name,
158
    'mode' => $mode,
159
  ));
160
  $object = array_shift($objects);
161

    
162
  if ($object && isset($object->data)) {
163
    return field_group_unpack($object);
164
  }
165

    
166
  return $object;
167
}
168

    
169
/**
170
 * Implements hook_ctools_plugin_api().
171
 */
172
function field_group_ctools_plugin_api($owner, $api) {
173
  if ($owner == 'field_group' && $api == 'field_group') {
174
    return array('version' => 1);
175
  }
176
}
177

    
178
/**
179
 * Implements hook_theme().
180
 */
181
function field_group_theme() {
182
  return array(
183
    'horizontal_tabs' => array(
184
      'render element' => 'element',
185
    ),
186
    'multipage' => array(
187
      'render element' => 'element',
188
    ),
189
    'multipage_pane' => array(
190
      'render element' => 'element',
191
    ),
192
  );
193
}
194

    
195
/**
196
 * Implements hook_theme_registry_alter().
197
 */
198
function field_group_theme_registry_alter(&$theme_registry) {
199

    
200
  // Inject field_group_build_entity_groups in all entity theming functions.
201
  $entity_info = entity_get_info();
202
  $entities = array();
203
  foreach ($entity_info as $entity => $info) {
204
    if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {
205
      // User uses user_profile for theming.
206
      if ($entity == 'user') $entity = 'user_profile';
207
      $entities[] = $entity;
208
    }
209
  }
210

    
211
  // Support for File Entity.
212
  if (isset($theme_registry['file_entity'])) {
213
    $entities[] = 'file_entity';
214
  }
215

    
216
  // Support for Entity API.
217
  if (isset($theme_registry['entity'])) {
218
    $entities[] = 'entity';
219
  }
220

    
221
  foreach ($entities as $entity) {
222
    if (isset($theme_registry[$entity])) {
223
      $theme_registry[$entity]['preprocess functions'][] = 'field_group_build_entity_groups';
224
      // DS support, make sure it comes after field_group.
225
      if ($key = array_search('ds_entity_variables', $theme_registry[$entity]['preprocess functions'])) {
226
        unset($theme_registry[$entity]['preprocess functions'][$key]);
227
        $theme_registry[$entity]['preprocess functions'][] = 'ds_entity_variables';
228
      }
229
    }
230
  }
231

    
232
}
233

    
234
/**
235
 * Implements hook_field_attach_delete_bundle().
236
 *
237
 * @param String $entity_type
238
 * @param String $bundle
239
 */
240
function field_group_field_attach_delete_bundle($entity_type, $bundle) {
241

    
242
  ctools_include('export');
243
  $list = field_group_read_groups(array('bundle' => $bundle, 'entity_type' => $entity_type));
244

    
245
  // Delete the entity's entry from field_group of all entities.
246
  // We fetch the field groups first to assign the removal task to ctools.
247
  if (isset($list[$entity_type], $list[$entity_type][$bundle])) {
248
    foreach ($list[$entity_type][$bundle] as $group_mode => $groups) {
249
      foreach ($groups as $group) {
250
        ctools_export_crud_delete('field_group', $group);
251
      }
252
    }
253
  }
254

    
255
}
256

    
257
/**
258
 * Implements hook_field_attach_form().
259
 */
260
function field_group_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
261

    
262
  $form['#attached']['css'][] = drupal_get_path('module', 'field_group') . '/field_group.field_ui.css';
263
  field_group_attach_groups($form, 'form', $form_state);
264
  $form['#pre_render'][] = 'field_group_form_pre_render';
265
}
266

    
267
/**
268
 * Implements hook_form_FORM_ID_alter().
269
 * Using hook_form_field_ui_field_overview_form_alter.
270
 */
271
function field_group_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
272
  form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
273
  field_group_field_ui_overview_form_alter($form, $form_state);
274
}
275

    
276
/**
277
 * Implements hook_form_FORM_ID_alter().
278
 * Using hook_form_field_ui_display_overview_form_alter.
279
 */
280
function field_group_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
281
  form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
282
  field_group_field_ui_overview_form_alter($form, $form_state, TRUE);
283
}
284

    
285
/**
286
 * Implements hook_field_attach_view_alter().
287
 */
288
function field_group_field_attach_view_alter(&$element, $context) {
289
  // Check whether the view mode uses custom display settings or the 'default' mode.
290
  $actual_mode = 'default';
291
  if (isset($element['#entity_type']) && isset($element['#bundle'])) {
292
    $view_mode_settings = field_view_mode_settings($element['#entity_type'], $element['#bundle']);
293
    $view_mode = $context['view_mode'];
294
    $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
295
    field_group_attach_groups($element, $actual_mode);
296
  }
297
}
298

    
299
/**
300
 * Implements hook_field_group_formatter_info().
301
 */
302
function field_group_field_group_formatter_info() {
303

    
304
  return array(
305
    'form' => array(
306
      'html-element' => array(
307
        'label' => t('HTML element'),
308
        'description' => t('This fieldgroup renders the inner content in a HTML element with classes and attributes.'),
309
        'instance_settings' => array('element' => 'div', 'show_label' => 0, 'label_element' => 'div', 'classes' => '', 'attributes' => '', 'required_fields' => 1, 'id' => ''),
310
      ),
311
      'div' => array(
312
        'label' => t('Div'),
313
        'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
314
        'format_types' => array('open', 'collapsible', 'collapsed'),
315
        'instance_settings' => array('description' => '', 'show_label' => 1, 'label_element' => 'h3', 'effect' => 'none', 'speed' => 'fast', 'classes' => '', 'required_fields' => 1, 'id' => ''),
316
        'default_formatter' => 'open',
317
      ),
318
      'html5' => array(
319
        'label' => t('HTML5'),
320
        'description' => t('This fieldgroup renders the inner content in a semantic HTML5 wrapper'),
321
        'instance_settings' => array('wrapper' => '', 'classes' => '', 'id' => ''),
322
      ),
323
      'fieldset' => array(
324
        'label' => t('Fieldset'),
325
        'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
326
        'format_types' => array('open', 'collapsible', 'collapsed'),
327
        'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1, 'id' => ''),
328
        'default_formatter' => 'collapsible',
329
      ),
330
      'tabs' => array(
331
        'label' => t('Vertical tabs group'),
332
        'description' => t('This fieldgroup renders child groups in its own vertical tabs wrapper.'),
333
        'instance_settings' => array('classes' => '', 'id' => ''),
334
      ),
335
      'tab' => array(
336
        'label' => t('Vertical tab'),
337
        'description' => t('This fieldgroup renders the content in a fieldset, part of vertical tabs group.'),
338
        'format_types' => array('open', 'closed'),
339
        'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
340
        'default_formatter' => 'closed',
341
      ),
342
      'htabs' => array(
343
        'label' => t('Horizontal tabs group'),
344
        'description' => t('This fieldgroup renders child groups in its own horizontal tabs wrapper.'),
345
        'instance_settings' => array('classes' => '', 'id' => ''),
346
      ),
347
      'htab' => array(
348
        'label' => t('Horizontal tab'),
349
        'format_types' => array('open', 'closed'),
350
        'description' => t('This fieldgroup renders the content in a fieldset, part of horizontal tabs group.'),
351
        'default_formatter' => 'closed',
352
        'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
353
      ),
354
      'multipage-group' => array(
355
        'label' => t('Multipage group'),
356
        'description' => t('This fieldgroup renders groups on separate pages.'),
357
        'instance_settings' => array('classes' => '', 'page_header' => 3, 'move_additional' => 1, 'page_counter' => 1, 'move_button' => 0),
358
      ),
359
      'multipage' => array(
360
        'label' => t('Multipage'),
361
        'format_types' => array('start', 'no-start'),
362
        'description' => t('This fieldgroup renders the content in a page.'),
363
        'default_formatter' => 'no-start',
364
        'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
365
      ),
366
      'accordion' => array(
367
        'label' => t('Accordion group'),
368
        'description' => t('This fieldgroup renders child groups as jQuery accordion.'),
369
        'instance_settings' => array('effect' => 'none', 'classes' => '', 'id' => ''),
370
      ),
371
      'accordion-item' => array(
372
        'label' => t('Accordion item'),
373
        'format_types' => array('open', 'closed'),
374
        'description' => t('This fieldgroup renders the content in a div, part of accordion group.'),
375
        'default_formatter' => 'closed',
376
        'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
377
      ),
378
    ),
379
    'display' => array(
380
      'html-element' => array(
381
        'label' => t('HTML element'),
382
        'description' => t('This fieldgroup renders the inner content in a HTML element with classes and attributes.'),
383
        'instance_settings' => array('element' => 'div', 'show_label' => 0, 'label_element' => 'div', 'classes' => '', 'attributes' => '', 'required_fields' => 1, 'id' => ''),
384
      ),
385
      'div' => array(
386
        'label' => t('Div'),
387
        'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
388
        'format_types' => array('open', 'collapsible', 'collapsed'),
389
        'instance_settings' => array('description' => '', 'show_label' => 1, 'label_element' => 'h3', 'effect' => 'none', 'speed' => 'fast', 'classes' => '', 'id' => ''),
390
        'default_formatter' => 'collapsible',
391
      ),
392
      'html5' => array(
393
        'label' => t('HTML5'),
394
        'description' => t('This fieldgroup renders the inner content in a semantic HTML5 wrapper'),
395
        'instance_settings' => array('wrapper' => '', 'classes' => '', 'id' => ''),
396
      ),
397
      'fieldset' => array(
398
        'label' => t('Fieldset'),
399
        'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
400
        'format_types' => array('open', 'collapsible', 'collapsed'),
401
        'instance_settings' => array('description' => '', 'classes' => '', 'id' => ''),
402
        'default_formatter' => 'collapsible',
403
      ),
404
      'tabs' => array(
405
        'label' => t('Vertical tabs group'),
406
        'description' => t('This fieldgroup renders child groups in its own vertical tabs wrapper.'),
407
        'instance_settings' => array('classes' => '', 'id' => ''),
408
      ),
409
      'tab' => array(
410
        'label' => t('Vertical tab'),
411
        'description' => t('This fieldgroup renders the content in a fieldset, part of vertical tabs group.'),
412
        'format_types' => array('open', 'closed'),
413
        'instance_settings' => array('description' => '', 'classes' => ''),
414
        'default_formatter' => 'closed',
415
      ),
416
      'htabs' => array(
417
        'label' => t('Horizontal tabs group'),
418
        'description' => t('This fieldgroup renders child groups in its own horizontal tabs wrapper.'),
419
        'instance_settings' => array('classes' => '', 'id' => ''),
420
      ),
421
      'htab' => array(
422
        'label' => t('Horizontal tab item'),
423
        'format_types' => array('open', 'closed'),
424
        'description' => t('This fieldgroup renders the content in a fieldset, part of horizontal tabs group.'),
425
        'instance_settings' => array('description' => '', 'classes' => '', 'id' => ''),
426
        'default_formatter' => 'closed',
427
      ),
428
      'accordion' => array(
429
        'label' => t('Accordion group'),
430
        'description' => t('This fieldgroup renders child groups as jQuery accordion.'),
431
        'instance_settings' => array('description' => '', 'classes' => '', 'effect' => 'bounceslide', 'id' => ''),
432
      ),
433
      'accordion-item' => array(
434
        'label' => t('Accordion item'),
435
        'format_types' => array('open', 'closed'),
436
        'description' => t('This fieldgroup renders the content in a div, part of accordion group.'),
437
        'instance_settings' => array('classes' => ''),
438
        'default_formatter' => 'closed',
439
      ),
440
    ),
441
  );
442
}
443

    
444
/**
445
 * Implements hook_field_group_format_settings().
446
 * If the group has no format settings, default ones will be added.
447
 * @params Object $group The group object.
448
 * @return Array $form The form element for the format settings.
449
 */
450
function field_group_field_group_format_settings($group) {
451
  // Add a wrapper for extra settings to use by others.
452
  $form = array(
453
    'instance_settings' => array(
454
      '#tree' => TRUE,
455
      '#weight' => 2,
456
    ),
457
  );
458

    
459
  $field_group_types = field_group_formatter_info();
460
  $mode = $group->mode == 'form' ? 'form' : 'display';
461
  $formatter = $field_group_types[$mode][$group->format_type];
462

    
463
  // Add the required formatter type selector.
464
  if (isset($formatter['format_types'])) {
465
    $form['formatter'] = array(
466
      '#title' => t('Fieldgroup settings'),
467
      '#type' => 'select',
468
      '#options' => drupal_map_assoc($formatter['format_types']),
469
      '#default_value' => isset($group->format_settings['formatter']) ? $group->format_settings['formatter'] : $formatter['default_formatter'],
470
      '#weight' => -4,
471
    );
472
  }
473

    
474
  if (isset($formatter['instance_settings']['required_fields']) && $mode == 'form') {
475
    $form['instance_settings']['required_fields'] = array(
476
      '#type' => 'checkbox',
477
      '#title' => t('Mark group as required if it contains required fields.'),
478
      '#default_value' => isset($group->format_settings['instance_settings']['required_fields']) ? $group->format_settings['instance_settings']['required_fields'] : (isset($formatter['instance_settings']['required_fields']) ? $formatter['instance_settings']['required_fields'] : ''),
479
      '#weight' => 2,
480
    );
481
  }
482

    
483
  if (isset($formatter['instance_settings']['id'])) {
484
    $form['instance_settings']['id'] = array(
485
      '#title' => t('ID'),
486
      '#type' => 'textfield',
487
      '#default_value' => isset($group->format_settings['instance_settings']['id']) ? $group->format_settings['instance_settings']['id'] : (isset($formatter['instance_settings']['id']) ? $formatter['instance_settings']['id'] : ''),
488
      '#weight' => 10,
489
      '#element_validate' => array('field_group_validate_id'),
490
    );
491
  }
492
  if (isset($formatter['instance_settings']['classes'])) {
493
    $form['instance_settings']['classes'] = array(
494
      '#title' => t('Extra CSS classes'),
495
      '#type' => 'textfield',
496
      '#default_value' => isset($group->format_settings['instance_settings']['classes']) ? $group->format_settings['instance_settings']['classes'] : (isset($formatter['instance_settings']['classes']) ? $formatter['instance_settings']['classes'] : ''),
497
      '#weight' => 11,
498
      '#element_validate' => array('field_group_validate_css_class'),
499
    );
500
  }
501

    
502
  if (isset($formatter['instance_settings']['description'])) {
503
    $form['instance_settings']['description'] = array(
504
      '#title' => t('Description'),
505
      '#type' => 'textarea',
506
      '#default_value' => isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : (isset($formatter['instance_settings']['description']) ? $formatter['instance_settings']['description'] : ''),
507
      '#weight' => 0,
508
    );
509
  }
510

    
511
  // Add optional instance_settings.
512
  switch ($group->format_type) {
513
    case 'html-element':
514
      $form['instance_settings']['element'] = array(
515
        '#title' => t('Element'),
516
        '#type' => 'textfield',
517
        '#default_value' => isset($group->format_settings['instance_settings']['element']) ? $group->format_settings['instance_settings']['element'] : $formatter['instance_settings']['element'],
518
        '#description' => t('E.g. div, section, aside etc.'),
519
        '#weight' => 1,
520
      );
521

    
522
      $form['instance_settings']['show_label'] = array(
523
        '#title' => t('Show label'),
524
        '#type' => 'select',
525
        '#options' => array(0 => t('No'), 1 => t('Yes')),
526
        '#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
527
        '#weight' => 2,
528
      );
529

    
530
      $form['instance_settings']['label_element'] = array(
531
        '#title' => t('Label element'),
532
        '#type' => 'textfield',
533
        '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
534
        '#weight' => 3,
535
      );
536

    
537
      $form['instance_settings']['attributes'] = array(
538
        '#title' => t('Attributes'),
539
        '#type' => 'textfield',
540
        '#default_value' => isset($group->format_settings['instance_settings']['attributes']) ? $group->format_settings['instance_settings']['attributes'] : $formatter['instance_settings']['attributes'],
541
        '#description' => t('E.g. name="anchor"'),
542
        '#weight' => 4,
543
      );
544
      break;
545
    case 'div':
546
      $form['label']['#description'] = t('Please enter a label for collapsible elements');
547
      $form['instance_settings']['show_label'] = array(
548
        '#title' => t('Show label'),
549
        '#type' => 'select',
550
        '#options' => array(0 => t('No'), 1 => t('Yes')),
551
        '#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
552
        '#weight' => 2,
553
      );
554
      $form['instance_settings']['label_element'] = array(
555
        '#title' => t('Label element'),
556
        '#type' => 'select',
557
        '#options' => array('h2' => t('Header 2'), 'h3' => t('Header 3')),
558
        '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
559
        '#weight' => 2,
560
      );
561
      $form['instance_settings']['effect'] = array(
562
        '#title' => t('Effect'),
563
        '#type' => 'select',
564
        '#options' => array('none' => t('None'), 'blind' => t('Blind')),
565
        '#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
566
        '#weight' => 3,
567
      );
568
      $form['instance_settings']['speed'] = array(
569
        '#title' => t('Speed'),
570
        '#type' => 'select',
571
        '#options' => array('none' => t('None'), 'slow' => t('Slow'), 'fast' => t('Fast')),
572
        '#default_value' => isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : $formatter['instance_settings']['speed'],
573
        '#weight' => 3,
574
      );
575
      break;
576
    case 'html5':
577
      $form['instance_settings']['wrapper'] = array(
578
        '#title' => t('HTML5 wrapper'),
579
        '#type' => 'select',
580
        '#options' => array('section' => t('Section'), 'article' => t('Article'), 'header' => t('Header'), 'footer' => t('Footer'), 'aside' => t('Aside')),
581
        '#default_value' => isset($group->format_settings['instance_settings']['wrapper']) ? $group->format_settings['instance_settings']['wrapper'] : 'section',
582
      );
583
      break;
584
    case 'fieldset':
585
      $form['label']['#description'] = t('Please enter a label for collapsible elements');
586
      break;
587
    case 'multipage-group':
588
      $form['instance_settings']['page_header'] = array(
589
        '#title' => t('Format page title'),
590
        '#type' => 'select',
591
        '#options' => array(0 => t('None'), 1 => t('Label only'), 2 => t('Step 1 of 10'), 3 => t('Step 1 of 10 [Label]'),),
592
        '#default_value' => isset($group->format_settings['instance_settings']['page_header']) ? $group->format_settings['instance_settings']['page_header'] : $formatter['instance_settings']['page_header'],
593
        '#weight' => 1,
594
      );
595
      $form['instance_settings']['page_counter'] = array(
596
        '#title' => t('Add a page counter at the bottom'),
597
        '#type' => 'select',
598
        '#options' => array(0 => t('No'), 1 => t('Format 1 / 10'), 2 => t('The count number only')),
599
        '#default_value' => isset($group->format_settings['instance_settings']['page_counter']) ? $group->format_settings['instance_settings']['page_counter'] : $formatter['instance_settings']['page_counter'],
600
        '#weight' => 2,
601
      );
602
      $form['instance_settings']['move_button'] = array(
603
        '#title' => t('Move submit button to last multipage'),
604
        '#type' => 'select',
605
        '#options' => array(0 => t('No'), 1 => t('Yes')),
606
        '#default_value' => isset($group->format_settings['instance_settings']['move_button']) ? $group->format_settings['instance_settings']['move_button'] : $formatter['instance_settings']['move_button'],
607
        '#weight' => 3,
608
      );
609
      $form['instance_settings']['move_additional'] = array(
610
        '#title' => t('Move additional settings to last multipage (if available)'),
611
        '#type' => 'select',
612
        '#options' => array(0 => t('No'), 1 => t('Yes')),
613
        '#default_value' => isset($group->format_settings['instance_settings']['move_additional']) ? $group->format_settings['instance_settings']['move_additional'] : $formatter['instance_settings']['move_additional'],
614
        '#weight' => 4,
615
      );
616
    case 'tabs':
617
    case 'htabs':
618
      break;
619
    case 'accordion':
620
      $form['instance_settings']['effect'] = array(
621
        '#title' => t('Effect'),
622
        '#type' => 'select',
623
        '#options' => array('none' => t('None'), 'bounceslide' => t('Bounce slide')),
624
        '#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
625
        '#weight' => 2,
626
      );
627
      break;
628
    case 'multipage':
629
      break;
630
    case 'tab':
631
    case 'htab':
632
    case 'accordion-item':
633
    default:
634
  }
635

    
636
  return $form;
637
}
638

    
639
/**
640
 * Helper function to prepare basic variables needed for most formatters.
641
 *
642
 * Called in field_group_field_group_pre_render(), but can also be called in
643
 * other implementations of hook_field_group_pre_render().
644
 */
645
function field_group_pre_render_prepare(&$group) {
646

    
647
  $classes = _field_group_get_html_classes($group);
648

    
649
  $group->classes = implode(' ', $classes->required);
650
  $group->description = !empty($group->format_settings['instance_settings']['description']) ? filter_xss_admin(t($group->format_settings['instance_settings']['description'])) : '';
651

    
652
}
653

    
654
/**
655
 * Implements hook_field_group_pre_render().
656
 *
657
 * @param Array $elements by address.
658
 * @param Object $group The Field group info.
659
 */
660
function field_group_field_group_pre_render(&$element, &$group, & $form) {
661

    
662
  field_group_pre_render_prepare($group);
663

    
664
  $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
665

    
666
  // Add all field_group format types to the js settings.
667
  $form['#attached']['js'][] = array(
668
    'data' => array('field_group' => array($group->format_type => $view_mode)),
669
    'type' => 'setting',
670
  );
671

    
672
  if (isset($group->format_settings['instance_settings']['id']) && !empty($group->format_settings['instance_settings']['id'])) {
673
    $element['#id'] = drupal_html_id($group->format_settings['instance_settings']['id']);
674
  }
675

    
676
  $element['#weight'] = $group->weight;
677

    
678
  // Call the pre render function for the format type.
679
  $function = "field_group_pre_render_" . str_replace("-", "_", $group->format_type);
680
  if (function_exists($function)) {
681
    $function($element, $group, $form);
682
  }
683

    
684
}
685

    
686
/**
687
 * Implements field_group_pre_render_<format-type>.
688
 * Format type: Fieldset.
689
 *
690
 * @param $element The field group form element.
691
 * @param $group The Field group object prepared for pre_render.
692
 * @param $form The root element or form.
693
 */
694
function field_group_pre_render_fieldset(&$element, $group, &$form) {
695

    
696
  $element += array(
697
    '#type' => 'fieldset',
698
    '#title' => check_plain(t($group->label)),
699
    '#collapsible' => $group->collapsible,
700
    '#collapsed' => $group->collapsed,
701
    '#pre_render' => array(),
702
    '#attributes' => array('class' => explode(' ', $group->classes)),
703
    '#description' => $group->description,
704
  );
705

    
706
  if ($group->collapsible || $group->collapsed) {
707
    $element['#attached']['library'][] = array('system', 'drupal.collapse');
708
  }
709
}
710

    
711
/**
712
 * Implements field_group_pre_render_<format-type>.
713
 * Format type: HTML element.
714
 *
715
 * @param $element The field group form element.
716
 * @param $group The Field group object prepared for pre_render.
717
 * @param $form The root element or form.
718
 */
719
function field_group_pre_render_html_element(&$element, $group, &$form) {
720
  $html_element = isset($group->format_settings['instance_settings']['element']) ? $group->format_settings['instance_settings']['element'] : 'div';
721
  $show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0;
722
  $label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'div';
723
  $configured_attributes = isset($group->format_settings['instance_settings']['attributes']) ? ' ' . $group->format_settings['instance_settings']['attributes'] : '';
724
  $group->classes = trim($group->classes);
725

    
726
  // This regex split the attributes string so that we can pass that
727
  // later to drupal_attributes().
728
  preg_match_all('/([^\s=]+)="([^"]+)"/', $configured_attributes, $matches);
729

    
730
  $element_attributes = array();
731
  // Put the attribute and the value together.
732
  foreach ($matches[1] as $key => $attribute) {
733
    $element_attributes[$attribute] = $matches[2][$key];
734
  }
735

    
736
  // Add the classes to the attributes array.
737
  if (!isset($element_attributes['class']) && $group->classes) {
738
    $element_attributes['class'] = $group->classes;
739
  }
740
  elseif (isset($element_attributes['class']) && $group->classes) {
741
    $element_attributes['class'] .= ' ' . $group->classes;
742
  }
743

    
744
  if (isset($element['#id'])) {
745
    $element_attributes['id'] = $element['#id'];
746
  }
747

    
748
  // Sanitize the attributes.
749
  $element_attributes = _filter_xss_attributes(drupal_attributes($element_attributes));
750
  $attributes = $element_attributes ? ' ' . implode(' ', $element_attributes) : '';
751

    
752
  $element['#prefix'] = '<' . $html_element . $attributes . '>';
753
  if ($show_label) {
754
    $element['#prefix'] .= '<' . $label_element . '><span>' . check_plain(t($group->label)) . '</span></' . $label_element . '>';
755
  }
756
  $element['#suffix'] = '</' . $html_element . '>';
757
}
758

    
759
/**
760
 * Implements field_group_pre_render_<format-type>.
761
 * Format type: Div.
762
 *
763
 * @param $element The field group form element.
764
 * @param $group The Field group object prepared for pre_render.
765
 * @param $form The root element or form.
766
 */
767
function field_group_pre_render_div(&$element, $group, &$form) {
768

    
769
  $show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0;
770
  $label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'h2';
771
  $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
772

    
773
  $element['#type'] = 'markup';
774
  $id = isset($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
775

    
776
  if ($group->format_settings['formatter'] != 'open') {
777

    
778
    $element['#prefix'] = '<div' . $id . ' class="' . $group->classes . '">
779
      <' . $label_element . '><span class="field-group-format-toggler">' . check_plain(t($group->label)) . '</span></' . $label_element . '>
780
      <div class="field-group-format-wrapper" style="display: ' . (!empty($group->collapsed) ? 'none' : 'block') . ';">';
781
    $element['#suffix'] = '</div></div>';
782
  }
783
  else {
784
    $class_attribute = !empty($group->classes) ? ' class="' . $group->classes . '"' : '';
785

    
786
    $element['#prefix'] = '<div' . $id . $class_attribute . '>';
787
    if ($show_label) {
788
      $element['#prefix'] .= '<' . $label_element . '><span>' . check_plain(t($group->label)) . '</span></' . $label_element . '>';
789
    }
790
    $element['#suffix'] = '</div>';
791
  }
792
  if (!empty($group->description)) {
793
    $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
794
  }
795

    
796
  if ($effect == 'blind') {
797
    $element['#attached']['library'][] = array('system', 'effects.blind');
798
  }
799

    
800
}
801

    
802
/**
803
 * Implements field_group_pre_render_<format-type>.
804
 * Format type: HTML5.
805
 *
806
 * @param $element The field group form element.
807
 * @param $group The Field group object prepared for pre_render.
808
 * @param $form The root element or form.
809
 */
810
function field_group_pre_render_html5(&$element, $group, &$form) {
811
  $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
812
  $class = !empty($group->classes) ? ' class="' . $group->classes . '"' : '';
813
  $element += array(
814
    '#type' => 'markup',
815
    '#prefix' => '<' . $group->format_settings['instance_settings']['wrapper'] . $id . $class . '>',
816
    '#suffix' => '</' . $group->format_settings['instance_settings']['wrapper'] . '>',
817
  );
818
}
819

    
820
/**
821
 * Implements field_group_pre_render_<format-type>.
822
 * Format type: Accordion.
823
 *
824
 * @param $element The field group form element.
825
 * @param $group The Field group object prepared for pre_render.
826
 * @param $form The root element or form.
827
 */
828
function field_group_pre_render_accordion(&$element, $group, &$form) {
829

    
830
  // Add the jQuery UI accordion.
831
  $element['#attached']['library'][] = array('system', 'ui.accordion');
832

    
833
  $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
834

    
835
  $element += array(
836
    '#type' => 'markup',
837
    '#prefix' => '<div class="' . $group->classes . '"' . $id .'>',
838
    '#suffix' => '</div>',
839
  );
840
}
841

    
842
/**
843
 * Implements field_group_pre_render_<format-type>.
844
 * Format type: Accordion item.
845
 *
846
 * @param $element The field group form element.
847
 * @param $group The Field group object prepared for pre_render.
848
 * @param $form The root element or form.
849
 */
850
function field_group_pre_render_accordion_item(&$element, $group, &$form) {
851

    
852
  $element += array(
853
    '#type' => 'markup',
854
    '#prefix' => '<h3 class="field-group-format-toggler ' . $group->format_type . ($group->collapsed ? '' : ' field-group-accordion-active') . '"><a href="#">' . check_plain(t($group->label)) . '</a></h3>
855
    <div class="field-group-format-wrapper ' . $group->classes . '">',
856
    '#suffix' => '</div>',
857
    //'#attributes' => array('class' => array($group->format_type)),
858
  );
859
  if (!empty($group->description)) {
860
    $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
861
  }
862

    
863
}
864

    
865
/**
866
 * Implements field_group_pre_render_<format-type>.
867
 * Format type: Horizontal tabs group.
868
 *
869
 * @param $element The field group form element.
870
 * @param $group The Field group object prepared for pre_render.
871
 * @param $form The root element or form.
872
 */
873
function field_group_pre_render_htabs(&$element, $group, &$form) {
874

    
875
  $classes = 'field-group-' . $group->format_type . '-wrapper';
876
  if (!empty($group->classes)) {
877
    $classes .= ' ' . $group->classes;
878
  }
879

    
880
  $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
881

    
882
  $element += array(
883
    '#type' => 'horizontal_tabs',
884
    '#title' => check_plain(t($group->label)),
885
    '#theme_wrappers' => array('horizontal_tabs'),
886
    '#prefix' => '<div class="' . $classes . '"' . $id . '>',
887
    '#suffix' => '</div>',
888
  );
889

    
890
  // By default vertical_tabs don't have titles but you can override it in the theme.
891
  if (!empty($group->label)) {
892
    $element['#title'] = check_plain($group->label);
893
  }
894

    
895
  // Only add form.js on forms.
896
  if (!empty($form['#type']) && $form['#type'] == 'form') {
897
    $element['#attached']['js'][] = 'misc/form.js';
898
  }
899

    
900
  $element['#attached']['library'][] = array('field_group', 'horizontal-tabs');
901
}
902

    
903
/**
904
 * Implements field_group_pre_render_<format-type>.
905
 * Format type: Horizontal tab.
906
 *
907
 * @param $element The field group form element.
908
 * @param $group The Field group object prepared for pre_render.
909
 * @param $form The root element or form.
910
 */
911
function field_group_pre_render_htab(&$element, $group, &$form) {
912

    
913
  $element += array(
914
    '#type' => 'fieldset',
915
    '#title' => check_plain(t($group->label)),
916
    '#collapsible' => $group->collapsible,
917
    '#collapsed' => $group->collapsed,
918
    '#attributes' => array('class' => explode(" ", $group->classes)),
919
    '#group' => $group->parent_name,
920
    // very important. Cannot be added on the form!
921
    '#parents' => array($group->parent_name),
922
    '#description' => $group->description,
923
  );
924

    
925
}
926

    
927
/**
928
 * Implements field_group_pre_render_<format-type>.
929
 * Format type: Multipage group.
930
 *
931
 * @param $element The field group form element.
932
 * @param $group The Field group object prepared for pre_render.
933
 * @param $form The root element or form.
934
 */
935
function field_group_pre_render_multipage_group(&$element, &$group, &$form) {
936

    
937
  $multipage_element = array(
938
    '#type' => 'multipage',
939
    '#theme_wrappers' => array('multipage'),
940
    '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper ' . $group->classes . '">',
941
    '#suffix' => '</div>',
942
  );
943

    
944
  $element += $multipage_element;
945

    
946
  $move_additional = isset($group->format_settings['instance_settings']['move_additional']) ? ($group->format_settings['instance_settings']['move_additional'] && isset($form['additional_settings'])) : isset($form['additional_settings']);
947
  $move_button = isset($group->format_settings['instance_settings']['move_button']) ? $group->format_settings['instance_settings']['move_button'] : 0;
948

    
949
  drupal_add_js(array(
950
    'field_group' => array(
951
      'multipage_move_submit' => (bool) $move_button,
952
      'multipage_move_additional' => (bool) $move_additional
953
    )
954
  ), 'setting');
955

    
956
}
957

    
958
/**
959
 * Implements field_group_pre_render_<format-type>.
960
 * Format type: Multipage.
961
 *
962
 * @param $element The field group form element.
963
 * @param $group The Field group object prepared for pre_render.
964
 * @param $form The root element or form.
965
 */
966
function field_group_pre_render_multipage(&$element, $group, &$form) {
967

    
968
  $group->classes .= $group->format_settings['formatter'] == 'start' ? ' multipage-open' : ' multipage-closed';
969
  $element += array(
970
    '#type' => 'multipage_pane',
971
    '#title' => check_plain(t($group->label)),
972
    '#collapsible' => $group->collapsible,
973
    '#collapsed' => $group->collapsed,
974
    '#attributes' => array('class' => explode(" ", $group->classes)),
975
    '#group' => $group->parent_name,
976
    '#group_object' => $group,
977
    '#parent_group_object' => $form['#groups'][$group->parent_name],
978
    // very important. Cannot be added on the form!
979
    '#parents' => array($group->parent_name),
980
    '#description' => $group->description,
981
  );
982

    
983
  $element['#attached']['library'][] = array('field_group', 'multipage');
984
}
985

    
986
/**
987
 * Implements field_group_pre_render_<format-type>.
988
 * Format type: Vertical tabs wrapper.
989
 *
990
 * @param $element The field group form element.
991
 * @param $group The Field group object prepared for pre_render.
992
 * @param $form The root element or form.
993
 */
994
function field_group_pre_render_tabs(&$element, $group, &$form) {
995

    
996
  $classes = 'field-group-' . $group->format_type . '-wrapper';
997
  if (!empty($group->classes)) {
998
    $classes .= ' ' . $group->classes;
999
  }
1000

    
1001
  $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
1002

    
1003
  $element += array(
1004
    '#type' => 'vertical_tabs',
1005
    '#theme_wrappers' => array('vertical_tabs'),
1006
    '#prefix' => '<div class="' . $classes . '"' . $id . '>',
1007
    '#suffix' => '</div>',
1008
  );
1009

    
1010
  // By default vertical_tabs don't have titles but you can override it in the theme.
1011
  if (!empty($group->label)) {
1012
    $element['#title'] = check_plain($group->label);
1013
  }
1014

    
1015
  $element[$group->group_name . '__active_tab'] = array(
1016
    '#type' => 'hidden',
1017
    '#default_value' => '',
1018
    '#attributes' => array('class' => array('vertical-tabs-active-tab')),
1019
  );
1020

    
1021
  $element['#attached']['library'][] = array('system', 'drupal.collapse');
1022
}
1023

    
1024
/**
1025
 * Implements field_group_pre_render_<format-type>.
1026
 * Format type: Vertical tab.
1027
 *
1028
 * @param $element The field group form element.
1029
 * @param $group The Field group object prepared for pre_render.
1030
 * @param $form The root element or form.
1031
 */
1032
function field_group_pre_render_tab(&$element, $group, &$form) {
1033

    
1034
  $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
1035

    
1036
  // Could be it never runs through htab.
1037
  $form['#attached']['js'][] = array(
1038
    'data' => array('field_group' => array('tabs' => $view_mode)),
1039
    'type' => 'setting',
1040
  );
1041

    
1042
  $add = array(
1043
    '#type' => 'fieldset',
1044
    '#id' => 'edit-' . $group->group_name,
1045
    '#title' => check_plain(t($group->label)),
1046
    '#collapsible' => $group->collapsible,
1047
    '#collapsed' => $group->collapsed,
1048
    '#attributes' => array('class' => explode(" ", $group->classes)),
1049
    '#description' => $group->description,
1050
  );
1051

    
1052
  // Front-end and back-end on configuration will lead
1053
  // to vertical tabs nested in a separate vertical group.
1054
  if ($view_mode != 'form') {
1055
    $add['#group'] = empty($group->parent_name) ? 'additional_settings' : $group->parent_name;
1056
    $add['#parents'] = array($add['#group']);
1057
    $element += $add;
1058
  }
1059
  // Form fieldgroups which are nested into a vertical tab group
1060
  // are handled a little different.
1061
  elseif (!empty($group->parent_name)) {
1062
    $add['#group'] = $group->parent_name;
1063
    $element += $add;
1064
  }
1065
  // Forms "can" have additional settins. We'll try to locate it first, if not
1066
  // exists, field_group will create a parallel additional settings entry.
1067
  else {
1068
    // Create the fieldgroup element.
1069
    $add['#parents'] = array('additional_settings');
1070
    $add['#group'] = 'additional_settings';
1071
    $add['#weight'] = -30 + $group->weight; // hardcoded to bring our extra additional vtabs on top.
1072

    
1073
    // Check if the additional_settings exist for this type of form.
1074
    if (isset($form['additional_settings']['group']['#groups']['additional_settings'])) {
1075

    
1076
      // Merge fieldgroups with the core additional settings.
1077
      $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name] = $add;
1078
      $form['additional_settings']['group']['#groups'][$group->group_name] = array('#group_exists' => TRUE);
1079
      // Nest the fields inside the appropriate structure.
1080
      foreach (element_children($element) as $fieldname) {
1081
        $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
1082
        unset($element[$fieldname]);
1083
      }
1084
    }
1085
    // Assumption the wrapper is in the root. This could be done by field_group itself
1086
    // in previous loop of tabs in same wrapper or even some other contrib / custom module.
1087
    else {
1088
      if (!isset($form['additional_settings']['#type'])) {
1089
        $form['additional_settings'] = array(
1090
          '#type' => 'vertical_tabs',
1091
          '#weight' => $group->weight,
1092
          '#theme_wrappers' => array('vertical_tabs'),
1093
          '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper">',
1094
          '#suffix' => '</div>',
1095
        );
1096
        $form['#attached']['library'][] = array('system', 'drupal.collapse');
1097
      }
1098
      $form['additional_settings'][$group->group_name] = $add;
1099
      // Nest the fields inside the appropriate structure.
1100
      foreach (element_children($element) as $fieldname) {
1101
        $form['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
1102
        unset($element[$fieldname]);
1103
      }
1104
    }
1105
  }
1106

    
1107
}
1108

    
1109
/**
1110
 * Implements hook_field_group_build_pre_render_alter().
1111
 * @param Array $elements by address.
1112
 */
1113
function field_group_field_group_build_pre_render_alter(& $element) {
1114

    
1115
  // Someone is doing a node view, in a node view. Reset content.
1116
  // TODO Check if this breaks something else.
1117
  if (isset($element['#node']->content) && count($element['#node']->content) > 0) {
1118
    $element['#node']->content = array();
1119
  }
1120

    
1121
  $display = isset($element['#view_mode']);
1122
  $groups = array_keys($element['#groups']);
1123

    
1124
  // Dish the fieldgroups with no fields for non-forms.
1125
  if ($display) {
1126
    field_group_remove_empty_display_groups($element, $groups);
1127
  }
1128
  else {
1129
    // Fix the problem on forms with additional settings.
1130
    field_group_remove_empty_form_groups('form', $element, $groups, $element['#groups'], $element['#entity_type']);
1131
  }
1132

    
1133
  // Add the default field_group javascript and stylesheet.
1134
  $element['#attached']['js'][] = drupal_get_path('module', 'field_group') . '/field_group.js';
1135

    
1136
  // Move additional settings to the last multipage pane if configured that way.
1137
  // Note that multipages MUST be in the root of the form.
1138
  foreach (element_children($element) as $name) {
1139
    if (isset($element[$name]['#type']) && $element[$name]['#type'] == 'multipage' && isset($element['additional_settings'])) {
1140
      $parent_group = $element['#groups'][$name];
1141
      $move_additional = isset($parent_group->format_settings['instance_settings']['move_additional']) ? $parent_group->format_settings['instance_settings']['move_additional'] : 1;
1142
      $last_pane = NULL;
1143
      foreach (element_children($element[$name], TRUE) as $pane) {
1144
        $last_pane = $pane;
1145
      }
1146
      $element[$name][$last_pane]['additional_settings'] = $element['additional_settings'];
1147
      unset($element['additional_settings']);
1148
    }
1149
  }
1150

    
1151
}
1152

    
1153
/**
1154
 * Remove empty groups on forms.
1155
 *
1156
 * @param String $parent_name
1157
 *   The name of the element.
1158
 * @param array $element
1159
 *   The element to check the empty state.
1160
 * @param array $groups
1161
 *   Array of group objects.
1162
 */
1163
function field_group_remove_empty_form_groups($name, & $element, $groups, &$form_groups, $entity) {
1164

    
1165
  $children = element_children($element);
1166

    
1167
  $hasChildren = FALSE;
1168
  if (count($children)) {
1169
    foreach ($children as $childname) {
1170

    
1171
      if (in_array($childname, $groups, TRUE)) {
1172
        field_group_remove_empty_form_groups($childname, $element[$childname], $groups, $form_groups, $entity);
1173
      }
1174
      $hasChildren = $hasChildren ? TRUE : _field_group_is_empty_element($element, $entity, $childname, $groups);
1175

    
1176
    }
1177
  }
1178

    
1179
  if (!$hasChildren) {
1180

    
1181
    // Remove empty elements from the #groups.
1182
    if (empty($element) && isset($form_groups[$name]) && !is_array($form_groups[$name])) {
1183
      foreach ($form_groups as $group_name => $group) {
1184
        if (isset($group->children)) {
1185
          $group_children = array_flip($group->children);
1186
          if (isset($group_children[$name])) {
1187
            unset($form_groups[$group_name]->children[$group_children[$name]]);
1188
          }
1189
        }
1190
      }
1191
    }
1192

    
1193
    $element['#access'] = FALSE;
1194

    
1195
  }
1196

    
1197
}
1198

    
1199
/**
1200
 * Determine if an element has non-empty children.
1201
 */
1202
function _field_group_is_empty_element($element, $entity, $childname, $groups) {
1203

    
1204
  $exceptions = array('user__account', 'comment__author');
1205
  $exception = $entity . '__' . $childname;
1206

    
1207
  if (in_array($exception, $exceptions)) {
1208
    return TRUE;
1209
  }
1210

    
1211
  if (isset($element[$childname]['#type'])
1212
    || isset($element[$childname]['#markup'])
1213
    || isset($element[$childname]['#prefix'])
1214
    || isset($element[$childname]['#suffix'])
1215
  ) {
1216
    return TRUE;
1217
  }
1218

    
1219
  // Prevent a double recursive loop (groups are already recursive looped in field_group_remove_empty_form_groups.
1220
  if (in_array($childname, $groups)) {
1221
    return FALSE;
1222
  }
1223

    
1224
  $children = element_children($element[$childname]);
1225

    
1226
  foreach ($children as $child) {
1227
    if (_field_group_is_empty_element($element[$childname], $entity, $child, $groups)) {
1228
      return TRUE;
1229
    }
1230
  }
1231

    
1232
  return FALSE;
1233

    
1234
}
1235

    
1236
/**
1237
 * Remove empty groups on entity display.
1238
 * @param array $element
1239
 *   The element to check the empty state.
1240
 * @param array $groups
1241
 *   Array of group objects.
1242
 */
1243
function field_group_remove_empty_display_groups(& $element, $groups) {
1244

    
1245
  $empty_child = TRUE;
1246
  $empty_group = TRUE;
1247

    
1248
  // Loop through the children for current element.
1249
  foreach (element_children($element) as $name) {
1250

    
1251
    // Descend if the child is a group.
1252
    if (in_array($name, $groups)) {
1253
      $empty_child = field_group_remove_empty_display_groups($element[$name], $groups);
1254
      if (!$empty_child) {
1255
        $empty_group = FALSE;
1256
      }
1257
    }
1258
    // Child is a field, the element is not empty and access is set to true (or empty).
1259
    elseif (!empty($element[$name]) && (!isset($element[$name]['#access']) || $element[$name]['#access'])) {
1260
      $empty_group = FALSE;
1261
    }
1262

    
1263
  }
1264

    
1265
  // Reset an empty group.
1266
  if ($empty_group) {
1267
    $element = NULL;
1268
  }
1269

    
1270
  return $empty_group;
1271

    
1272
}
1273

    
1274
/**
1275
 * Implements hook_field_group_format_summary().
1276
 */
1277
function field_group_field_group_format_summary($group) {
1278

    
1279
  $group_form = module_invoke_all('field_group_format_settings', $group);
1280

    
1281
  $output = '';
1282
  if (isset($group->format_settings['formatter'])) {
1283
    $output .= '<strong>' . $group->format_type . '</strong> ' . $group->format_settings['formatter'] . '';
1284
  }
1285
  if (isset($group->format_settings['instance_settings'])) {
1286
    $last = end($group->format_settings['instance_settings']);
1287
    $output .= '<br />';
1288
    foreach ($group->format_settings['instance_settings'] as $key => $value) {
1289
      if (empty($value)) {
1290
        continue;
1291
      }
1292

    
1293
      $output .= '<strong>' . $key . '</strong> ';
1294

    
1295
      if (isset($group_form['instance_settings'], $group_form['instance_settings'][$key]['#options'])) {
1296
        if (is_array($value)) {
1297
          $value = implode(array_filter($value), ', ');
1298
        }
1299
        else {
1300
          $value = $group_form['instance_settings'][$key]['#options'][$value];
1301
        }
1302
      }
1303

    
1304
      // Shorten the string.
1305
      if (drupal_strlen($value) > 38) {
1306
        $value = truncate_utf8($value, 50, TRUE, TRUE);
1307
      }
1308
      // If still numeric, handle it as yes or no.
1309
      elseif (is_numeric($value)) {
1310
        $value = $value == '1' ? t('yes') : t('no');
1311
      }
1312
      $output .= check_plain($value);
1313
      $output .= $last == $value ? ' ' : '<br />';
1314
    }
1315
  }
1316
  return $output;
1317
}
1318

    
1319
/**
1320
 * Implements hook_element_info().
1321
 */
1322
function field_group_element_info() {
1323
  $types['horizontal_tabs'] = array(
1324
    '#theme_wrappers' => array('horizontal_tabs'),
1325
    '#default_tab' => '',
1326
    '#process' => array('form_process_horizontal_tabs'),
1327
  );
1328
  $types['multipage'] = array(
1329
    '#theme_wrappers' => array('multipage'),
1330
    '#default_tab' => '',
1331
    '#process' => array('form_process_multipage'),
1332
  );
1333
  $types['multipage_pane'] = array(
1334
    '#value' => NULL,
1335
    '#process' => array('form_process_fieldset', 'ajax_process_form'),
1336
    '#pre_render' => array('form_pre_render_fieldset'),
1337
    '#theme_wrappers' => array('multipage_pane'),
1338
  );
1339
  return $types;
1340
}
1341

    
1342
/**
1343
 * Implements hook_library().
1344
 */
1345
function field_group_library() {
1346

    
1347
  $path = drupal_get_path('module', 'field_group');
1348
  // Horizontal Tabs.
1349
  $libraries['horizontal-tabs'] = array(
1350
    'title' => 'Horizontal Tabs',
1351
    'website' => 'http://drupal.org/node/323112',
1352
    'version' => '1.0',
1353
    'js' => array(
1354
      $path . '/horizontal-tabs/horizontal-tabs.js' => array(),
1355
    ),
1356
    'css' => array(
1357
      $path . '/horizontal-tabs/horizontal-tabs.css' => array(),
1358
    ),
1359
  );
1360
  // Multipage Tabs.
1361
  $libraries['multipage'] = array(
1362
    'title' => 'Multipage',
1363
    'website' => 'http://drupal.org/node/323112',
1364
    'version' => '1.0',
1365
    'js' => array(
1366
      $path . '/multipage/multipage.js' => array(),
1367
    ),
1368
    'css' => array(
1369
      $path . '/multipage/multipage.css' => array(),
1370
    ),
1371
  );
1372

    
1373
  return $libraries;
1374
}
1375

    
1376
/**
1377
 * Implements hook_field_extra_fields().
1378
 */
1379
function field_group_field_extra_fields() {
1380
  $extra = array();
1381

    
1382
  $extra['user']['user'] = array('form' => array());
1383

    
1384
  // User picture field to integrate with user module.
1385
  if (variable_get('user_pictures', 0)) {
1386
    $extra['user']['user']['form']['picture'] = array(
1387
      'label' => t('Picture'),
1388
      'description' => t('User picture'),
1389
      'weight' => 5,
1390
    );
1391
  }
1392

    
1393
  // Field to itegrate with overlay module.
1394
  if (module_exists('overlay')) {
1395
    $extra['user']['user']['form']['overlay_control'] = array(
1396
      'label' => t('Administrative overlay'),
1397
      'description' => t('Administrative overlay'),
1398
      'weight' => 5,
1399
    );
1400
  }
1401

    
1402
  // Field to itegrate with contact module.
1403
  if (module_exists('contact')) {
1404
    $extra['user']['user']['form']['contact'] = array(
1405
      'label' => t('Contact'),
1406
      'description' => t('Contact user element'),
1407
     'weight' => 5,
1408
    );
1409
  }
1410

    
1411
  // Field to integrate with the locale module.
1412
  if (module_exists('locale')) {
1413
    $extra['user']['user']['form']['locale'] = array(
1414
      'label' => t('Language settings'),
1415
      'description' => t('Language settings for the user account.'),
1416
      'weight' => 5,
1417
    );
1418
  }
1419

    
1420
  // Field to integrate with the wysiwyg module on user settings.
1421
  if (module_exists('wysiwyg')) {
1422
    $extra['user']['user']['form']['wysiwyg'] = array(
1423
      'label' => t('Wysiwyg status'),
1424
      'description' => t('Text formats enabled for rich-text editing'),
1425
      'weight' => 5,
1426
    );
1427
  }
1428

    
1429
  return $extra;
1430
}
1431

    
1432
/**
1433
 * Implements hook_field_attach_rename_bundle().
1434
 */
1435
function field_group_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
1436
  db_query('UPDATE {field_group} SET bundle = :bundle WHERE bundle = :old_bundle AND entity_type = :entity_type', array(
1437
    ':bundle' => $bundle_new,
1438
    ':old_bundle' => $bundle_old,
1439
    ':entity_type' => $entity_type,
1440
  ));
1441
}
1442

    
1443
/**
1444
 * Creates a group formatted as horizontal tabs.
1445
 * This function will never be callable from within field_group rendering. Other
1446
 * modules using #type horizontal_tabs will have the benefit of this processor.
1447
 *
1448
 * @param $element
1449
 *   An associative array containing the properties and children of the
1450
 *   fieldset.
1451
 * @param $form_state
1452
 *   The $form_state array for the form this horizontal tab widget belongs to.
1453
 * @return
1454
 *   The processed element.
1455
 */
1456
function form_process_horizontal_tabs($element, &$form_state) {
1457
  // Inject a new fieldset as child, so that form_process_fieldset() processes
1458
  // this fieldset like any other fieldset.
1459
  $element['group'] = array(
1460
    '#type' => 'fieldset',
1461
    '#theme_wrappers' => array(),
1462
    '#parents' => $element['#parents'],
1463
  );
1464

    
1465
  // The JavaScript stores the currently selected tab in this hidden
1466
  // field so that the active tab can be restored the next time the
1467
  // form is rendered, e.g. on preview pages or when form validation
1468
  // fails.
1469
  $name = implode('__', $element['#parents']);
1470
  if (isset($form_state['values'][$name . '__active_tab'])) {
1471
    $element['#default_tab'] = $form_state['values'][$name . '__active_tab'];
1472
  }
1473
  $element[$name . '__active_tab'] = array(
1474
    '#type' => 'hidden',
1475
    '#default_value' => $element['#default_tab'],
1476
    '#attributes' => array('class' => array('horizontal-tabs-active-tab')),
1477
  );
1478

    
1479
  return $element;
1480
}
1481

    
1482
/**
1483
 * Returns HTML for an element's children fieldsets as horizontal tabs.
1484
 *
1485
 * @param $variables
1486
 *   An associative array containing:
1487
 *   - element: An associative array containing the properties and children of the
1488
 *     fieldset. Properties used: #children.
1489
 *
1490
 * @ingroup themeable
1491
 */
1492
function theme_horizontal_tabs($variables) {
1493
  $element = $variables['element'];
1494
  // Add required JavaScript and Stylesheet.
1495
  drupal_add_library('field_group', 'horizontal-tabs');
1496

    
1497
  $output = '<h2 class="element-invisible">' . (!empty($element['#title']) ? $element['#title'] : t('Horizontal Tabs')) . '</h2>';
1498
  $output .= '<div class="horizontal-tabs-panes">' . $element['#children'] . '</div>';
1499

    
1500
  return $output;
1501
}
1502

    
1503
/**
1504
 * Creates a group formatted as multipage.
1505
 * This function will never be callable from within field_group rendering. Other
1506
 * modules using #type multipage will have the benefit of this processor.
1507
 *
1508
 * @param $element
1509
 *   An associative array containing the properties and children of the
1510
 *   fieldset.
1511
 * @param $form_state
1512
 *   The $form_state array for the form this multipage tab widget belongs to.
1513
 * @return
1514
 *   The processed element.
1515
 */
1516
function form_process_multipage($element, &$form_state) {
1517
  // Inject a new fieldset as child, so that form_process_fieldset() processes
1518
  // this fieldset like any other fieldset.
1519
  $element['group'] = array(
1520
    '#type' => 'fieldset',
1521
    '#theme_wrappers' => array(),
1522
    '#parents' => $element['#parents'],
1523
  );
1524

    
1525
  // The JavaScript stores the currently selected tab in this hidden
1526
  // field so that the active control can be restored the next time the
1527
  // form is rendered, e.g. on preview pages or when form validation
1528
  // fails.
1529
  $name = implode('__', $element['#parents']);
1530
  if (isset($form_state['values'][$name . '__active_control'])) {
1531
    $element['#default_tab'] = $form_state['values'][$name . '__active_control'];
1532
  }
1533
  $element[$name . '__active_control'] = array(
1534
    '#type' => 'hidden',
1535
    '#default_value' => $element['#default_control'],
1536
    '#attributes' => array('class' => array('multipage-active-control')),
1537
  );
1538

    
1539
  return $element;
1540
}
1541

    
1542
/**
1543
 * Returns HTML for an element's children fieldsets as multipage.
1544
 *
1545
 * @param $variables
1546
 *   An associative array containing:
1547
 *   - element: An associative array containing the properties and children of the
1548
 *     fieldset. Properties used: #children.
1549
 *
1550
 * @ingroup themeable
1551
 */
1552
function theme_multipage($variables) {
1553
  $element = $variables['element'];
1554
  // Add required JavaScript and Stylesheet.
1555
  $element['#attached']['library'][] = array('field_group', 'multipage');
1556

    
1557
  $output = '<h2 class="element-invisible">' . (!empty($element['#title']) ? $element['#title'] : t('Multipage')) . '</h2>';
1558

    
1559
  $output .= '<div class="multipage-panes">';
1560
  $output .= $element['#children'];
1561
  $output .= '</div>';
1562

    
1563
  return $output;
1564
}
1565

    
1566
/**
1567
 * Returns HTML for multipage pane.
1568
 *
1569
 * @param $variables
1570
 *   An associative array containing:
1571
 *   - element: An associative array containing the properties and children of the
1572
 *     fieldset. Properties used: #children.
1573
 *
1574
 * @ingroup themeable
1575
 */
1576
function theme_multipage_pane($variables) {
1577

    
1578
  $element = $variables['element'];
1579
  $group = $variables['element']['#group_object'];
1580
  $parent_group = $variables['element']['#parent_group_object'];
1581

    
1582
  static $multipages;
1583
  if (!isset($multipages[$group->parent_name])) {
1584
    $multipages = array($group->parent_name => 0);
1585
  }
1586
  $multipages[$parent_group->group_name]++;
1587

    
1588
  // Create a page title from the label.
1589
  $page_header = isset($parent_group->format_settings['instance_settings']['page_header']) ? $parent_group->format_settings['instance_settings']['page_header'] : 3;
1590
  switch ($page_header) {
1591
    case 1:
1592
      $title = $element['#title'];
1593
      break;
1594
    case 2:
1595
      $title = t('Step %count of %total', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children)));
1596
      break;
1597
    case 3:
1598
      $title = t('Step %count of %total !label', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children), '!label' => $element['#title']));
1599
      break;
1600
    case 0:
1601
    default:
1602
      $title = '';
1603
      break;
1604
  }
1605

    
1606
  element_set_attributes($element, array('id'));
1607
  _form_set_class($element, array('form-wrapper'));
1608

    
1609
  $output = '<div' . drupal_attributes($element['#attributes']) . '>';
1610
  if (!empty($element['#title'])) {
1611
    // Always wrap fieldset legends in a SPAN for CSS positioning.
1612
    $output .= '<h2 class="multipage-pane-title"><span>' . $title . '</span></h2>';
1613
  }
1614
  $output .= '<div class="fieldset-wrapper multipage-pane-wrapper">';
1615
  if (!empty($element['#description'])) {
1616
    $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
1617
  }
1618
  $output .= $element['#children'];
1619
  if (isset($element['#value'])) {
1620
    $output .= $element['#value'];
1621
  }
1622

    
1623
  // Add a page counter if needed.
1624
  // counter array(0 => t('No'), 1 => t('Format 1 / 10'), 2 => t('The count number only'));
1625
  $page_counter_format = isset($parent_group->format_settings['instance_settings']['page_counter']) ? $parent_group->format_settings['instance_settings']['page_counter'] : 1;
1626
  $multipage_element['#page_counter_rendered'] = '';
1627
  if ($page_counter_format == 1) {
1628
    $output .= t('<span class="multipage-counter">%count / %total</span>', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children)));
1629
  }
1630
  elseif ($page_counter_format == 2) {
1631
    $output .=  t('<span class="multipage-counter">%count</span>', array('%count' => $multipages[$parent_group->group_name]));
1632
  }
1633

    
1634
  $output .= '</div>';
1635
  $output .= "</div>\n";
1636

    
1637
  return $output;
1638

    
1639
}
1640

    
1641
/**
1642
 * Get all groups.
1643
 *
1644
 * @param $entity_type
1645
 *   The name of the entity.
1646
 * @param $bundle
1647
 *   The name of the bundle.
1648
 * @param $view_mode
1649
 *   The view mode.
1650
 * @param $reset.
1651
 *   Whether to reset the cache or not.
1652
 */
1653
function field_group_info_groups($entity_type = NULL, $bundle = NULL, $view_mode = NULL, $reset = FALSE) {
1654
  static $groups = FALSE;
1655

    
1656
  if (!$groups || $reset) {
1657
    if (!$reset && $cached = cache_get('field_groups', 'cache_field')) {
1658
      $groups = $cached->data;
1659
    }
1660
    else {
1661
      $ctools_export_load_object = &drupal_static('ctools_export_load_object');
1662
      $ctools_export_load_object_all = &drupal_static('ctools_export_load_object_all');
1663
      unset($ctools_export_load_object['field_group']);
1664
      unset($ctools_export_load_object_all['field_group']);
1665
      $groups = field_group_read_groups();
1666
      cache_set('field_groups', $groups, 'cache_field');
1667
    }
1668
  }
1669

    
1670
  if (!isset($entity_type)) {
1671
    return $groups;
1672
  }
1673
  elseif (!isset($bundle) && isset($groups[$entity_type])) {
1674
    return $groups[$entity_type];
1675
  }
1676
  elseif (!isset($view_mode) && isset($groups[$entity_type][$bundle])) {
1677
    return $groups[$entity_type][$bundle];
1678
  }
1679
  elseif (isset($groups[$entity_type][$bundle][$view_mode])) {
1680
    return $groups[$entity_type][$bundle][$view_mode];
1681
  }
1682
  return array();
1683
}
1684

    
1685
/**
1686
 * Read all groups.
1687
 *
1688
 * @param array $conditions
1689
 *   Parameters for the query, as elements of the $conditions array.
1690
 *   'entity_type' The name of the entity type.
1691
 *   'bundle' The name of the bundle.
1692
 *   'mode' The view mode.
1693
 *
1694
 * @param boolean $enabled
1695
 *   Return enabled or disabled groups.
1696
 *
1697
 * @return array
1698
 *   Array of groups.
1699
 */
1700
function field_group_read_groups($conditions = array(), $enabled = TRUE) {
1701

    
1702
  $groups = array();
1703
  ctools_include('export');
1704

    
1705
  if (empty($conditions)) {
1706
    $records = ctools_export_load_object('field_group');
1707
  }
1708
  else {
1709
    $records = ctools_export_load_object('field_group', 'conditions', $conditions);
1710
  }
1711

    
1712
  foreach ($records as $group) {
1713

    
1714
    // Return only enabled groups.
1715
    if ($enabled && isset($group->disabled) && $group->disabled) {
1716
      continue;
1717
    }
1718
    // Return only disabled groups.
1719
    elseif (!$enabled && (!isset($group->disabled) || !$group->disabled)) {
1720
      continue;
1721
    }
1722

    
1723
    $groups[$group->entity_type][$group->bundle][$group->mode][$group->group_name] = field_group_unpack($group);
1724

    
1725
  }
1726
  drupal_alter('field_group_info', $groups);
1727
  return $groups;
1728

    
1729
}
1730

    
1731
/**
1732
 * Utility function to recreate identifiers.
1733
 */
1734
function _field_group_recreate_identifiers() {
1735

    
1736
  // Migrate the field groups so they have a unique identifier.
1737
  $result = db_select('field_group', 'fg')
1738
    ->fields('fg')
1739
    ->execute();
1740
  $rows = array();
1741
  foreach($result as $row) {
1742
    $row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
1743
    $row->data = unserialize($row->data);
1744
    $rows[] = $row;
1745
  }
1746
  foreach ($rows as $row) {
1747
    drupal_write_record('field_group', $row, array('id'));
1748
  }
1749

    
1750
}
1751

    
1752
/**
1753
 * Checks if a field_group exists in required context.
1754
 *
1755
 * @param String $group_name
1756
 *   The name of the group.
1757
 * @param String $entity_type
1758
 *   The name of the entity.
1759
 * @param String $bundle
1760
 *   The bundle for the entity.
1761
 * @param String $mode
1762
 *   The view mode context the group will be rendered.
1763
 */
1764
function field_group_exists($group_name, $entity_type, $bundle, $mode) {
1765
  $groups = field_group_read_groups();
1766
  return !empty($groups[$entity_type][$bundle][$mode][$group_name]);
1767
}
1768

    
1769
/**
1770
 * Unpacks a database row in a FieldGroup object.
1771
 * @param $packed_group
1772
 *   Database result object with stored group data.
1773
 * @return $group
1774
 *   Field group object.
1775
 */
1776
function field_group_unpack($packed_group) {
1777
  if (empty($packed_group->data)) {
1778
    return $packed_group;
1779
  }
1780

    
1781
  // Extract unserialized data.
1782
  $group = clone $packed_group;
1783
  $data = $group->data;
1784
  unset($group->data);
1785
  $group->label = isset($data['label']) ? $data['label'] : '';
1786
  $group->weight = isset($data['weight']) ? $data['weight'] : '';
1787
  $group->children = isset($data['children']) ? $data['children'] : '';
1788
  $group->format_type = !empty($data['format_type']) ? $data['format_type'] : 'fieldset';
1789
  if (isset($data['format_settings'])) {
1790
    $group->format_settings = $data['format_settings'];
1791
  }
1792

    
1793
  return $group;
1794
}
1795

    
1796
/**
1797
 * Packs a FieldGroup object into a database row.
1798
 * @param $group
1799
 *   FieldGroup object.
1800
 * @return $record
1801
 *   Database row object, ready to be inserted/update
1802
 */
1803
function field_group_pack($group) {
1804

    
1805
  $record = clone $group;
1806
  $record->data = array(
1807
    'label' => $record->label,
1808
    'weight' => $record->weight,
1809
    'children' => $record->children,
1810
    'format_type' => !empty($record->format_type) ? $record->format_type : 'fieldset',
1811
  );
1812
  if (isset($record->format_settings)) {
1813
    $record->data['format_settings'] = $record->format_settings;
1814
  }
1815
  return $record;
1816
}
1817

    
1818
/**
1819
 * Delete a field group.
1820
 * This function is also called by ctools export when calls are
1821
 * made through ctools_export_crud_delete().
1822
 *
1823
 * @param $group
1824
 *   A group definition.
1825
 * @param $ctools_crud
1826
 *  Is this function called by the ctools crud delete.
1827
 */
1828
function field_group_group_export_delete($group, $ctools_crud = TRUE) {
1829

    
1830
  $query = db_delete('field_group');
1831

    
1832
  if (isset($group->identifier)) {
1833
    $query->condition('identifier', $group->identifier);
1834
    if (!$ctools_crud) {
1835
      ctools_export_crud_disable('field_group', $group->identifier);
1836
    }
1837
  }
1838
  elseif (isset($group->id)) {
1839
    $query->condition('id', $group->id);
1840
  }
1841

    
1842
  if (!empty($group->mode)) {
1843
    $query->condition('mode', $group->mode);
1844
  }
1845

    
1846
  $query->execute();
1847

    
1848
  cache_clear_all('field_groups', 'cache_field');
1849
  module_invoke_all('field_group_delete_field_group', $group);
1850

    
1851
}
1852

    
1853
/**
1854
 * field_group_group_save().
1855
 *
1856
 * Saves a group definition.
1857
 * This function is called by ctools export when calls are made
1858
 * through ctools_export_crud_save().
1859
 *
1860
 * @param $group
1861
 *   A group definition.
1862
 */
1863
function field_group_group_save(& $group) {
1864

    
1865
  // Prepare the record.
1866
  $object = field_group_pack($group);
1867

    
1868
  if (isset($object->export_type) && $object->export_type & EXPORT_IN_DATABASE) {
1869
    // Existing record.
1870
    $update = array('id');
1871
    module_invoke_all('field_group_update_field_group', $object);
1872
  }
1873
  else {
1874
    // New record.
1875
    $update = array();
1876
    $object->export_type = EXPORT_IN_DATABASE;
1877
    module_invoke_all('field_group_create_field_group', $object);
1878
  }
1879

    
1880
  return drupal_write_record('field_group', $object, $update);
1881

    
1882
}
1883

    
1884
/**
1885
 * Function to retrieve all format possibilities for the fieldgroups.
1886
 */
1887
function field_group_formatter_info($display_overview = FALSE) {
1888
  $cache = &drupal_static(__FUNCTION__, array());
1889
  if (empty($cache)) {
1890
    if ($cached = cache_get('field_group_formatter_info', 'cache_field')) {
1891
      $formatters = $cached->data;
1892
    }
1893
    else {
1894
      $formatters = array();
1895
      $formatters += module_invoke_all('field_group_formatter_info');
1896
      $hidden_region = array(
1897
        'label' => '<' . t('Hidden') . '>',
1898
        'description' => '',
1899
        'format_types' => array(),
1900
        'instance_settings' => array(),
1901
        'default_formatter' => '',
1902
      );
1903
      //$formatters['form']['hidden'] = $hidden_region;
1904
      $formatters['display']['hidden'] = $hidden_region;
1905
      cache_set('field_group_formatter_info', $formatters, 'cache_field');
1906
    }
1907
    $cache = $formatters;
1908
  }
1909
  return $cache;
1910
}
1911

    
1912
/**
1913
 * Attach groups to the (form) build.
1914
 *
1915
 * @param Array $element
1916
 *   The part of the form.
1917
 * @param String $view_mode
1918
 *   The mode for the build.
1919
 * @param Array $form_state
1920
 *   The optional form state when in view_mode = form context.
1921
 */
1922
function field_group_attach_groups(&$element, $view_mode, $form_state = array()) {
1923

    
1924
  $entity_type = $element['#entity_type'];
1925
  $bundle = $element['#bundle'];
1926

    
1927
  $element['#groups'] = field_group_info_groups($entity_type, $bundle, $view_mode);
1928
  $element['#fieldgroups'] = $element['#groups'];
1929

    
1930
  // Create a lookup array.
1931
  $group_children = array();
1932
  foreach ($element['#groups'] as $group_name => $group) {
1933
    if (!empty($group->children)) {
1934
      foreach ($group->children as $child) {
1935
        $group_children[$child] = $group_name;
1936
      }
1937
    }
1938
  }
1939
  $element['#group_children'] = $group_children;
1940

    
1941
}
1942

    
1943
/**
1944
 * Pre render callback for rendering groups.
1945
 * @see field_group_field_attach_form
1946
 * @param $element Form that is being rendered.
1947
 */
1948
function field_group_form_pre_render(&$element) {
1949
  return field_group_build_entity_groups($element, 'form');
1950
}
1951

    
1952
/**
1953
 * Preprocess/ Pre-render callback.
1954
 *
1955
 * @see field_group_form_pre_render()
1956
 * @see field_group_theme_registry_alter
1957
 * @see field_group_fields_nest()
1958
 * @param $vars preprocess vars or form element
1959
 * @param $type The type of object being rendered
1960
 * @return $element Array with re-arranged fields in forms.
1961
 */
1962
function field_group_build_entity_groups(&$vars, $type) {
1963

    
1964
  if ($type == 'form') {
1965
    $element = &$vars;
1966
    $nest_vars = NULL;
1967
  }
1968
  else {
1969
    $element = &$vars['elements'];
1970
    $nest_vars = &$vars;
1971
  }
1972

    
1973
  // No groups on the entity.
1974
  if (empty($element['#fieldgroups'])) {
1975
    return $element;
1976
  }
1977

    
1978
  // Nest the fields in the corresponding field groups.
1979
  field_group_fields_nest($element, $nest_vars);
1980

    
1981
  // Allow others to alter the pre_rendered build.
1982
  drupal_alter('field_group_build_pre_render', $element);
1983

    
1984
  // Return the element on forms.
1985
  if ($type == 'form') {
1986
    return $element;
1987
  }
1988

    
1989
  // No groups on the entity. Prerender removed empty field groups.
1990
  if (empty($element['#fieldgroups'])) {
1991
    return $element;
1992
  }
1993

    
1994
  // Put groups inside content if we are rendering an entity_view.
1995
  foreach ($element['#fieldgroups'] as $group) {
1996
    if (!empty($element[$group->group_name]) && $type != 'user_profile') {
1997
      $vars['content'][$group->group_name] = $element[$group->group_name];
1998
    }
1999
    elseif (!empty($element[$group->group_name])) {
2000
      $vars['user_profile'][$group->group_name] = $element[$group->group_name];
2001
    }
2002
  }
2003

    
2004
  // New css / js can be attached.
2005
  drupal_process_attached($element);
2006
}
2007

    
2008
/**
2009
 * Recursive function to nest fields in the field groups.
2010
 *
2011
 * This function will take out all the elements in the form and
2012
 * place them in the correct container element, a fieldgroup.
2013
 * The current group element in the loop is passed recursively so we can
2014
 * stash fields and groups in it while we go deeper in the array.
2015
 * @param Array $element
2016
 *   The current element to analyse for grouping.
2017
 * @param Array $vars
2018
 *   Rendering vars from the entity being viewed.
2019
 */
2020
function field_group_fields_nest(&$element, &$vars = NULL) {
2021

    
2022
  // Create all groups and keep a flat list of references to these groups.
2023
  $group_references = array();
2024
  foreach ($element['#fieldgroups'] as $group_name => $group) {
2025
    // check for any erroneous groups from other modules
2026
    if (is_string($group_name)) {
2027
      // Construct own weight, as some fields (for example preprocess fields) don't have weight set.
2028
      $element[$group_name] = array();
2029
      $group_references[$group_name] = &$element[$group_name];
2030
    }
2031
  }
2032

    
2033
  // Loop through all form children looking for those that are supposed to be
2034
  // in groups, and insert placeholder element for the new group field in the
2035
  // correct location within the form structure.
2036
  $element_clone = array();
2037
  foreach (element_children($element) as $child_name) {
2038
    $element_clone[$child_name] = $element[$child_name];
2039
    // If this element is in a group, create the placeholder element.
2040
    if (isset($element['#group_children'][$child_name])) {
2041
      $element_clone[$element['#group_children'][$child_name]] = array();
2042
    }
2043
  }
2044
  $element = array_merge($element_clone, $element);
2045

    
2046
  // Move all children to their parents. Use the flat list of references for
2047
  // direct access as we don't know where in the root_element hierarchy the
2048
  // parent currently is situated.
2049
  foreach ($element['#group_children'] as $child_name => $parent_name) {
2050

    
2051
    // Entity being viewed
2052
    if ($vars) {
2053
      // If not a group, check vars['content'] for empty field.
2054
      if (!isset($element['#fieldgroups'][$child_name]) && isset($vars['content'][$child_name])) {
2055
        $group_references[$parent_name][$child_name] = $vars['content'][$child_name];
2056
        unset($vars['content'][$child_name]);
2057
      }
2058
      elseif (!isset($element['#fieldgroups'][$child_name]) && isset($vars['user_profile'][$child_name])) {
2059
        $group_references[$parent_name][$child_name] = $vars['user_profile'][$child_name];
2060
        unset($vars['user_profile'][$child_name]);
2061
      }
2062
      // If this is a group, we have to use a reference to keep the reference
2063
      // list intact (but if it is a field we don't mind).
2064
      else {
2065
        $group_references[$parent_name][$child_name] = &$element[$child_name];
2066
        unset($element[$child_name]);
2067
      }
2068
    }
2069
    // Form being viewed
2070
    else {
2071

    
2072
      // Block denied fields (#access) before they are put in groups.
2073
      // Fields (not groups) that don't have children (like field_permissions) are removed
2074
      // in field_group_field_group_build_pre_render_alter.
2075
      if (isset($element[$child_name]) && (!isset($element[$child_name]['#access']) || $element[$child_name]['#access'])) {
2076
        // If this is a group, we have to use a reference to keep the reference
2077
        // list intact (but if it is a field we don't mind).
2078
        $group_references[$parent_name][$child_name] = &$element[$child_name];
2079
        $group_references[$parent_name]['#weight'] = $element['#fieldgroups'][$parent_name]->weight;
2080
      }
2081

    
2082
      // The child has been copied to its parent: remove it from the root element.
2083
      unset($element[$child_name]);
2084
    }
2085

    
2086
  }
2087

    
2088
  // Bring extra element wrappers to achieve a grouping of fields.
2089
  // This will mainly be prefix and suffix altering.
2090
  foreach ($element['#fieldgroups'] as $group_name => $group) {
2091
    field_group_pre_render($group_references[$group_name], $group, $element);
2092
  }
2093

    
2094
}
2095

    
2096
/**
2097
 * Function to pre render the field group element.
2098
 *
2099
 * @see field_group_fields_nest()
2100
 *
2101
 * @param $element Array of group element that needs to be created!
2102
 * @param $group Object with the group information.
2103
 * @param $form The form object itself.
2104
 */
2105
function field_group_pre_render(& $element, $group, & $form) {
2106

    
2107
  // Only run the pre_render function if the group has elements.
2108
  // $group->group_name
2109
  if ($element == array()) {
2110
    return;
2111
  }
2112

    
2113
  // Let modules define their wrapping element.
2114
  // Note that the group element has no properties, only elements.
2115
  foreach (module_implements('field_group_pre_render') as $module) {
2116
    $function = $module . '_field_group_pre_render';
2117
    if (function_exists($function)) {
2118
      // The intention here is to have the opportunity to alter the
2119
      // elements, as defined in hook_field_group_formatter_info.
2120
      // Note, implement $element by reference!
2121
      $function($element, $group, $form);
2122
    }
2123
  }
2124

    
2125
  // Allow others to alter the pre_render.
2126
  drupal_alter('field_group_pre_render', $element, $group, $form);
2127

    
2128
}
2129

    
2130
/**
2131
 * Hides field groups including children in a render array.
2132
 *
2133
 * @param array $element
2134
 *   A render array. Can be a form, node, user, ...
2135
 * @param array $group_names
2136
 *   An array of field group names that should be hidden.
2137
 */
2138
function field_group_hide_field_groups(&$element, $group_names) {
2139
  foreach ($group_names as $group_name) {
2140
    if (isset($element['#fieldgroups'][$group_name]) && isset($element['#group_children'])) {
2141
      // Hide the field group.
2142
      $element['#fieldgroups'][$group_name]->format_type = 'hidden';
2143
      // Hide the elements inside the field group.
2144
      $sub_groups = array();
2145
      foreach (array_keys($element['#group_children'], $group_name) as $field_name) {
2146
        if (isset($element['#fieldgroups'][$field_name])) {
2147
          $sub_groups[] = $field_name;
2148
        } else {
2149
          $element[$field_name]['#access'] = FALSE;
2150
        }
2151
      }
2152
      field_group_hide_field_groups($element, $sub_groups);
2153
    }
2154
  }
2155
}
2156

    
2157
/**
2158
 * Calculates html classes for a group.
2159
 */
2160
function _field_group_get_html_classes(&$group) {
2161

    
2162
  if (isset($group->format_settings['formatter'])) {
2163
    $group->collapsible = in_array($group->format_settings['formatter'], array('collapsible', 'collapsed'));
2164
    // Open or closed horizontal or vertical tabs will be collapsible by default.
2165
    if ($group->format_type == 'tab' || $group->format_type == 'htab') {
2166
      $group->collapsible = TRUE;
2167
    }
2168
    $group->collapsed = in_array($group->format_settings['formatter'], array('collapsed', 'closed'));
2169
  }
2170

    
2171
  $classes = new stdClass();
2172

    
2173
  // Prepare extra classes, required and optional ones.
2174
  $optional = array(str_replace('_', '-', $group->group_name));
2175
  $required = array();
2176
  if ($group->format_type == 'multipage') {
2177
    $required[] = 'field-group-' . $group->format_type;
2178
  }
2179
  else {
2180
    $optional[] = 'field-group-' . $group->format_type;
2181
  }
2182

    
2183
  if (isset($group->format_settings['formatter']) && $group->collapsible) {
2184
    $required[] = 'collapsible';
2185
    if ($group->collapsed) {
2186
      $required[] = 'collapsed';
2187
    }
2188
  }
2189

    
2190
  if (isset($group->format_settings['instance_settings'])) {
2191

    
2192
    // Add a required-fields class to trigger the js.
2193
    if (!empty($group->format_settings['instance_settings']['required_fields'])) {
2194
      $required[] = 'required-fields';
2195
    }
2196

    
2197
    // Add user selected classes.
2198
    if (!empty($group->format_settings['instance_settings']['classes'])) {
2199
      $required[] = check_plain($group->format_settings['instance_settings']['classes']);
2200
    }
2201

    
2202
    // Extra required classes for div.
2203
    if ($group->format_type == 'div') {
2204
      if ($group->format_settings['formatter'] != 'open') {
2205

    
2206
        $speed = isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : 'none';
2207
        $required[] = 'speed-' . $speed;
2208

    
2209
        $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
2210
        $required[] = 'effect-' . $effect;
2211
      }
2212
    }
2213

    
2214
    // Extra required classes for accordions.
2215
    elseif ($group->format_type == 'accordion') {
2216
      $required[] = 'field-group-' . $group->format_type . '-wrapper';
2217
      $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
2218
      $required[] = 'effect-' . $effect;
2219
    }
2220

    
2221
  }
2222

    
2223
  $classes->required = $required;
2224
  $classes->optional = $optional;
2225

    
2226
  return $classes;
2227
}
2228

    
2229
/**
2230
 * Get the default formatter settings for a given formatter and a mode.
2231
 */
2232
function _field_group_get_default_formatter_settings($format_type, $mode) {
2233

    
2234
  $field_group_types = field_group_formatter_info();
2235
  $display_mode = $mode == 'form' ? 'form' : 'display';
2236
  $formatter = $field_group_types[$display_mode][$format_type];
2237

    
2238
  return array(
2239
    'formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '',
2240
    'instance_settings' => $formatter['instance_settings']
2241
  );
2242
}
2243

    
2244
/**
2245
 * Callback to bulk export field groups.
2246
 */
2247
function field_group_field_group_to_hook_code($data, $module) {
2248
  ctools_include('export');
2249
  $schema = ctools_export_get_schema('field_group');
2250
  $export = $schema['export'];
2251

    
2252
  $translatables = array();
2253
  $objects = ctools_export_load_object('field_group', 'names', array_values($data));
2254
  $code = "/**\n";
2255
  $code .= " * Implements hook_{$export['default hook']}()\n";
2256
  $code .= " */\n";
2257
  $code .= "function " . $module . "_{$export['default hook']}() {\n";
2258
  $code .= "  \${$export['identifier']}s = array();\n\n";
2259
  foreach ($objects as $object) {
2260
    $code .= ctools_export_object('field_group', $object, '  ');
2261
    $code .= "  \${$export['identifier']}s['" . check_plain($object->$export['key']) . "'] = \${$export['identifier']};\n\n";
2262
    if (!empty($object->data['label'])) {
2263
      $translatables[] =  $object->data['label'];
2264
    }
2265
    if (!empty($object->data['description'])) {
2266
      $translatables[] =  $object->data['description'];
2267
    }
2268
  }
2269

    
2270
  if (!empty($translatables)) {
2271
    $code .= features_translatables_export($translatables, '  ') . "\n";
2272
  }
2273

    
2274
  $code .= "  return \${$export['identifier']}s;";
2275
  $code .= "}\n";
2276
  return $code;
2277
}