You are here

function commerce_wishlist_form_alter in Commerce Wishlist 7.3

Same name and namespace in other branches
  1. 7 commerce_wishlist.module \commerce_wishlist_form_alter()
  2. 7.2 commerce_wishlist.module \commerce_wishlist_form_alter()

Implements hook_form_alter().

File

./commerce_wishlist.module, line 265
Provides a wish list for use in Drupal Commerce.

Code

function commerce_wishlist_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'commerce_order_ui_order_form') {
    if (!commerce_wishlist_order_is_wishlist($form_state['commerce_order'])) {
      $form['commerce_wishlist_visibility']['#access'] = FALSE;
    }
  }
  if (strstr($form_id, 'commerce_cart_add_to_cart_form')) {
    $form['#submit'][] = 'commerce_wishlist_product_added_to_cart';

    // Check if the product is disabled.
    if (isset($form['submit']['#attributes']['disabled']) && $form['submit']['#attributes']['disabled'] == 'disabled') {
      return;
    }

    // Make sure that this product is allowed.
    if (!in_array($form_state['default_product']->type, array_filter(variable_get('commerce_wishlist_product_types', array())))) {
      return;
    }
    $product_id = $form_state['default_product']->product_id;

    // Look for the presence of the wish list line item to know if we are in the
    // view or if we are in a regular form. We know that it's already in the
    // wishlist because we're viewing the wishlist now if it's present. Set up
    // the form submit functionality based on the configuration.
    if (isset($form_state['build_info']['args'][2]['wish_list_line_item'])) {

      // Get user ID from the URL. The reason we cannot use menu_get_object()
      // here is that the argument is not defined using hook_menu() and the
      // user ID is not recognized as %user in menu router.
      if ($form_state['build_info']['args'][0]->data['context']['view']['remove_from_wishlist']) {
        $form['#submit'][] = 'commerce_wishlist_add_to_cart_remove';
      }
      return;
    }

    // If it's already in the wishlist, show the already in wishlist link.
    // Otherwise, show the button or link as configured.
    if (commerce_wishlist_user_has_product_in_wishlist($product_id)) {
      $form['add_to_wishlist'] = array(
        '#markup' => theme('commerce_wishlist_already_in_wishlist_link', array(
          'user_id' => $user->uid,
        )),
        '#weight' => variable_get('commerce_wishlist_weight', 0),
      );
    }
    elseif (variable_get('commerce_wishlist_element', 'button') == 'button') {
      $form += commerce_wishlist_add_form();
    }
    else {

      // Add the "Add to wishlist" link to the form.
      $form['add_to_wishlist'] = array(
        '#markup' => theme('commerce_wishlist_product_add_link', array(
          'product_id' => $product_id,
          'user' => $user,
        )),
        '#weight' => variable_get('commerce_wishlist_weight', 0),
      );
    }
  }
  if ($form_id == 'user_login' && !empty($_GET['wishlist_product'])) {
    $_SESSION['commerce_wishlist']['product_id'] = check_plain($_GET['wishlist_product']);
  }
}