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()->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->addCommand(new OpenModalDialogCommand($title, $content, ['width' => '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 &$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?


1 Answer 1


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 => 'onAddToCart',
      KernelEvents::RESPONSE => '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->purchasedEntity = $event->getEntity();

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

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


  • Right, perhaps I wasn't too clear. I tried this method and it wouldn't work. I never got an ajax event so my function kept returning. In the linked module it also returns if the event is not an ajax event. When I looked at this with xdebug I found the event there was never an ajax one and thus no modal.
    – Derek C.
    Feb 28, 2020 at 1:41
  • Yes, when there is no Ajax event the code can't work. Ajax events are always triggered client-side. Check first in the F12 browser tools what kind of request the client is sending to Drupal.
    – 4uk4
    Feb 28, 2020 at 7:20
  • Well how do I get this to work then? Why is there no ajax being created?
    – Derek C.
    Feb 29, 2020 at 19:35
  • So I should hook into the commerce form and add #ajax to the submit button and that should do it? That creates an ajax response whenever the cart is submitted meaning I should be able to catch it with my code?
    – Derek C.
    Feb 29, 2020 at 23:04

Your Answer

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

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