VLMEventSubscriber.php in Views Load More 8
File
src/EventSubscriber/VLMEventSubscriber.php
View source
<?php
namespace Drupal\views_load_more\EventSubscriber;
use Drupal\views\Ajax\ViewAjaxResponse;
use Drupal\views_load_more\Ajax\VLMAppendCommand;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class VLMEventSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array(
'onResponse',
0,
);
return $events;
}
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) {
if ($command['command'] == 'viewsScrollTop') {
unset($commands[$key]);
}
else {
if ($command['command'] == 'insert' && $command['selector'] == '.view-dom-id-' . $view->dom_id) {
$stylePlugin = $view
->getStyle();
$content = $commands[$key]['data'];
$cmd_options = array(
'wrapper_selector' => $commands[$key]['selector'],
'content_selector' => $pagerPlugin->options['advanced']['content_selector'],
'pager_selector' => $pagerPlugin->options['advanced']['pager_selector'],
'effect' => $pagerPlugin->options['effects']['type'],
'speed' => $pagerPlugin->options['effects']['speed'],
);
unset($commands[$key]);
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)));
}
}
}
}
}
}
}