IcalDisplay.php in Views iCal 8
File
src/Plugin/views/display/IcalDisplay.php
View source
<?php
namespace Drupal\views_ical\Plugin\views\display;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\Feed;
class IcalDisplay extends Feed {
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['page'] = [
'title' => $this
->t('iCal Settings'),
'column' => 'second',
'build' => [
'#weight' => -10,
],
];
$displays = array_filter($this
->getOption('displays'));
if (count($displays) > 1) {
$attach_to = $this
->t('Multiple displays');
}
elseif (count($displays) == 1) {
$display = array_shift($displays);
$displays = $this->view->storage
->get('display');
if (!empty($displays[$display])) {
$attach_to = $displays[$display]['display_title'];
}
}
if (!isset($attach_to)) {
$attach_to = $this
->t('None');
}
$options['displays'] = array(
'category' => 'path',
'title' => $this
->t('Attach to'),
'value' => $attach_to,
);
if ($this
->getOption('filename')) {
$options['path']['value'] .= $this
->t(' (@filename)', [
'@filename' => $this
->getOption('filename'),
]);
}
}
public function render() {
if (($response = $this->view
->getResponse()) && $this
->getOption('filename')) {
$response->headers
->set('Content-Disposition', 'attachment; filename="' . $this
->generateFilename($this
->getOption('filename')) . '"');
}
return parent::render();
}
protected function generateFilename($filename_pattern) {
return $this
->globalTokenReplace($filename_pattern);
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'path':
$form['filename'] = [
'#type' => 'textfield',
'#title' => $this
->t('Filename'),
'#default_value' => $this->options['filename'],
'#description' => $this
->t('The filename that will be suggested to the browser for downloading purposes. You may include replacement patterns from the list below.'),
];
$this
->globalTokenForm($form, $form_state);
break;
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
$section = $form_state
->get('section');
switch ($section) {
case 'path':
$this
->setOption('filename', $form_state
->getValue('filename'));
break;
}
}
}