When a field accepts multiple values, the admin form adds a button "Add another item". Mousedown submits a POST to /system/ajax to get another field (i'm using multifield here).

I'd like to hook into the javascript completed event to do some custom handling on those additional fields.

enter image description here

How can I most easily hook these ajax calls for javascript callback and reference to the block being added? Is it possible without PHP?

  • 1
    Not sure if Drupal raises an event, but if not you could always use $( document ).ajaxComplete(function( event, xhr, settings ) { .. }
    – Clive
    Mar 9 '16 at 0:29
  • @Clive, is there a better way to ID the original button clicked? I'm basically parsing the response for a string i'm familiar with in this block. Then doing a bunch more to check for attributes i'll manage as HTML is added and removed:
    – E.A.T
    Mar 9 '16 at 6:54
  • $(document).ajaxComplete(function(event,xhr,settings){ if (settings.url.indexOf("/system/ajax") > -1) { var data = xhr.responseText; if (data) { data = JSON.parse(data); if (data.length > 0 && typeof data[1].data != 'undefined') { data = data[1].data; if (data.indexOf('field-search-radius') > -1) { console.log('i found you!'); } } } } });
    – E.A.T
    Mar 9 '16 at 6:54
  • Client-side only? I don't think there is - you'd need to hook into the form ajax in PHP
    – Clive
    Mar 9 '16 at 8:48

You can use hook_menu_alter and define your own custom callback function like below.

function demo_pane_menu_alter(&$items) {
  $items['system/ajax']['page callback'] = 'demo_pane_taxonomy_autocomplete';
  • Won't this break all Ajax operations?
    – Kevin
    Mar 9 '16 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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