class AjaxResponseSubscriber in Views Infinite Scroll 8
Response subscriber to handle AJAX responses.
Hierarchy
- class \Drupal\views_infinite_scroll\EventSubscriber\AjaxResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AjaxResponseSubscriber
1 string reference to 'AjaxResponseSubscriber'
1 service uses AjaxResponseSubscriber
File
- src/
EventSubscriber/ AjaxResponseSubscriber.php, line 13
Namespace
Drupal\views_infinite_scroll\EventSubscriberView source
class AjaxResponseSubscriber implements EventSubscriberInterface {
/**
* Alter the views AJAX response commands only for the infinite pager.
*
* @param array $commands
* An array of commands to alter.
*/
protected function alterPaginationCommands(array &$commands) {
foreach ($commands as $delta => &$command) {
// Substitute the 'replace' method with our custom jQuery method which
// will allow views content to be injected one after the other.
if (isset($command['method']) && $command['method'] === 'replaceWith') {
$command['method'] = 'infiniteScrollInsertView';
}
// Stop the view from scrolling to the top of the page.
if ($command['command'] === 'viewsScrollTop') {
unset($commands[$delta]);
}
}
}
/**
* 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();
// Only alter commands if the user has selected our pager and it attempting
// to move beyond page 0.
if ($view
->getPager()
->getPluginId() !== 'infinite_scroll' || $event
->getRequest()->query
->get('page') == 0) {
// When the current page is 0 it might be the case that there where no
// additional items in this case we want to still append the empty result.
return;
}
$commands =& $response
->getCommands();
$this
->alterPaginationCommands($commands);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::RESPONSE => [
[
'onResponse',
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxResponseSubscriber:: |
protected | function | Alter the views AJAX response commands only for the infinite pager. | |
AjaxResponseSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AjaxResponseSubscriber:: |
public | function | Renders the ajax commands right before preparing the result. |