You are here

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'
views_infinite_scroll.services.yml in ./views_infinite_scroll.services.yml
views_infinite_scroll.services.yml
1 service uses AjaxResponseSubscriber
views_infinite_scroll.ajax_subscriber in ./views_infinite_scroll.services.yml
Drupal\views_infinite_scroll\EventSubscriber\AjaxResponseSubscriber

File

src/EventSubscriber/AjaxResponseSubscriber.php, line 13

Namespace

Drupal\views_infinite_scroll\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
AjaxResponseSubscriber::alterPaginationCommands protected function Alter the views AJAX response commands only for the infinite pager.
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.