Verified Commit b2a606d1 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3348789 by catch, Altcom_Neil, markie, lauriii, pelicani, longwave,...

Issue #3348789 by catch, Altcom_Neil, markie, lauriii, pelicani, longwave, olli: Compress ajax_page_state
parent 1229f8c3
Loading
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -891,6 +891,10 @@ services:
  argument_resolver.default:
    class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver
    public: false
  http_middleware.ajax_page_state:
    class: Drupal\Core\StackMiddleware\AjaxPageState
    tags:
      - { name: http_middleware, priority: 500 }
  http_middleware.negotiation:
    class: Drupal\Core\StackMiddleware\NegotiationMiddleware
    tags:
+5 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\Core\Ajax;

use Drupal\Component\Utility\UrlHelper;

/**
 * AJAX command for adjusting Drupal's JavaScript settings.
 *
@@ -53,6 +55,9 @@ public function __construct(array $settings, $merge = FALSE) {
   * Implements Drupal\Core\Ajax\CommandInterface:render().
   */
  public function render() {
    if (isset($this->settings['ajax_page_state']['libraries'])) {
      $this->settings['ajax_page_state']['libraries'] = UrlHelper::compressQueryParameter($this->settings['ajax_page_state']['libraries']);
    }

    return [
      'command' => 'settings',
+6 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
@@ -337,6 +338,11 @@ public function getJsAssets(AttachedAssetsInterface $assets, $optimize, Language
      // Update the $assets object accordingly, so that it reflects the final
      // settings.
      $assets->setSettings($settings);
      // Convert ajaxPageState to a compressed string from an array, since it is
      // used by ajax.js to pass to AJAX requests as a query parameter.
      if (isset($settings['ajaxPageState']['libraries'])) {
        $settings['ajaxPageState']['libraries'] = UrlHelper::compressQueryParameter($settings['ajaxPageState']['libraries']);
      }
      $settings_as_inline_javascript = [
        'type' => 'setting',
        'group' => JS_SETTING,
+54 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\StackMiddleware;

use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Expands the compressed ajax_page_state query parameter into an array.
 */
class AjaxPageState implements HttpKernelInterface {

  /**
   * Constructs a new AjaxPageState instance.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   The wrapped HTTP kernel.
   */
  public function __construct(protected readonly HttpKernelInterface $httpKernel) {
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
    if ($type === static::MAIN_REQUEST) {
      if ($request->request->has('ajax_page_state')) {
        $request->request->set('ajax_page_state', $this->parseAjaxPageState($request->request->all('ajax_page_state')));
      }
      elseif ($request->query->has('ajax_page_state')) {
        $request->query->set('ajax_page_state', $this->parseAjaxPageState($request->query->all('ajax_page_state')));
      }
    }
    return $this->httpKernel->handle($request, $type, $catch);
  }

  /**
   * Parse the ajax_page_state variable in the request.
   *
   * Decompresses the libraries array key.
   *
   * @param array $ajax_page_state
   *   An array of query parameters, where the libraries parameter is compressed.
   *
   * @return array
   */
  private function parseAjaxPageState(array $ajax_page_state): array {
    $ajax_page_state['libraries'] = UrlHelper::uncompressQueryParameter($ajax_page_state['libraries']);
    return $ajax_page_state;
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -835,6 +835,7 @@

    // Allow Drupal to return new JavaScript and CSS files to load without
    // returning the ones already loaded.
    // @see \Drupal\Core\StackMiddleWare\AjaxPageState
    // @see \Drupal\Core\Theme\AjaxBasePageNegotiator
    // @see \Drupal\Core\Asset\LibraryDependencyResolverInterface::getMinimalRepresentativeSubset()
    // @see system_js_settings_alter()
Loading