You are here

function commerce_product_urls_form_alter in Commerce Product URLs 7

Implements hook_form_alter().

If there is more than one product assigned to current product display, we want to add current product's ID to line item's display_path value, to make sure that cart items link back to correct products. Code borrowed from commerce_cart_add_to_cart_form().

File

./commerce_product_urls.module, line 40
Implements unique URLs for particular products on product displays. See d.o. issue #1082596: http://drupal.org/node/1082596

Code

function commerce_product_urls_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {

    // EXPERIMENTAL CODE: Add JS file adding a new command to Drupal's Ajax
    // framework for changing the URL when selected value of any of the
    // attribute fields changes. That JS is using HTML5 history.pushState(),
    // which obviously is not going to work in all browsers, for the moment
    // though I don't see any better solution that would allow to update
    // the URL query parameters without reloading the page.
    if (variable_get('commerce_product_urls_update_url', FALSE)) {

      // We want to do it on node (product display) pages only. In any other
      // case (for example on views listings etc) URL should not be modified.
      if (!empty(menu_get_object()->nid)) {

        // Just in case if it was not added before.
        drupal_add_library('system', 'drupal.ajax');
        $form['product_id']['#attached'] = array(
          'js' => array(
            drupal_get_path('module', 'commerce_product_urls') . '/commerce_product_urls.js',
          ),
        );
        $initial_parsed_url = array();
        if (variable_get('commerce_product_urls_url_key', 'id') == 'sku') {
          $initial_parsed_url['sku'] = $form_state['default_product']->sku;
        }
        else {
          $initial_parsed_url['id'] = $form_state['default_product']->product_id;
        }

        // Pass URL update fallback setting to Javascript, so that it knows how to
        // behave when it detects we deal with an older browser without HTML5 support.
        $js_settings = array(
          'commerceProductURLs' => array(
            'updateURLFallback' => variable_get('commerce_product_urls_update_url_fallback', FALSE),
            'product_id' => drupal_http_build_query($initial_parsed_url),
          ),
        );
        drupal_add_js($js_settings, 'setting');
      }
    }

    // Retrieve the array of product IDs from the line item's context data array,
    // and add current product's ID to line item's display_path value.
    $product_ids = _commerce_product_urls_get_product_ids_from_line_item($form_state['line_item']);
    if (count($product_ids) > 1) {
      $parsed_url = drupal_parse_url($form_state['build_info']['args'][0]->data['context']['display_path']);
      if (isset($form_state['default_product']->product_id)) {
        $parsed_url['query'] = _commerce_product_urls_build_query_string($form_state, FALSE);
      }
      $parsed_url['absolute'] = FALSE;
      $parsed_url['alias'] = TRUE;
      $parsed_url['language'] = $GLOBALS['language'];

      // First argument is always a line item object (see commerce_cart_field_attach_view_alter()).
      $form_state['build_info']['args'][0]->data['context']['display_path'] = ltrim(url($parsed_url['path'], $parsed_url), '/');
    }

    // Our own submit callback to add better form redirect, going back
    // to the same product variation that was just added to the cart.
    $form['#submit'][] = 'commerce_product_urls_commerce_cart_add_to_cart_form_submit';
  }
}