You are here

class VLMEventSubscriber in Views Load More 2.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/VLMEventSubscriber.php \Drupal\views_load_more\EventSubscriber\VLMEventSubscriber

Hierarchy

  • class \Drupal\views_load_more\EventSubscriber\VLMEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of VLMEventSubscriber

1 string reference to 'VLMEventSubscriber'
views_load_more.services.yml in ./views_load_more.services.yml
views_load_more.services.yml
1 service uses VLMEventSubscriber
views_load_more.event_subscriber in ./views_load_more.services.yml
Drupal\views_load_more\EventSubscriber\VLMEventSubscriber

File

src/EventSubscriber/VLMEventSubscriber.php, line 12

Namespace

Drupal\views_load_more\EventSubscriber
View source
class VLMEventSubscriber implements EventSubscriberInterface {

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

  /**
   * Filter a views AJAX response when the Load More pager is set.  Remove the
   * scrollTop commane and add in a viewsLoadMoreAppend AJAX command.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if ($response instanceof ViewAjaxResponse) {
      $view = $response
        ->getView();
      $pagerPlugin = $view
        ->getPager();
      if ($pagerPlugin
        ->getPluginId() == 'load_more' && $pagerPlugin
        ->getCurrentPage() > 0) {
        $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'] == '.js-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'] = "> div > {$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)));
            }
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VLMEventSubscriber::getSubscribedEvents public static function
VLMEventSubscriber::onResponse public function Filter a views AJAX response when the Load More pager is set. Remove the scrollTop commane and add in a viewsLoadMoreAppend AJAX command.