You are here

function commerce_wishlist_form_alter in Commerce Wishlist 7

Same name and namespace in other branches
  1. 7.3 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 105
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 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') {
        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 = TRUE;
    if ($product_types) {
      if (empty($product_types[$product_type])) {
        $access = FALSE;
      }
    }
    $product_id = $form_state['default_product']->product_id;
    $nid = _commerce_wishlist_get_context_entity_id($form_state['context']);
    $element = variable_get('commerce_wishlist_element', 'button');
    $in_wishlist = commerce_wishlist_in_wishlist($user->uid, $product_id);
    $link_exists = _commerce_wishlist_link($user->uid);
    switch ($element) {
      case 'button':
        $form += commerce_wishlist_add_form();
        if ($in_wishlist) {
          $form['add_to_wishlist'] = array(
            '#markup' => $link_exists,
            '#weight' => variable_get('commerce_wishlist_weight', 0),
          );
        }
        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 (!$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;
        }
        $form['add_to_wishlist'] = array(
          '#markup' => $link,
          '#weight' => variable_get('commerce_wishlist_weight', 0),
        );
        break;
    }
    $form['add_to_wishlist']['#access'] = $access;
  }
  if ($form_id == 'user_login' && !empty($_GET['product_id'])) {
    $form['#submit'][] = '_commerce_wishlist_user_login';
  }
}