Creating ajax modal from event Here's the deal. I want to create a modal popup when an add to cart event is triggered.

I've got the event working just fine but can't seem to get the ajax to work properly. What I'm working off of was using the magnific popup library and was creating the popup using \Drupal::messenger()-&gt;addMessage($content, NULL); where content was a render array and the theme has

{{ attach_library('commerce_add_to_cart_confirmation/confirmation_overlay_css') }}
{{ attach_library('commerce_add_to_cart_confirmation/magnific_modal') }}


right at the top.

I thought this was very weird and funky. After some searching I found that you could create an ajax response like :

  $content = "Something to show in the modal";
  $title = "Hi, I'm a Modal";
  $response = new AjaxResponse();
  $response-&gt;addCommand(new OpenModalDialogCommand($title, $content, ['width' =&gt; '700']));
  return $response;


So I started doing this instead but it wouldn't work. I was told that nothing was handling that return since this function was just called because of an event. That made sense.

I then found this which seemed to be doing nearly the same as me.
I modified my code to use the same method.
No dialog box. :( I used xdebug and saw that there was no ajax response coming from the kernel::response and thus my function kept returning no response.

I then thought that it could be that ajax isn't loaded. I used the chrome network dev tool and found no ajax.dialog there. I tried adding this to my .module file :

function commerce_add_to_cart_confirmation_page_attachments(array &amp;$attachments) {
  $attachments['#attached']['library'][] = 'core/drupal.dialog.ajax';
}


Then I saw ajax.dialog in the network but still no popup on my site.
The module linked above uses a hook to some commerce form (I can't tell which because the name is so cluttered) and wonder if that makes a difference.

How can I create a modal popup from a CartEvents::CART_ENTITY_ADD event subscriber? Am I missing something to do with the libraries or something wrong with my php?
How can I create a modal popup from a CartEvents::CART_ENTITY_ADD
  event subscriber? Am I missing something to do with the libraries or
  something wrong with my php?


You can't return a response from any event, this is only possible from specific kernel events by setting the response in the event. But the example you have linked should work in your case. It splits the task into two event subscribers:

  public static function getSubscribedEvents() {
    return [
      CartEvents::CART_ENTITY_ADD =&gt; 'onAddToCart',
      KernelEvents::RESPONSE =&gt; 'onResponse',
    ];
  }


which are in the same service class, so the second event can use the variable the first one has set:

  public function onAddToCart(CartEntityAddEvent $event) {
    $this-&gt;purchasedEntity = $event-&gt;getEntity();
  }

  public function onResponse(FilterResponseEvent $event) {
    $response = $event-&gt;getResponse();

    // We only care if this happened after an entity was added to the cart.
    if (!$this-&gt;purchasedEntity) {
      return;

    ...

    $event-&gt;setResponse($response);
  }