Projet

Général

Profil

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

root / drupal7 / sites / all / modules / commerce_fieldgroup_panes / commerce_fieldgroup_panes.module @ 76df55b7

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', TRUE);
15

    
16
  // Check if the $groups is an array and if it contains
17
  // at least one element. If not return no new
18
  // panel.
19
  if (!is_array($groups) || empty($groups)) {
20
    return $checkout_panes;
21
  }
22

    
23
  // Iterate over the field groups and convert them into
24
  // checkout panes. By default the panes are not enabled.
25
  foreach ($groups as $group) {
26
    $group_label = check_plain(t($group->label));
27
    $checkout_panes['commerce_fieldgroup_pane__' . $group->group_name] = array(
28
      'name' => t('Order Fieldgroup: @group_label', array('@group_label' => $group_label)),
29
      'title' => $group_label,
30
      'base' => 'commerce_fieldgroup_panes_contents',
31
      'enabled' => FALSE,
32
    );
33
  }
34

    
35
  return $checkout_panes;
36
}
37

    
38
/**
39
 * Implements the callback of the checkout pane form setting
40
 */
41
function commerce_fieldgroup_panes_contents_settings_form($checkout_pane) {
42
  $form = array();
43
  return $form;
44
}
45

    
46
/**
47
 * Implements the callback of the checkout pane form
48
 */
49
function commerce_fieldgroup_panes_contents_checkout_form($form, &$form_state, $checkout_pane, $order) {
50
  $pane_form = array('#parents' => array($checkout_pane['pane_id']));
51
  list($unused, $identifier) = explode('__', $checkout_pane['pane_id']);
52

    
53
  $form_state['commerce_order'] = $order;
54

    
55
  // Attach the selected field group as form to the panel form
56
  field_attach_form('commerce_order', $order, $pane_form, $form_state, $langcode = NULL);
57

    
58
  $group = $pane_form['#fieldgroups'][$identifier];
59
  $children = $group->children;
60
  $info = field_group_read_groups(array('bundle' => $order->type, 'entity_type' => 'commerce_order', 'mode' => 'form'));
61
  $field_groups = $info['commerce_order'][$order->type]['form'];
62
  foreach($group->children as $group_child) {
63
    if (isset($field_groups[$group_child])) {
64
      foreach ($field_groups[$group_child]->children as $child) {
65
        $children[] = $child;
66
      }
67
    }
68
  }
69

    
70
  // Set the field group formatter, to prevent the field_group module
71
  // to add a field_set element to it.
72
  $pane_form['#fieldgroups'][$identifier]->format_type = 'commerce_fieldgroup_panes';
73
  $pane_form['#description'] = isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : '';
74

    
75
  // Iterate over all field elements and remove
76
  // the elments which are not subelements of the current field group
77
  foreach (element_children($pane_form) as $child) {
78
    if (!in_array($child, $children)) {
79
      unset($pane_form[$child]);
80
    }
81
  }
82

    
83
  // Check for form pre_render callback set by Fieldgroup 2.x.
84
  $position = array_search('field_group_form_pre_render', $pane_form['#pre_render']);
85
  if ($position !== FALSE) {
86
    // Insert our pre_render callback.
87
    array_splice($pane_form['#pre_render'], $position, 0, 'commerce_fieldgroup_panes_field_group_form_pre_render');
88
  }
89

    
90
  return $pane_form;
91
}
92

    
93
/**
94
 * Implements the callback for the checkout pane form submit
95
 */
96
function commerce_fieldgroup_panes_contents_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
97
  // Do nothing unless the checkout pane still exists.
98
  if (!isset($form[$checkout_pane['pane_id']])) {
99
    return;
100
  }
101

    
102
  // Notify field widgets -> attach the $form_state['values'] to the $order object
103
  field_attach_submit('commerce_order', $order, $form[$checkout_pane['pane_id']], $form_state);
104

    
105
  // Save the order
106
  commerce_order_save($order);
107
}
108

    
109
/**
110
 * Implements the callback for the checkout pane review form
111
 */
112
function commerce_fieldgroup_panes_contents_review($form, $form_state, $checkout_pane, $order) {
113
  list($unused, $identifier) = explode('__', $checkout_pane['pane_id']);
114

    
115
  $groups = field_group_info_groups('commerce_order', 'commerce_order', 'form');
116
  $group = $groups[$identifier];
117

    
118
  $output = '';
119

    
120
  // Iterate over all field group childrens
121
  // and render them to produce the review pane content
122
  foreach ($group->children as $child) {
123
    // Get the field view by using the 'checkout_pane' view mode.
124
    $field = field_view_field('commerce_order', $order, $child, 'checkout_pane');
125
    $output .= drupal_render($field);
126
  }
127

    
128
  return $output;
129
}
130

    
131
/**
132
 * Implementation of hook_entity_info_alter
133
 *
134
 * We need an additional view_mode. By altering the entity we can add
135
 * this view mode.
136
 */
137
function commerce_fieldgroup_panes_entity_info_alter(&$entity_info) {
138
  // Alter the commerce_order entity to add the additional view mode
139
  $entity_info['commerce_order']['view modes']['checkout_pane'] = array(
140
    'label' => t('Checkout Pane'),
141
    'custom settings' => FALSE,
142
  );
143
}
144

    
145
/**
146
 * Pre render callback for rendering groups.
147
 *
148
 * @see field_group_field_attach_form
149
 * @param $element Form that is beïng rendered.
150
 *
151
 * Runs immediately before field_group_form_pre_render() when Fieldgroup 2.x installed.
152
 */
153
function commerce_fieldgroup_panes_field_group_form_pre_render(&$element) {
154
  $element['#groups'] = array_merge($element['#groups'], $element['#fieldgroups']);
155
  return $element;
156
}