You are here

function commerce_wishlist_form_alter in Commerce Wishlist 7.2

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

Implements hook_form_alter().

File

./commerce_wishlist.module, line 82
Provides the wishlist for use in Drupal Commerce.

Code

function commerce_wishlist_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if (strstr($form_id, 'commerce_cart_add_to_cart_form')) {

    // Check if the product is disabled.
    if (isset($form['submit']['#attributes']['disabled']) && $form['submit']['#attributes']['disabled'] == 'disabled') {
      return;
    }
    if (isset($form_state['build_info']['args'][0]->data['context']['view'])) {
      if ($form_state['build_info']['args'][0]->data['context']['view']['view_name'] == 'wishlist') {

        // 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 (arg(1) && is_numeric(arg(1)) && ($account = user_load(arg(1)))) {

          // Get node ID from the database.
          $query = db_select('commerce_wishlist', 'cw')
            ->addTag('commerce_wishlist')
            ->fields('wi', array(
            'nid',
          ))
            ->condition('cw.uid', $account->uid)
            ->condition('wi.product_id', $form_state['default_product']->product_id);
          $query
            ->join('commerce_wishlist_item', 'wi', 'wi.wishlist_id = cw.wishlist_id');
          $nid = $query
            ->execute()
            ->fetchField();

          // Attach user information to "Add to cart" buttons.
          $form['wishlist_uid'] = array(
            '#type' => 'hidden',
            '#value' => $account->uid,
          );
          $form['wishlist_nid'] = array(
            '#type' => 'hidden',
            '#value' => $nid,
          );
          $form['#submit'][] = 'commerce_wishlist_product_added_to_cart';
        }
        return;
      }
    }

    // Check if is a product
    $product_type = $form_state['default_product']->type;
    $product_types = array_filter(variable_get('commerce_wishlist_product_types', array()));
    $access = $product_types && !empty($product_types[$product_type]);

    // Prepare all variables for "Add to wishlist" button.
    $nid = _commerce_wishlist_get_context_entity_id($form_state['context']);
    $product_id = $form_state['default_product']->product_id;
    $element = variable_get('commerce_wishlist_element', 'button');
    $in_wishlist = _commerce_wishlist_in_wishlist($user->uid, $product_id);
    $link_exists = theme('commerce_wishlist_already_in_wishlist_link', array(
      'user_id' => $user->uid,
    ));

    // If $nid is empty, this means that the product "Add to cart" form is being
    // displayed on an entity which is not node; e.g. list of commerce products.
    //
    // This functionality should be discussed in the issue queue, but for now,
    // this module does not support adding products to wishlist from non-node
    // entities.
    if (!empty($nid)) {
      switch ($element) {
        case 'button':
          $form += commerce_wishlist_add_form();
          if ($in_wishlist) {
            $form['add_to_wishlist'] = array(
              '#markup' => $link_exists,
            );
          }
          break;
        case 'link':
          $url = 'wishlist-actions/nojs/add/' . $user->uid . '/' . $product_id . '/' . $nid;
          $params = array(
            'attributes' => array(
              'class' => array(
                'ajax' => 'use-ajax',
                'add-to-wishlist',
              ),
              'id' => 'add-wishlist-' . $product_id,
            ),
            'query' => array(
              'destination' => $_GET['q'],
              'token' => drupal_get_token(),
            ),
          );

          // If the current user is not logged in, build a different link that
          // points to the login page and lists all other relevant details
          // (product ID, node ID and original URL) in query string.
          if (!$user->uid) {
            unset($params['attributes']['class']['ajax'], $params['query']);
            $params['query']['product_id'] = $product_id;
            $params['query']['nid'] = $nid;
            $params['query']['original_url'] = $_GET['q'];
            $url = 'user/login';
          }
          $link = l(t('Add to Wishlist'), $url, $params);
          if ($in_wishlist) {
            $link = $link_exists;
          }

          // Add the "Add to wishlist" button to the form.
          $form['add_to_wishlist'] = array(
            '#markup' => $link,
          );
          break;
      }
      $form['add_to_wishlist']['#weight'] = variable_get('commerce_wishlist_weight', 0);
      $form['add_to_wishlist']['#access'] = $access;
    }
  }
  if ($form_id == 'user_login' && !empty($_GET['product_id'])) {
    $form['#submit'][] = '_commerce_wishlist_user_login_submit';
  }
}