You are here

function commerce_cp_form_alter in Commerce Cart Pane 7

Implements hook_form_alter().

File

./commerce_cp.module, line 179

Code

function commerce_cp_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_form_commerce_cart_form_default') {
    global $user;
    $cart_order = commerce_cart_order_load($user->uid);

    // default weight of Summary element on cart form
    $summary_default_value = 99;
    $summary_weight = 0;

    // get a list enabled panes
    $cart_panes = variable_get('commerce_cp_panes_weight', array());

    // get a summary weight for recalculating weight of else panes
    if (isset($cart_panes['summary'])) {
      $summary_weight = $cart_panes['summary'];
    }

    // get a list of all panes
    $custom_panes = module_invoke_all('commerce_cp_info');

    // get keys of all system panes
    $system_panes = commerce_cp_get_system_panes(TRUE);

    // get list of system panes that should be hidden
    $hidden_system_panes = array_diff($system_panes, array_keys($cart_panes));

    // hide corresponding system panes
    foreach ($hidden_system_panes as $pane_id) {
      if (isset($form[$pane_id])) {
        $form[$pane_id]['#access'] = FALSE;
      }
    }

    // go throught all cart panes and combine them in cart form.
    // also recalculate a weight of all panes in relation to Summary
    // weight because Summary form element cannot be hidden and its weight
    // is always fixed and = 99.
    foreach ($cart_panes as $pane_id => $weight) {
      if ($pane_id != 'summary') {

        // recalculate weight for all panes except Summary pane
        $pane_weight = $summary_default_value + $weight - $summary_weight;

        // if its system pane - just setup a weight
        if (isset($form[$pane_id])) {
          $form[$pane_id]['#weight'] = $pane_weight;
        }
        else {
          if (isset($custom_panes[$pane_id])) {
            $callback = $custom_panes[$pane_id]['pane callback'];
            if (function_exists($callback)) {
              $form += $callback($form, $form_state, $cart_order, $pane_weight);
            }
          }
        }
      }
    }
  }
}