Problem/Motivation

As of beta4 Drupal seems only to allow AJAX for most of the FormElements or the RenderElement of the type 'link'. Its usage via #ajax (form) or use-ajax (link) is well documented in theAPI. Even though 'link's are also treat as non-form element it probably only define a callback url (href) when using the 'use-ajax' class but does not have to any setting manipulation (e.g. method, event, etc.)

When investigating ajax.js i was unable to find any automated routine for AJAX usage in other elements execpt form elements. You can be pretty sure that only the FormElement type is defined. Looking at this issue will reveal that there has been heavy focus on getting this done for forms.

There seem to exist a RenderElement AJAX which has no documented usage and from my point of view it is not obvious how to use it at all.

At least there seem to be tests in the core/system module which implements the mention working ajax elements. The test are outdated and use deprecated features (see comments in the module files)

Workaround

Let's say you want to be able to use AJAX when you click on a specific div. Right now you would have to wrap an a Tag around this div like:

<a id="someLink" class="use-ajax" href="somepath/callback/linkcontroller">
<div class="fancy-icon"></div></a>

If you want to implement this in a render array it gets complicated. You'd either have to override the link template (or have another suggestion for it) or maybe define an inline template.

$variables['ajaxstuff'] = [
  '#type' => 'inline_template',
  '#template' => '<a id="someLink" class="use-ajax" href="{{ url }}">
    <div class="fancy-icon"></div></a>'
  '#context' => [
    'url' => Url::fromRoute('mymodule.ajaxcontroller'),
  ],
],

(There might be other ways for this i have no clue about like copying this routine.)

You could also try to attach JavaScript to your DOM but i could not come up with code which would work with eg a div tag.

(function($) {
"use strict"

// ajax.js example
Drupal.behaviors.myCustomAJAXStuff = {
  attach: function (context, settings) {
    Drupal.ajax['edit-submit'].commands.insert = function (ajax, response, status) {
      new_content = $(response.data);
      $('#my-wrapper').append(new_content);
      alert('New content was appended to #my-wrapper');
    }
  }
};
})(jQuery);

(The example above will trigger AjaxRequest>InsertCommand on and submit button on a form)

Proposed resolution

As i have no clue about the policies regarding ajax usage and implementation in D8 the problem above is a meager feature request.

If this is not possible to discuss for whatever reason i'd like to have an updated documentation for developers which clearly says what i am allowed to do and what not. More importantly what are the best practices for ajax usage with d8.

I also see the possibility that i don't know what is going on at all and someone would simply close this issue as 'won't fix' or 'works as designed'

Comments

steamx’s picture

Issue summary: View changes
steamx’s picture

joelpittet’s picture

Version: 8.0.0-beta4 » 8.0.x-dev
Issue tags: -needs commiter feedback +Documentation, +Needs subsystem maintainer review

From the issue summary it sounds like maybe needs documentation? We can add documentation in 8.0.x bug fix release if this is that.

Doesn't need committer review, just needs subsystem maintainer.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

seanB’s picture

#610068-40: Document AJAX no-js and use-ajax contains everything I know of the attributes that can be used on a link with the use-ajax class.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.