You are here

class AjaxResponseSubscriber in Layout Builder Modal 8

Provides an event subscriber that alters Ajax Responses.

Hierarchy

  • class \Drupal\layout_builder_modal\EventSubscriber\AjaxResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AjaxResponseSubscriber

1 string reference to 'AjaxResponseSubscriber'
layout_builder_modal.services.yml in ./layout_builder_modal.services.yml
layout_builder_modal.services.yml
1 service uses AjaxResponseSubscriber
layout_builder_modal.ajax_response_subscriber in ./layout_builder_modal.services.yml
Drupal\layout_builder_modal\EventSubscriber\AjaxResponseSubscriber

File

src/EventSubscriber/AjaxResponseSubscriber.php, line 14

Namespace

Drupal\layout_builder_modal\EventSubscriber
View source
class AjaxResponseSubscriber implements EventSubscriberInterface {

  /**
   * Close modal dialog if Layout Builder is re-rendered.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event.
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if ($response instanceof AjaxResponse) {
      $should_close_dialog = FALSE;
      $commands =& $response
        ->getCommands();
      foreach ($commands as $command) {
        if (isset($command['selector']) && $command['selector'] === '#layout-builder') {
          $should_close_dialog = TRUE;
          break;
        }
      }
      if ($should_close_dialog) {
        $response
          ->addCommand(new CloseDialogCommand('#layout-builder-modal'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::RESPONSE => [
        [
          'onResponse',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AjaxResponseSubscriber::onResponse public function Close modal dialog if Layout Builder is re-rendered.