View source
<?php
namespace Drupal\calendar\Plugin\views\pager;
use Drupal\calendar\CalendarHelper;
use Drupal\calendar\Plugin\views\area\CalendarHeader;
use Drupal\calendar\Plugin\views\argument\CalendarYearMonthDate;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\datetime\Plugin\views\argument\Date;
class CalendarPager extends PagerPluginBase implements ContainerFactoryPluginInterface {
const NEXT = '+';
const PREVIOUS = '-';
protected $argument;
protected $styleInfo;
protected $calendarHelper;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $calendarHelper) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->calendarHelper = $calendarHelper;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('calendar.helper'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->styleInfo =& $this->view->styleInfo;
}
public function render($input) {
$calendar_arguments = CalendarHelper::getCalendarArguments($this->view);
foreach ($calendar_arguments as $date_argument) {
if ($date_argument['argument']) {
$items['previous'] = [
'url' => $this
->getPagerUrl($date_argument, self::PREVIOUS, $input),
];
$items['next'] = [
'url' => $this
->getPagerUrl($date_argument, self::NEXT, $input),
];
}
}
return [
'#theme' => $this
->themeFunctions(),
'#items' => $items,
'#exclude' => $this->options['exclude_display'],
];
}
protected function getPagerUrl($date_argument, $mode, array $input) {
$format = reset($this->view->argument)
->getArgFormat();
if ($format == 'YW') {
$new = new \DateTime();
$year = (int) substr($date_argument['argument'], 0, 4);
$month = (int) substr($date_argument['argument'], 4, 2);
$new
->setISODate($year, $month);
}
else {
$new = \DateTime::createFromFormat($format, $date_argument['argument']);
}
$new
->modify($mode . '1 ' . $this->styleInfo
->getGranularity());
$value = $new
->format($format);
$current_position = 0;
$arg_vals = [];
foreach ($this->view->argument as $name => $argument) {
if (!$argument instanceof Date) {
$arg_vals["arg_{$current_position}"] = $argument
->getValue();
}
else {
$arg_vals["arg_{$current_position}"] = $value;
}
$current_position++;
}
return $this->view
->getUrl($arg_vals, $this->view->current_display);
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['exclude_display'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Exclude from Display'),
'#default_value' => $this->options['exclude_display'],
'#description' => $this
->t('Use this option if you only want to display the pager in Calendar Header area.'),
];
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['exclude_display'] = [
'default' => FALSE,
];
return $options;
}
}