Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

This module provides a generic JavaScript solution and a field formatter to easily integrate AJAX callbacks and commands with Drupal fields. It allows you to create custom AJAX responses in your controllers and display them within your content by configuring a link field.

Why re-implement all time the same thing ? That what I said to me when I started to work with Ajax Callback and Ajax Commands.
So instead of re-implements for each case a new JS and solution to allow me to display some content generated by Ajax Command, I decided to make a generic solution.

To work with Ajax Command we need 3 things :

  • The JS which will request and execute the Ajax Command
  • The Controller which build the Ajax Response and what to do or display
  • The connector to link the JS to fields.

So the JS and the connector can be generic. The controller not because the role of a Controller is to do specific stuffs.

Features

This module provide you :

  • A generic JS which request and execute an Ajax Request.
  • A Formatter which alter the field type Link to allow us to plug the Ajax on a field.

How it works ?

The Formatter will slightly alter your field Link by :

  • Transforming the render element form link (a) to span (span).
  • Adding 2 new attributes to the render element :
    • data-execute-ajax-callback-field-formatter-url : Filled with the URL from the field link
    • data-execute-ajax-callback-field-formatter-id : Build with the following pattern "[entity_type]--[entity_id]-[field_name]--[delta]" with :
      • entity_type : The type of the entity containing the field
      • entity_id : The ID of the entity containing the field
      • field_name : The field name.
      • delta : The element's delta.

Thanks to attributes placed by the formatter the JS can be call and performs Ajax Request.

Post-Installation

Once the module installed, you should find a new Formatter for field type Link. You'll just have to use it.
To do it you have to set your field link following this rules :

  • In the URL you'll pass the url to your controller which response an Ajax Response.
  • In text part you'll set the default value

And you just have to create a new controller which return Ajax Response.

Supporting this Module

If you want to support this module, use it, share it and feel free to suggest improvements !

Activity

Total releases
2
First release
Feb 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 2
Maintenance
Dormant

Releases

Version Type Release date
1.1.0 Stable Feb 10, 2025
1.1.x-dev Dev Feb 10, 2025