4

ajax['path']

Description: This is the Drupal menu path for a callback function which will generate HTML and return the string of HTML to Drupal. The result will replace the div specified in #ajax['wrapper']. This property is infrequently used in Drupal 7 and higher, because it is set automatically if using the #ajax['callback'] property.

However, I can't still why #ajax['path'] function offers more than #ajax['callback'].

When should I use which one?

4
  • In your question you claim 'path' offers more than 'callback' - care to tell why? Both can replace any DOM item of a form, simply 'path' offers way less automation. Is there any practical problem behind your question?
    – Mołot
    Jul 10, 2013 at 11:16
  • Maybe in another words, which method is better to use, except that callback needs less code to setup?
    – deem
    Jul 10, 2013 at 11:18
  • There is no 'better' per se, but as documentation says, in 7 path is rarely used. If you want your AJAX calls to be automated, use callback. If you want them more lightweight, use path. If in doubt, use callback. But that's in no way definitive answer, just my own rule of thumb.
    – Mołot
    Jul 10, 2013 at 11:20
  • The automated callback doesn't appear to handle a situation such as the callback being called on the second page of a form with a pager. I'm still figuring it out, but I think the callback loses the URL parameters.
    – jgreep
    May 10, 2016 at 13:20

1 Answer 1

4

It depends on your need.

From Form API Reference,

#ajax['callback'] and #ajax['path'] are mutually exclusive. 'callback' is easier to use and requires less code setup, but may not provide the capabilities required for some functions. When you can, use 'callback'.

In simple words the function specified in #ajax['callback'] generates the content and #ajax['path'] controls the execution of that callback.

#ajax['path'] is defaults to system/ajax which fetches the respective form from the cache and process it then calls the specified callback for fetching the new content.

However if you want alter this flow, instead of system/ajax give your custom callback(But you will end-up with writing another AJAX system).

For example Views module uses its own path( views/ajax) instead of system/ajax. The reason is that Views doesn't want the cached form so its re-builds it again(However there is a issue going on with regards to this approach).

So just set the #ajax['callback'] alone to have AJAX Form.

Your Answer

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

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