You are here

class AjaxResponseSubscriber in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/AjaxResponseSubscriber.php \Drupal\geolocation\EventSubscriber\AjaxResponseSubscriber
  2. 8.2 src/EventSubscriber/AjaxResponseSubscriber.php \Drupal\geolocation\EventSubscriber\AjaxResponseSubscriber

Response subscriber to handle AJAX responses.

Hierarchy

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

Expanded class hierarchy of AjaxResponseSubscriber

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

File

src/EventSubscriber/AjaxResponseSubscriber.php, line 13

Namespace

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

  /**
   * Alter the views AJAX response commands only for the map.
   *
   * @param array $commands
   *   An array of commands to alter.
   */
  protected function alterCommands(array &$commands) {
    foreach ($commands as $delta => &$command) {

      // Substitute the 'replace' method without our custom jQuery method which
      // will allow views content to be injected one after the other.
      if (isset($command['method']) && $command['method'] === 'replaceWith' && isset($command['selector']) && substr($command['selector'], 0, 16) === '.js-view-dom-id-') {
        $command['command'] = 'geolocationCommonMapsUpdate';
        unset($command['method']);
      }
    }
  }

  /**
   * Renders the ajax commands right before preparing the result.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The response event, which contains the possible AjaxResponse object.
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();

    // Only alter views ajax responses.
    if (!$response instanceof ViewAjaxResponse) {
      return;
    }
    $view = $response
      ->getView();
    if ($view
      ->getStyle()
      ->getPluginId() !== 'maps_common') {

      // This view is not of maps_common style, but maybe an attachment is.
      $common_map_attachment = FALSE;
      $attached_display_ids = $view->display_handler
        ->getAttachedDisplays();
      foreach ($attached_display_ids as $display_id) {
        $current_display = $view->displayHandlers
          ->get($display_id);
        if (!empty($current_display)) {
          if (!empty($current_display
            ->getOption('style')['type']) && $current_display
            ->getOption('style')['type'] == 'maps_common') {
            $common_map_attachment = TRUE;
          }
        }
      }
      if (!$common_map_attachment) {
        return;
      }
    }
    $page_change = $event
      ->getRequest()->query
      ->get('page', FALSE);
    $commands =& $response
      ->getCommands();
    foreach ($commands as $delta => &$command) {

      // Substitute the 'replace' method without our custom jQuery method which
      // will allow views content to be injected one after the other.
      if (isset($command['method']) && $command['method'] === 'replaceWith' && isset($command['selector']) && substr($command['selector'], 0, 16) === '.js-view-dom-id-') {
        $command['command'] = 'geolocationCommonMapsUpdate';
        unset($command['method']);
      }

      // Stop the view from scrolling to the top of the page.
      if ($page_change === FALSE && $command['command'] === 'viewsScrollTop') {
        unset($commands[$delta]);
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxResponseSubscriber::alterCommands protected function Alter the views AJAX response commands only for the map.
AjaxResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AjaxResponseSubscriber::onResponse public function Renders the ajax commands right before preparing the result.