Problem/Motivation

System Ajax doesn't get loaded on the page if the cart is empty. dc_ajax_add_cart_html.js requires this library and throws a

TypeError: Drupal.ajax is undefined

when it doesn't have that.

That JS error in turn prevents other JS from running on the page and breaks pages.

Proposed resolution

Just need to load library with the JS always to meet the dependency.

Comments

joelpittet created an issue. See original summary.

joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new644 bytes
mglaman’s picture

+++ b/dc_ajax_add_cart.module
@@ -336,10 +336,9 @@ function dc_ajax_add_cart_block_view($delta = '') {
       drupal_add_css(drupal_get_path('module', 'dc_ajax_add_cart') . '/css/dc_ajax_add_cart.css');
       drupal_add_js(drupal_get_path('module', 'dc_ajax_add_cart') . '/js/dc_ajax_add_cart_html.js');

If the cart is empty, do any of these resources need to be added?

joelpittet’s picture

@mglaman Yes, I believe they are needed, if you have AJAX add to cart, the resource needs to be available after the cart addition AJAX to be able to populate 'ajax-shopping-cart-wrapper'. The CSS is self explanatory.

subhojit777’s picture

Status: Needs review » Closed (works as designed)

I was not able replicate the JS error. The ajax library is required by the "remove from cart" link, and it will only appear if there is some item in the cart.

joelpittet’s picture

Status: Closed (works as designed) » Needs review

Pretty sure this is still needed

subhojit777’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new241.58 KB

You are concerned about this part of the module:

      // Hide cart if it is empty.
      if ($hide_empty_cart) {
        $block['content'] = theme('html_tag', array(
          'element' => array(
            '#tag' => 'div',
            '#value' => '',
            '#attributes' => array(
              'class' => 'ajax-shopping-cart-wrapper',
            ),
          ),
        ));
      }
      else {
        $block['content'] = '<div class="ajax-shopping-cart-wrapper">';
        $block['content'] .= theme('dc_ajax_shopping_cart', array(
          'order' => $commerce_cart['order'],
          'line_items' => $line_items,
          'quantity' => $quantity,
          'total' => $total,
        ));
        $block['content'] .= '</div>';

        drupal_add_library('system', 'drupal.ajax', TRUE);
      }

We need drupal_add_library('system', 'drupal.ajax', TRUE); so as to ajaxify the "Remove from cart" link. And this link is only going to appear if there is a product in the cart. And suppose there is a product in cart, then Drupal ajax library is going to be added in the page.

This is a vanilla Drupal + Commerce installation, there is no product in cart, and there are no JS errors.

Please mention the steps so that I can replicate the problem.

subhojit777’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)
joelpittet’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.01 KB

@subhojit777 the dc_ajax_add_cart_html.js file tries to extend Drupal.ajax and there is no check that object does or doesn't exist and that is why the error shows when you don't have drupal.ajax library loaded. It's essentially a dependency.

The latest code it should be in dc_ajax_add_cart_include_cart_resources().

Any other library that adds drupal.ajax would unintentionally solve this dependency problem.

Here's a new patch.

mglaman’s picture

+++ b/includes/dc_ajax_add_cart.inc
@@ -9,6 +9,7 @@
+  drupal_add_library('system', 'drupal.ajax', TRUE);
   drupal_add_css(drupal_get_path('module', 'dc_ajax_add_cart') . '/css/dc_ajax_add_cart.css');
   drupal_add_js(drupal_get_path('module', 'dc_ajax_add_cart') . '/js/dc_ajax_add_cart_html.js');

To make this even more of a nitpickery: why not hook_library to have dependency declared there. Then we only need to register the library and we get the CSS, JS and dependencies

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/func...

joelpittet’s picture

@mglaman was going to recommend that as well but didn't want to push my luck because it was already closed:P But if that is preferred I don't mind writing that patch.

  • subhojit777 committed 8658ac9 on 7.x-2.x authored by joelpittet
    Issue #2643158 by joelpittet, mglaman: AJAX library not added when cart...
subhojit777’s picture

Status: Needs review » Fixed

Any other library that adds drupal.ajax would unintentionally solve this dependency problem.

Yeah true. Thanks for pointing it out. I never thought it this way. I was assuming that the ajax library was added from the block view only. I carried out a test by removing drupal_add_library('system', 'drupal.ajax', TRUE); and still it was functioning. This patch is infact better than the previous one.

The ajax functionalities in the block, and ajaxifying the "Add to Cart" button, all of them needs to be decoupled, but that is not the scope of this issue. I would very much like to decouple them in the 8.x version of this module.

I've opened another issue #2891050: Use hook_library() to add resources which is going to implement Matt's suggestion.

Thank you Joël :)

joelpittet’s picture

Status: Fixed » Needs review

Thanks for committing that, I'll post a WIP patch in the other issue.

subhojit777’s picture

Status: Needs review » Fixed

We have a separate issue for adding hook_library() #2891050: Use hook_library() to add resources

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.