0

I have a view with an exposed form with ajax enabled (is is media_library)

I click apply filters.

An ajax request is triggered and ViewAjaxController->ajaxView() runs.

This method has a comment that says "Reuse the same DOM id so it matches that in drupalSettings." so the same DOM id is reused.

ViewExecutable->preExecute() runs where the DOM ID is generated if not already set (which it is)

The response comes back with settings and an insert command. First the settings are merged.

Now the insert command runs. As the method is "html" Drupal.detachBehaviors() is called which in turn calls Drupal.behaviors.ViewsAjaxView.detach() which as the view DOM id exists in the DOM it deletes the settings for that ajax view.

Now Drupal.attachBehaviors() runs and calls Drupal.behaviors.ViewsAjaxView.attach()

This method doesn't do anything as the settings have been deleted.


After this the new view exposed form is on the page, but none of the behaviors have been bound so any further interaction with it will just break the page.

One solution:

In ViewExecutable->preExecute() just before the dom_id property is set the hook views_pre_view is invoked.

I can implement this hook and always just set a new DOM ID for the media library view:

/**
 * Implements hook_views_pre_view().
 */
function HOOK_views_pre_view(ViewExecutable $view): void {
  if ($view->id() === 'media_library') {
    $view->dom_id = hash('sha256', $view->storage->id() . \Drupal::time()->getRequestTime() . mt_rand());
  }
}

This means a fresh DOM is is always returned so the old settings are deleted but the new settings are still available when attachBehaviors runs.

My question is why is this necessary? Is this a bug in views?

I can't reproduce this in D10, I'm running Drupal 9.5.11

I've reviewed all patches applied and can't see anything that would affect this process.

I've also search for all implementations of hook_views_pre_view in my code base and again can't see anything that would affect this process.

0

Your Answer

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

Browse other questions tagged or ask your own question.