Unverified Commit 8b522adc authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3548212 by nod_, godotislate, mstrelan: Remove dependency on...

Issue #3548212 by nod_, godotislate, mstrelan: Remove dependency on core/drupal.ajax from contextual module
parent cfcb78ce
Loading
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ drupal.contextual-links:
  dependencies:
    - core/jquery
    - core/drupal
    - core/drupal.ajax
    - core/drupalSettings
    - core/once
    - core/drupal.touchevents-test
+17 −11
Original line number Diff line number Diff line
@@ -180,13 +180,19 @@
      // Perform an AJAX request to let the server render the contextual links
      // for each of the placeholders.
      if (uncachedIDs.length > 0) {
        $.ajax({
          url: Drupal.url('contextual/render'),
          type: 'POST',
          data: { 'ids[]': uncachedIDs, 'tokens[]': uncachedTokens },
          dataType: 'json',
          success(results) {
            Object.entries(results).forEach(([contextualID, html]) => {
        const data = new URLSearchParams();
        uncachedIDs.forEach((id) => data.append('ids[]', id));
        uncachedTokens.forEach((id) => data.append('tokens[]', id));

        const req = new Request(Drupal.url('contextual/render'), {
          method: 'POST',
          body: data,
        });

        fetch(req)
          .then((res) => res.json())
          .then((json) =>
            Object.entries(json).forEach(([contextualID, html]) => {
              // Store the metadata.
              storage.setItem(`Drupal.contextual.${contextualID}`, html);
              // If the rendered contextual links are empty, then the current
@@ -207,9 +213,9 @@
                  initContextual($placeholders.eq(i), html);
                }
              }
            });
          },
        });
            }),
          )
          .catch((error) => console.warn(error));
      }
    },
  };
@@ -532,6 +538,6 @@
   * @listens event:drupalContextualLinkAdded
   */
  $(document).on('drupalContextualLinkAdded', (event, data) => {
    Drupal.ajax.bindAjaxLinks(data.$el[0]);
    Drupal.ajax?.bindAjaxLinks(data.$el[0]);
  });
})(jQuery, Drupal, drupalSettings, window.JSON, window.sessionStorage);
+7 −0
Original line number Diff line number Diff line
@@ -76,7 +76,10 @@ public static function preRenderLinks(array $element) {

    // Transform contextual links into parameters suitable for links.html.twig.
    $links = [];
    $use_ajax = FALSE;
    foreach ($items as $class => $item) {
      // Check whether any of the contextual links have the use-ajax class.
      $use_ajax = $use_ajax || in_array('use-ajax', $item['localized_options']['class'] ?? [], TRUE);
      $class = Html::getClass($class);
      $links[$class] = [
        'title' => $item['title'],
@@ -84,6 +87,10 @@ public static function preRenderLinks(array $element) {
      ];
    }
    $element['#links'] = $links;
    if ($use_ajax) {
      // Add AJAX library if any of the links need it.
      $element['#attached']['library'][] = 'core/drupal.ajax';
    }

    // Allow modules to alter the renderable contextual links element.
    static::moduleHandler()->alter('contextual_links_view', $element, $items);
+2 −2
Original line number Diff line number Diff line
@@ -94,9 +94,9 @@ public function testLogin(): void {
      'CacheTagInvalidationCount' => 0,
      'CacheTagLookupQueryCount' => 13,
      'ScriptCount' => 3,
      'ScriptBytes' => 167569,
      'ScriptBytes' => 140083,
      'StylesheetCount' => 2,
      'StylesheetBytes' => 45450,
      'StylesheetBytes' => 44024,
    ];
    $this->assertMetrics($expected, $performance_data);

+9 −0
Original line number Diff line number Diff line
@@ -20,4 +20,13 @@ window.drupalCumulativeXhrCount = 0;
    htmx.on('htmx:beforeSend', increment);
    htmx.on('htmx:afterRequest', decrement);
  }
  // Catch calls to native fetch().
  const oldFetch = window.fetch;
  window.fetch = function newFetch(resource, options) {
    increment();
    return oldFetch(resource, options).then((res) => {
      decrement();
      return res;
    });
  };
})(jQuery, window.htmx);
Loading