You are here

function commerce_add_to_cart_extras_form_alter in Commerce Add to Cart Extras 7

Implements hook_form_alter().

If this is a Views Form (determined by $form_id) that contains this module's quantity handler, alter the label of the submit button to "Add to cart".

File

./commerce_add_to_cart_extras.module, line 22

Code

function commerce_add_to_cart_extras_form_alter(&$form, &$form_state, $form_id) {
  $is_quantity_form = FALSE;
  $needs_commerce_extra_quantity_includes = FALSE;
  if (strpos($form_id, 'views_form_') === 0) {
    $view = $form_state['build_info']['args'][0];
    if (empty($view->result)) {

      // We don't need an add to cart button if there's nothing to add.
      return;
    }
    foreach ($view->field as $field) {
      if ($field instanceof commerce_add_to_cart_extras_handler_field_quantity) {
        $is_quantity_form = TRUE;
        if (!empty($field->options['commerce_extra_quantity']) && $field->options['commerce_extra_quantity'] == 1) {
          $needs_commerce_extra_quantity_includes = TRUE;
        }
      }
    }

    // Make sure the proper URL is used for the form submit action.
    $query = drupal_get_query_parameters($_GET, array(
      'q',
    ));
    $form['#action'] = url(current_path(), array(
      'query' => $query,
    ));
  }
  if ($is_quantity_form) {
    $form['actions']['submit']['#value'] = t('Add to cart');
  }
  if ($needs_commerce_extra_quantity_includes && module_exists('commerce_extra_quantity')) {
    foreach (element_children($form['add_to_cart_quantity']) as $rownum) {
      $form['add_to_cart_quantity'][$rownum]['#theme_wrappers'] = array(
        'quantity_plusminus_wrapper',
        'form_element',
      );
    }
    $form['#attached']['js'][] = drupal_get_path('module', 'commerce_extra_quantity') . '/commerce_extra_quantity.js';
    $form['#attached']['css'][] = drupal_get_path('module', 'commerce_extra_quantity') . '/commerce_extra_quantity.css';
  }
}