Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 297377

drupal_get_form on Ajax callback returns this javascript error: “TypeError: element_settings.url.replace is not a function”

$
0
0

Given this code:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function store_form_uc_product_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#ajax'] = array('callback' => 'store_ajax_cart_add','progress' => FALSE,
  );
}

/**
 * Ajax Callback, Add a Product to the cart.
 */
function store_ajax_cart_add($form, &$form_state) {

  // Load the array of shopping cart items.
  $total_qty = uc_cart_get_total_qty();
  $items = uc_cart_get_contents();

  $commands = array();

  $build = array();

  // Display the empty cart page if there are no items in the cart.
  if (empty($items)) {
    $build['#theme'] = 'uc_empty_cart';
  }
  else {
    $build = drupal_get_form('uc_cart_view_form', $items) + array('#prefix' => '
','#suffix' => '
', ); } unset($build['cart_form']['actions']['continue_shopping']); $commands[] = ajax_command_html('.block-uc-cart .content', ''); $commands[] = ajax_command_html('.block-uc-cart .content', drupal_render($build)); $commands[] = ajax_command_before('#'.$form['#id'], '
'.theme('status_messages').'
'); $commands[] = ajax_command_invoke('div.ajax-hidden', 'ajaxSlide'); $commands[] = ajax_command_invoke('div.ajax-hidden', 'removeClass', array('ajax-hidden')); if (!empty($total_qty)) { $commands[] = ajax_command_after('.cart-block-icon-empty', ''); $commands[] = ajax_command_invoke('.cart-block-item-count', 'text', array(number_format($total_qty))); $commands[] = ajax_command_invoke('.cart-block-icon-empty', 'addClass', array('cart-block-icon-full')); $commands[] = ajax_command_invoke('.cart-block-icon-empty', 'removeClass', array('cart-block-icon-empty')); } return array('#type' => 'ajax', '#commands' => $commands); }

Every time the form is submitted (with Ajax) I get this Javascript error:

TypeError: element_settings.url.replace is not a function
this.url = element_settings.url.replace(/\/nojs(\/|$|\?|&|#)/g, '/ajax$1');
ajax.js?v=7.14 (line 132)

I discovered that this has to do with the drupal_get_form in the ajax callback. When I remove the form get, the error disappears.

Also, if I go to misc/ajax.js and remove line 132 it also clears up the errors.

I'd like to not modify core (misc/ajax.js), and I'd also like to return the form with Ajax.

Am I doing something wrong is is this a Drupal bug?


Viewing all articles
Browse latest Browse all 297377

Trending Articles