Score:0

Rerun behaviors

cn flag

In order to output the SearchApiAutoCompleteFormBlock (view) within a modal, we lost the bound JavaScript events on the input. In other words, the DOM element (<div>) for the block was recreated by JavaScript after Drupal adds the events.

Now we need to rebind the events for the autocomplete.

This is the behavior of the Search API autocomplete module.

Drupal.behaviors.searchApiAutocomplete = {
attach: function (context, settings) {
  // Find all our fields with autocomplete settings.
  $(context)
    .find('.ui-autocomplete-input[data-search-api-autocomplete-search]')
    .once('search-api-autocomplete')
    .each(function () {
      var uiAutocomplete = $(this).data('ui-autocomplete');
      if (!uiAutocomplete) {
        return;
      }
      var $element = uiAutocomplete.menu.element;
      $element.addClass('search-api-autocomplete-search');
      var elementSettings = autocomplete.getSettings(this, settings);
      if (elementSettings['delay']) {
        uiAutocomplete.options['delay'] = elementSettings['delay'];
      }
      if (elementSettings['min_length']) {
        uiAutocomplete.options['minLength'] = elementSettings['min_length'];
      }
      // Override the "select" callback of the jQuery UI autocomplete.
      var oldSelect = uiAutocomplete.options.select;
      uiAutocomplete.options.select = function (event, ui) {
        // If this is a URL suggestion, instead of autocompleting we
        // redirect the user to that URL.
        if (ui.item.url) {
          location.href = ui.item.url;
          return false;
        }

        var ret = oldSelect.apply(this, arguments);

        // If auto-submit is enabled, submit the form.
        if (elementSettings['auto_submit'] && elementSettings['selector']) {
          $(elementSettings['selector'], this.form).trigger('click');
        }

        return ret;
      };
    });
}

I would expect there is some kind of mechanism to re-register view block contexts. I have no idea if this can be done easily. Any help is appreciated.

cn flag
Might be misunderstanding, but if you've changed the DOM and want to re-bind behaviors, you normally just run `Drupal.attachBehaviors()`
mogio avatar
cn flag
Thanks Clive. I tried "Drupal.attachBehaviors()" before - it was not working due to a totally non dependent issue. But your post helped as I rechecked the whole script and was able to find the issue.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.