1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* This module adds for each field group of the order entity a new checkout
|
6
|
* pane.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Implements hook_commerce_checkout_pane_info().
|
11
|
*/
|
12
|
function commerce_fieldgroup_panes_commerce_checkout_pane_info() {
|
13
|
$checkout_panes = array();
|
14
|
$groups = field_group_info_groups('commerce_order', 'commerce_order', 'form');
|
15
|
|
16
|
// Check if the $groups is an array and if it contains at least one element.
|
17
|
// If not return no new panel.
|
18
|
if (!is_array($groups) || empty($groups)) {
|
19
|
return $checkout_panes;
|
20
|
}
|
21
|
|
22
|
// Iterate over the field groups and convert them into checkout panes. By
|
23
|
// default the panes are not enabled.
|
24
|
foreach ($groups as $group) {
|
25
|
$group_label = t($group->label);
|
26
|
$checkout_panes['commerce_fieldgroup_pane__' . $group->group_name] = array(
|
27
|
'name' => t('Order Fieldgroup: @group_label', array('@group_label' => $group_label)),
|
28
|
'title' => $group_label,
|
29
|
'base' => 'commerce_fieldgroup_panes_contents',
|
30
|
'enabled' => FALSE,
|
31
|
);
|
32
|
}
|
33
|
|
34
|
return $checkout_panes;
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Implements the callback of the checkout pane form setting.
|
39
|
*/
|
40
|
function commerce_fieldgroup_panes_contents_settings_form($checkout_pane) {
|
41
|
$form = array();
|
42
|
return $form;
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Implements the callback of the checkout pane form
|
47
|
*/
|
48
|
function commerce_fieldgroup_panes_contents_checkout_form($form, &$form_state, $checkout_pane, $order) {
|
49
|
$pane_form = array('#parents' => array($checkout_pane['pane_id']));
|
50
|
list(, $identifier) = explode('__', $checkout_pane['pane_id']);
|
51
|
|
52
|
// Attach the selected field group as form to the panel form.
|
53
|
field_attach_form('commerce_order', $order, $pane_form, $form_state, $langcode = NULL);
|
54
|
|
55
|
$group = $pane_form['#fieldgroups'][$identifier];
|
56
|
$children = $group->children;
|
57
|
$info = field_group_read_groups(array('bundle' => $order->type, 'entity_type' => 'commerce_order', 'mode' => 'form'));
|
58
|
$field_groups = $info['commerce_order'][$order->type]['form'];
|
59
|
foreach($group->children as $group_child) {
|
60
|
if (isset($field_groups[$group_child])) {
|
61
|
foreach ($field_groups[$group_child]->children as $child) {
|
62
|
$children[] = $child;
|
63
|
}
|
64
|
}
|
65
|
}
|
66
|
|
67
|
// Set the field group formatter, to prevent the field_group module to add a
|
68
|
// field_set element to it.
|
69
|
$pane_form['#fieldgroups'][$identifier]->format_type = 'commerce_fieldgroup_panes';
|
70
|
$pane_form['#description'] = isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : '';
|
71
|
|
72
|
// Iterate over all field elements and remove the elements which are not
|
73
|
// subelements of the current field group.
|
74
|
foreach (element_children($pane_form) as $child) {
|
75
|
if (!in_array($child, $children)) {
|
76
|
unset($pane_form[$child]);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
// Check for form pre_render callback set by Fieldgroup 2.x.
|
81
|
$position = array_search('field_group_form_pre_render', $pane_form['#pre_render']);
|
82
|
if ($position !== FALSE) {
|
83
|
// Insert our pre_render callback.
|
84
|
array_splice($pane_form['#pre_render'], $position, 0, 'commerce_fieldgroup_panes_field_group_form_pre_render');
|
85
|
}
|
86
|
|
87
|
return $pane_form;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Implements the callback for the checkout pane form submit.
|
92
|
*/
|
93
|
function commerce_fieldgroup_panes_contents_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
|
94
|
// Do nothing unless the checkout pane still exists.
|
95
|
if (!isset($form[$checkout_pane['pane_id']])) {
|
96
|
return;
|
97
|
}
|
98
|
|
99
|
// Notify field widgets -> attach the $form_state['values'] to the $order
|
100
|
// object.
|
101
|
field_attach_submit('commerce_order', $order, $form[$checkout_pane['pane_id']], $form_state);
|
102
|
commerce_order_save($order);
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Implements the callback for the checkout pane review form
|
107
|
*/
|
108
|
function commerce_fieldgroup_panes_contents_review($form, $form_state, $checkout_pane, $order) {
|
109
|
list(, $identifier) = explode('__', $checkout_pane['pane_id']);
|
110
|
|
111
|
$groups = field_group_info_groups('commerce_order', 'commerce_order', 'form');
|
112
|
$group = $groups[$identifier];
|
113
|
|
114
|
$output = array();
|
115
|
// Iterate over all field group childrens and render them to produce the
|
116
|
// review pane content.
|
117
|
foreach ($group->children as $child) {
|
118
|
// Get the field view by using the 'checkout_pane' view mode.
|
119
|
$output[$child] = field_view_field('commerce_order', $order, $child, 'checkout_pane');
|
120
|
}
|
121
|
|
122
|
return drupal_render($output);
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Implements hook_entity_info_alter().
|
127
|
*
|
128
|
* We need an additional view_mode. By altering the entity we can add
|
129
|
* this view mode.
|
130
|
*/
|
131
|
function commerce_fieldgroup_panes_entity_info_alter(&$entity_info) {
|
132
|
// Alter the commerce_order entity to add the additional view mode
|
133
|
$entity_info['commerce_order']['view modes']['checkout_pane'] = array(
|
134
|
'label' => t('Checkout Pane'),
|
135
|
'custom settings' => FALSE,
|
136
|
);
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Pre render callback for rendering groups.
|
141
|
*
|
142
|
* @see field_group_field_attach_form
|
143
|
* @param $element Form that is beïng rendered.
|
144
|
*
|
145
|
* Runs immediately before field_group_form_pre_render() when Fieldgroup 2.x installed.
|
146
|
*/
|
147
|
function commerce_fieldgroup_panes_field_group_form_pre_render(&$element) {
|
148
|
$element['#groups'] = array_merge($element['#groups'], $element['#fieldgroups']);
|
149
|
return $element;
|
150
|
}
|