Ajax Callback Field Formatter
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 !