AjaxResponseSubscriber.php in Geolocation Field 8
File
src/EventSubscriber/AjaxResponseSubscriber.php
View source
<?php
namespace Drupal\geolocation\EventSubscriber;
use Drupal\views\Ajax\ViewAjaxResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class AjaxResponseSubscriber implements EventSubscriberInterface {
protected function alterCommands(array &$commands) {
foreach ($commands as $delta => &$command) {
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']);
}
}
}
public function onResponse(FilterResponseEvent $event) {
$response = $event
->getResponse();
if (!$response instanceof ViewAjaxResponse) {
return;
}
$view = $response
->getView();
if ($view
->getStyle()
->getPluginId() !== 'maps_common') {
$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) {
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']);
}
if ($page_change === FALSE && $command['command'] === 'viewsScrollTop') {
unset($commands[$delta]);
}
}
}
public static function getSubscribedEvents() {
return [
KernelEvents::RESPONSE => [
[
'onResponse',
],
],
];
}
}