CalendarHeader.php in Calendar 8.2
File
src/Plugin/views/area/CalendarHeader.php
View source
<?php
namespace Drupal\calendar\Plugin\views\area;
use Drupal\calendar\CalendarHelper;
use Drupal\calendar\Plugin\views\argument\CalendarYearWeekDate;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\area\TokenizeAreaPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CalendarHeader extends TokenizeAreaPluginBase implements ContainerFactoryPluginInterface {
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'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['tokenize']['default'] = TRUE;
$options['empty']['default'] = TRUE;
$options['content'] = [
'default' => '',
];
$options['pager_embed'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['content'] = [
'#title' => $this
->t('Heading'),
'#type' => 'textfield',
'#default_value' => $this->options['content'],
];
$form['pager_embed'] = [
'#title' => $this
->t('Use Pager'),
'#type' => 'checkbox',
'#default_value' => $this->options['pager_embed'],
];
}
public function render($empty = FALSE) {
if (!$empty || !empty($this->options['empty'])) {
$header_text = $date_argument = NULL;
$render = [];
$calendar_arguments = $this->calendarHelper
->getCalendarArguments($this->view);
foreach ($calendar_arguments as $date_argument) {
switch ($date_argument['id']) {
case 'calendar_day':
$date = new DrupalDateTime($date_argument['argument']);
$argument_text = $date
->format('F d Y');
$header_text = $this
->renderTextField($argument_text);
break;
case 'calendar_year_week':
$date = $this->calendarHelper
->weekInfo($date_argument['argument']);
$argument_text = $this
->t('Week') . ' ' . $date['weekno'] . ' ' . $this
->t('starting') . ' ' . $date['startweekdate']
->format('F d Y');
$header_text = $this
->renderTextField($argument_text);
break;
case 'calendar_year_month':
$date = new DrupalDateTime($date_argument['argument'] . '01');
$argument_text = $date
->format('F') . ' ' . $date
->format('Y');
$header_text = $this
->renderTextField($argument_text);
break;
case 'calendar_year':
break;
}
if (!$this->options['pager_embed']) {
$render = [
'#theme' => 'calendar_header',
'#title' => $header_text,
];
}
else {
if ($this->view->display_handler
->renderPager()) {
$exposed_input = isset($this->view->exposed_raw_input) ? $this->view->exposed_raw_input : NULL;
$render = $this->view
->renderPager($exposed_input);
$render['#exclude'] = FALSE;
$render['#items']['current'] = $header_text;
}
}
}
return $render;
}
return [];
}
public function renderTextField($value) {
if ($value) {
return $this
->sanitizeValue($this
->tokenizeValue($value), 'xss_admin');
}
return '';
}
}