View source
<?php
namespace Drupal\xls_serialization\Plugin\views\style;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rest\Plugin\views\style\Serializer;
class ExcelExport extends Serializer {
public function __construct(array $configuration, $plugin_id, $plugin_definition, $serializer, array $serializer_formats, array $serializer_format_providers) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer, $serializer_formats, $serializer_format_providers);
$this->formats = [
'xls',
'xlsx',
];
$this->formatProviders = [
'xls' => 'xls_serialization',
'xlsx' => 'xls_serialization',
];
}
public function defineOptions() {
$options = parent::defineOptions();
$options['xls_settings']['contains'] = [
'xls_format' => [
'default' => 'Excel2007',
],
];
$options['xls_settings']['metadata']['contains'] = [
'creator' => [
'default' => '',
],
'last_modified_by' => [
'default' => '',
],
'title' => [
'default' => '',
],
'description' => [
'default' => '',
],
'subject' => [
'default' => '',
],
'keywords' => [
'default' => '',
],
'category' => [
'default' => '',
],
'manager' => [
'default' => '',
],
'company' => [
'default' => '',
],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'style_options':
$form['formats']['#type'] = 'radios';
$form['formats']['#default_value'] = reset($this->options['formats']);
unset($form['formats']['#description']);
$xls_options = $this->options['xls_settings'];
$form['xls_settings'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('XLS(X) settings'),
'#tree' => TRUE,
'xls_format' => [
'#type' => 'select',
'#title' => $this
->t('Format'),
'#options' => [
'Excel2007' => $this
->t('Excel 2007'),
'Excel5' => $this
->t('Excel 5'),
],
'#default_value' => $xls_options['xls_format'],
],
];
$metadata = !empty($xls_options['metadata']) ? array_filter($xls_options['metadata']) : [];
$form['xls_settings']['metadata'] = [
'#type' => 'details',
'#title' => $this
->t('Document metadata'),
'#open' => $metadata,
];
$xls_fields = [
'creator' => $this
->t('Author/creator name'),
'last_modified_by' => $this
->t('Last modified by'),
'title' => $this
->t('Title'),
'description' => $this
->t('Description'),
'subject' => $this
->t('Subject'),
'keywords' => $this
->t('Keywords'),
'category' => $this
->t('Category'),
'manager' => $this
->t('Manager'),
'company' => $this
->t('Company'),
];
foreach ($xls_fields as $xls_field_key => $xls_field_title) {
$form['xls_settings']['metadata'][$xls_field_key] = [
'#type' => 'textfield',
'#title' => $xls_field_title,
];
if (isset($xls_options['metadata'][$xls_field_key])) {
$form['xls_settings']['metadata']['#default_value'] = $xls_options['metadata'][$xls_field_key];
}
}
break;
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$format = $form_state
->getValue([
'style_options',
'formats',
]);
$form_state
->setValue([
'style_options',
'formats',
], [
$format => $format,
]);
parent::submitOptionsForm($form, $form_state);
}
public function attachTo(array &$build, $display_id, Url $url, $title) {
$url_options = [];
$input = $this->view
->getExposedInput();
if ($input) {
$url_options['query'] = $input;
}
if ($pager = $this->view
->getPager()) {
$url_options['query']['page'] = $pager
->getCurrentPage();
}
$url_options['absolute'] = TRUE;
if (!empty($this->options['formats'])) {
$url_options['query']['_format'] = reset($this->options['formats']);
}
$url = $url
->setOptions($url_options)
->toString();
$type = $this->displayHandler
->getContentType();
$this->view->feedIcons[] = [
'#theme' => 'export_icon',
'#url' => $url,
'#type' => mb_strtoupper($type),
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'class' => [
Html::cleanCssIdentifier($type) . '-feed',
'views-data-export-feed',
],
],
],
],
'#attached' => [
'library' => [
'views_data_export/views_data_export',
],
],
];
$build['#attached']['html_head_link'][][] = [
'rel' => 'alternate',
'type' => $this->displayHandler
->getMimeType(),
'title' => $title,
'href' => $url,
];
}
}