Allow AJAX links to replace a specific selector

Created on 17 January 2019, almost 6 years ago
Updated 24 January 2023, almost 2 years ago

Problem/Motivation

This is a follow-up of #3020716: Add vertical tabs style menu to media library . The current AJAX system supports opening dialogs by adding the use-ajax class to a link. In the media library, once you are in the library, we have some links that need to replace parts of the dialog instead of close the current dialog and open a new one (which is an accessibility issue, see #3020716-18: Add vertical tabs style menu to media library ).

The AJAX system supports replacing selectors with AJAX content, but to do that you currently have to do a custom implementation in JS. It would be great if the AJAX system provides an easier way to do this.

Proposed resolution

Add more flexibility to the current AJAX system for the use-ajax class OR add a new class for this new use-case.

The following things would be nice:
- Add a class to a link so the AJAX behavior is automatically attached to the link.
- The href of the link will be used to fetch the new content.
- A data attribute to specify the selector that needs to be replaced.
- A data attribute to specify where the focus needs to go afterwards.
- A data attribute to specify the throbber.

Remaining tasks

Discuss how to implement this.
Write a patch.

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Needs work

Version

10.1

Component
Ajax 

Last updated 1 day ago

Created by

🇳🇱Netherlands seanB Netherlands

Live updates comments and jobs are added and updated live.
  • Needs frontend framework manager review

    Used to alert the fron-tend framework manager core committer(s) that a front-end focused issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • 🇫🇷France prudloff Lille

    Here is a reroll for 9.5.

  • 🇮🇳India _utsavsharma

    Patch for 10.1.x.

  • 🇧🇬Bulgaria ovanes Sofia

    Hello adding adjusted patch for core 9.5.x

  • 🇨🇦Canada b_sharpe

    There's some issues here with focus with:

            if (this.focus) {
              target = document.querySelector(this.focus);
            } else {
    

    Seeing the following error in the console:

    An error occurred during the execution of the Ajax response: SyntaxError: Document.querySelector: 'function focus() {
        [native code]
    }' is not a valid selector
    

    because in the context here, "this" is actually the Window object, so focus is a function.

  • 🇲🇦Morocco m.bkm

    I have applied patch #64 to my Drupal 9.5.3 and I’m having the same issue and getting the same error message. Anyone have any ideas on how to fix this ?

    An error occurred during the execution of the Ajax response: SyntaxError: Failed to execute ‘querySelector’ on ‘Document’: ‘function focus() { [native code] }’ is not a valid selector.

  • 🇱🇻Latvia martins.bertins Rēzekne

    Adjusted patch for 10.1.x from #63.
    Found that progress was not being set correctly and there was a .orig file included.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update about 1 year ago
    29,723 pass
  • 🇺🇸United States tregonia

    Found out today that the patches do not apply against 10.2. I have been debugging and the apply failure is isolated to the form loop in ajax.js; or the logic that handles the focus.

    I have removed that section, and resolved the apply failure. I cannot speak to whether any further changes are necessary for other concerns. Please review.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 12 months ago
    Patch Failed to Apply
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 12 months ago
    Patch Failed to Apply
  • 🇺🇸United States tregonia

    Updated the diff because it was not applying.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 12 months ago
    25,790 pass, 1,823 fail
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 12 months ago
    Build Successful
  • 🇪🇸Spain akalam

    Rerolled #69 for 10.2

  • 🇪🇸Spain akalam

    The wrapper ajax setting is not being considered when replacing the content. That can causes issues when the wrapper is set to something different than the content region. Here is a patch fixing it. Basically if the wrapper exist on the content inside the ajax response, we reduce the content to just the "wrapper" element. If the wrapper element is not found, we keep the content as it is, replacing the wrapper with the content from the ajax response.

  • 🇪🇸Spain akalam

    Patch 73 was introducing a regression: In the context of an Ajax call, there are situations when the wrapper id is in the response, but we don't want to replace just the partial but the whole response. This is the case of the media library after adding a new media, because the div containing the media add form, has the same id as the form with the file upload at the top of the media list.
    As consequence, with the patch applied, when you add a new media through the media library, the dialog gets replaced with just the file upload form, so the auto-insert feature doesn't work (because the media list doesn't get replaced).

    As a solution, I'm proposing the replacement of partial response to be an opt-in (disabled by default). This setting can be activated through a data attribute (data-replace-partial="true") on specific links, so it can be used keeping the default behavior untouched.

  • 🇫🇷France prudloff Lille

    prudloff changed the visibility of the branch 9.3.x to hidden.

  • 🇫🇷France prudloff Lille

    prudloff changed the visibility of the branch 11.x to hidden.

  • 🇫🇷France prudloff Lille

    prudloff changed the visibility of the branch 3026636-allow-ajax-links to hidden.

  • Merge request !10505Apply patch from comment 74 → (Open) created by prudloff
  • Pipeline finished with Failed
    15 days ago
    Total: 257s
    #364438
Production build 0.71.5 2024