function commerce_webform_form_views_form_commerce_cart_form_default_alter in Commerce Webform 8
Same name and namespace in other branches
- 7.2 commerce_webform.module \commerce_webform_form_views_form_commerce_cart_form_default_alter()
- 7 commerce_webform.module \commerce_webform_form_views_form_commerce_cart_form_default_alter()
Implements hook_form_FORM_ID_alter(). Modify the cart modification form.
File
- ./
commerce_webform.module, line 580 - Commerce Webform module file
Code
function commerce_webform_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state, $form_id) {
// Store a list of all webform added products in this order.
$webforms = array();
// Go through all items in the basket and note if they are webform
// products. If they cannot have their quantities changed, remove
// the quantity edit textfield.
foreach (element_children($form['edit_quantity']) as $id) {
// If this is a commerce webform item, check if it is required.
$line_item_id = $form['edit_quantity'][$id]['#line_item_id'];
$line_item = commerce_line_item_load($line_item_id);
if ($line_item->type == 'commerce_webform') {
if (!in_array('_commerce_webform_cart_validate', $form['#validate']) && $line_item->data['commerce_webform']['choose_quantity'] && $line_item->data['commerce_webform']['required']) {
// This is a commerce_webform line item with variable quantity
// and is required, add a validation function to stop it from
// being set to 0.
array_unshift($form['#validate'], '_commerce_webform_cart_validate');
}
$nid = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_webform_nid
->value();
if (!isset($webforms[$nid])) {
$webforms[$nid] = array(
'count' => 0,
'title' => node_load($nid)->title,
);
}
$webforms[$nid]['count']++;
if (!$line_item->data['commerce_webform']['choose_quantity']) {
// Remove the ability to change the quantity.
$form['edit_quantity'][$id]['#type'] = 'value';
$form['edit_quantity'][$id]['quantity'] = array(
'#markup' => '<p>' . $form['edit_quantity'][$id]['#default_value'] . '</p>',
);
}
}
}
// Go through each of the delete buttons on the form. If they
// remove a commerce webform added item, show a message to
// connect them to the webform. If they are required add
// a submission function which removes all other webform
// items from the basket.
foreach (element_children($form['edit_delete']) as $id) {
if (!empty($form['edit_delete'][$id]['#line_item_id'])) {
$line_item_id = $form['edit_delete'][$id]['#line_item_id'];
$line_item = commerce_line_item_load($line_item_id);
if ($line_item->type == 'commerce_webform') {
$nid = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_webform_nid
->value();
$form['edit_delete'][$id]['#suffix'] = '<br /><em>' . t('Part of %webform', array(
'%webform' => $webforms[$nid]['title'],
)) . '</em>';
if ($webforms[$nid]['count'] > 1 && $line_item->data['commerce_webform']['required']) {
$form['edit_delete'][$id]['#suffix'] .= '<br /><em>' . t('Removing this item will remove the other %webform items from your basket.', array(
'%webform' => $webforms[$nid]['title'],
)) . '</em>';
array_unshift($form['edit_delete'][$id]['#submit'], '_commerce_webform_remove_required_product_from_order');
}
}
}
}
}