Problem summary

AJAX requests are always made using POST, which disables render caching (https://www.drupal.org/node/956186).

Further (reason behind creating separate issue) - AJAX requests in form submit, form element validate, ajax at form element levels use the same URL as the page (partially covered in https://www.drupal.org/node/2504115).

When the AJAX request is made - first because of POST, second because of URL being the same as current page, it processes every region, block on the page including hook_ENTITY_TYPE_view, hook_form_alter (for all the forms) and anything inside as even render cache is disabled.

In such calls, we usually want only part of the form to be processed and updated and don't want anything except the form to be processed. This results into huge impact to performance, especially if there is dynamic or processed content added to node in hook_node_view or for instance there are related content blocks on the page, hook_node_view is called for each node rendered on the page (same for any other entity if used)

Proposed solution

  • Allow AJAX callbacks to use different URL than current URL.
  • For POST (or any) requests, allow setting a configuration in routes.yml which, if set true, will not process any region/block on the page and only the required form is processed.

Comments

nikunjkotecha created an issue. See original summary.

dawehner’s picture

There is also #2503429: [PP-*] Allow both AJAX and non-AJAX forms to POST to dedicated URLs which seems to be basically the same issue.

nikunjkotecha’s picture

hmmm, I went through all the open issues in ajax system, missed going through ones in forms :(

I would still like to have this one separate, reason is the place where I want it to be supported.

Imagine an address form on product page to check if delivery is possible, you select a country and do an ajax call to populate states based on the selected country but that ajax call loads the whole page even bypassing the render cache.

I wish if we can convert this:

$form['get_shipping_methods'] = [
      '#type' => 'button',
      '#value' => $this->t('deliver to this address'),
      '#ajax' => [
        'callback' => [$this, 'getShippingMethods'],
        'wrapper' => 'address_wrapper',
      ],
      '#submit' => [],
      '#weight' => -50,
      '#limit_validation_errors' => [
        ['address'],
      ],
    ];

into:

$form['get_shipping_methods'] = [
      '#type' => 'button',
      '#value' => $this->t('deliver to this address'),
      '#ajax' => [
        'callback' => ['route', 'module_name.get_shipping_methods'],
        'wrapper' => 'address_wrapper',
      ],
      '#submit' => [],
      '#weight' => -50,
      '#limit_validation_errors' => [
        ['address'],
      ],
    ];

And something similar for #element_validate

nikunjkotecha’s picture

Category: Support request » Feature request
nikunjkotecha’s picture

nikunjkotecha’s picture

At-least form form_item[#ajax], [#ajax][url] is good option, still checking how we can return rendered form here or update form easily.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Status: Active » Closed (duplicate)