You are here

public function VLMEventSubscriber::onResponse in Views Load More 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/VLMEventSubscriber.php \Drupal\views_load_more\EventSubscriber\VLMEventSubscriber::onResponse()

Filter a views AJAX response when the Load More pager is set. Remove the scrollTop commane and add in a viewsLoadMoreAppend AJAX command.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event:

File

src/EventSubscriber/VLMEventSubscriber.php, line 33
Contains \Drupal\views_load_more\EventSubscriber\VLMEventSubscriber

Class

VLMEventSubscriber

Namespace

Drupal\views_load_more\EventSubscriber

Code

public function onResponse(FilterResponseEvent $event) {
  $response = $event
    ->getResponse();
  if ($response instanceof ViewAjaxResponse) {
    $view = $response
      ->getView();
    $pagerPlugin = $view
      ->getPager();
    if ($pagerPlugin
      ->getPluginId() == 'load_more') {
      $commands =& $response
        ->getCommands();
      foreach ($commands as $key => $command) {

        // Remove 'viewsScrollTop' command, as this behavior is unnecessary.
        if ($command['command'] == 'viewsScrollTop') {
          unset($commands[$key]);
        }
        else {
          if ($command['command'] == 'insert' && $command['selector'] == '.view-dom-id-' . $view->dom_id) {
            $stylePlugin = $view
              ->getStyle();

            // Take the data attribute, which is the content of the view,
            // otherwise discard the insert command for the view, we're
            // replacing it with a VLMAppendCommand
            $content = $commands[$key]['data'];
            $cmd_options = array(
              'wrapper_selector' => $commands[$key]['selector'],
              // Changes to the content and pager selectors, if any required by
              // theme.
              'content_selector' => $pagerPlugin->options['advanced']['content_selector'],
              'pager_selector' => $pagerPlugin->options['advanced']['pager_selector'],
              // Animation effects
              'effect' => $pagerPlugin->options['effects']['type'],
              'speed' => $pagerPlugin->options['effects']['speed'],
            );
            unset($commands[$key]);

            // Special case for lists and tables
            if ($stylePlugin
              ->getPluginId() == 'html_list' && in_array($stylePlugin->options['type'], array(
              'ul',
              'ol',
            ))) {
              if (empty($stylePlugin->options['wrapper_class'])) {
                $cmd_options['target_list'] = "> {$stylePlugin->options['type']}:not(.links)";
              }
              else {
                $wrapper_classes = explode(' ', $stylePlugin->options['wrapper_class']);
                $wrapper_classes = implode('.', $wrapper_classes);
                $cmd_options['target_list'] = ".{$wrapper_classes} > {$stylePlugin->options['type']}:not(.links)";
              }
            }
            else {
              if ($stylePlugin
                ->getPluginId() == 'table') {
                $cmd_options['target_list'] = '.views-table tbody';
              }
            }
            $response
              ->addCommand(new VLMAppendCommand($content, array_filter($cmd_options)));
          }
        }
      }
    }
  }
}