5

How should I implement a callback to a single Drupal class (in my case it's Transliteration). I don't need a full Drupal load/bootstrap, but one independent class.

The purpose of the AJAX code would be the following:

  • User inputs 'áéíóű'
  • The input is sent via AJAX
  • The Transliteration class changes the input to 'aeiou'
  • The result is returned to the client

I've found this related issue, but it would be nice to have a simple example (and documentation) for it.

0

2 Answers 2

7

How should I implement a callback to a single Drupal class (in my case it's Transliteration). I don't need a full Drupal load/bootstrap just one independent class.

Taken literally, within the confines of Drupal, I don't believe you can. We rely on PSR-0 for class loading, so at the very least you must bootstrap Drupal to get the class loader running. (Though not necessarily a full bootstrap).

To truly just load a single class, you'd need a to call a different file than index.php where you'd hard code the class loading.

I haven't looked at how Ajax requests specifically have changed from D7 => D8, but thanks to D8's DI, only classes that are used in a single request will be instantiated, so the need to try to speed optimize things out, as you seem to want to do, should be lessened.

1
  • Nice, this really works. A simple AJAX request has a 25-35msec response time, instead of the front page response time of 80-100msec on my server. It's enough.
    – alippai
    Commented Feb 16, 2013 at 18:04
1

As far as for avoiding to fully bootstrap Drupal to use a callback implemented in a class, I think the answer is not different from the one you would get for Drupal 7: Boostrap Drupal using a custom PHP file.

The fact Drupal is not using anymore the cron.php file that is used in Drupal 7 leads me to think that fully bootstrapping Drupal is not a performance issue.
It is true that Drupal 8 still has its authorize.php, but that is necessary for a specific case which is not a case most of the modules would need to handle.

If you really need to do it, consider the differences that there are between the index.php file used from Drupal 7, and the same file used by Drupal 8.
Keep also in mind that, in Drupal 8, AJAX responses are handled with the AjaxResponse class, and soon the form system will be converted to use the new AJAX API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.