Ajax Paging in Views breaks Add to Cart form

Created on 17 October 2017, over 7 years ago
Updated 7 April 2023, about 2 years ago

Paging a view with ajax enabled currently breaks Add to Cart forms & variation field updates.

Attempting to modify the ajax-loaded products attributes will cause the ajax request, to update the attributes, to fail with a 404 to /views/ajax.

Attempting to add the ajax-loaded product to the cart (even without modifying the attributes) redirects to /views/ajax, which renders the 404 page.

I have created a failing test case to show the error.

I'm trying to diagnose the cause of the issue, but could use some advice on where to look. I'm inspecting the ProductVariationAttributesWidget, initial elements and ajax loaded elements both seem to look the same. It seems like the form actions are getting replaced with the views form actions, but if so, I'm not sure when that is happening. I've stepped through and haven't seen the element's #ajax property change.

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Views integration

Created by

πŸ‡ΊπŸ‡ΈUnited States thechanceg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡΅πŸ‡±Poland sayco ToruΕ„

    It seems like the issue is related to the https://www.drupal.org/project/drupal/issues/2504115 β†’

    I've figure out some sort of solution proposed here for a views module
    https://www.drupal.org/project/drupal/issues/2504115 β†’

    which will allow us to access the original views_path that is sent over with the main request.
    I've add my own implementation to the hook_preprocess_views_view, but for the commerce_cart module it could be more or less as follows:

    function commerce_cart_preprocess_views_view(&$variables) {
      $view = $variables['view'];
      if (strpos($view->storage->get('tag'), 'commerce_cart_form') !== FALSE) {
        $variables['rows']['footer'] = $variables['footer'];
        $variables['footer'] = '';
    
        $request = \Drupal::requestStack()->getMainRequest();
        $is_ajax_reload = $request->isXmlHttpRequest() && $request->request->get('_drupal_ajax') === '1';
        if (FALSE === $is_ajax_reload) {
          return;
        }
        $variables['rows']['#action'] = $request->request->get('view_path');
      }
    }
    

    Seems to be working as expected now.

Production build 0.71.5 2024