View source
<?php
namespace Drupal\views_data_export\Plugin\views\style;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Url;
use Drupal\rest\Plugin\views\style\Serializer;
class DataExport extends Serializer {
use RedirectDestinationTrait;
protected $defaultFieldLabels = TRUE;
public function defineOptions() {
$options = parent::defineOptions();
$options['csv_settings']['contains'] = [
'delimiter' => [
'default' => ',',
],
'enclosure' => [
'default' => '"',
],
'escape_char' => [
'default' => '\\',
],
'strip_tags' => [
'default' => TRUE,
],
'trim' => [
'default' => TRUE,
],
'encoding' => [
'default' => 'utf8',
],
'utf8_bom' => [
'default' => FALSE,
],
'use_serializer_encode_only' => [
'default' => FALSE,
],
];
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']);
$format_options = $this
->getFormatOptions();
if (in_array('csv', $format_options)) {
$csv_options = $this->options['csv_settings'];
$form['csv_settings'] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $this
->t('CSV settings'),
'#tree' => TRUE,
'#states' => [
'visible' => [
':input[name="style_options[formats]"]' => [
'value' => 'csv',
],
],
],
'delimiter' => [
'#type' => 'textfield',
'#title' => $this
->t('Delimiter'),
'#description' => $this
->t('Indicates the character used to delimit fields. Defaults to a comma (<code>,</code>). For tab-separation use <code>\\t</code> characters.'),
'#default_value' => $csv_options['delimiter'],
],
'enclosure' => [
'#type' => 'textfield',
'#title' => $this
->t('Enclosure'),
'#description' => $this
->t('Indicates the character used for field enclosure. Defaults to a double quote (<code>"</code>).'),
'#default_value' => $csv_options['enclosure'],
],
'escape_char' => [
'#type' => 'textfield',
'#title' => $this
->t('Escape character'),
'#description' => $this
->t('Indicates the character used for escaping. Defaults to a backslash (<code>\\</code>).'),
'#default_value' => $csv_options['escape_char'],
],
'strip_tags' => [
'#type' => 'checkbox',
'#title' => $this
->t('Strip HTML'),
'#description' => $this
->t('Strips HTML tags from CSV cell values.'),
'#default_value' => $csv_options['strip_tags'],
],
'trim' => [
'#type' => 'checkbox',
'#title' => $this
->t('Trim whitespace'),
'#description' => $this
->t('Trims whitespace from beginning and end of CSV cell values.'),
'#default_value' => $csv_options['trim'],
],
'encoding' => [
'#type' => 'radios',
'#title' => $this
->t('Encoding'),
'#description' => $this
->t('Determines the encoding used for CSV cell values.'),
'#options' => [
'utf8' => $this
->t('UTF-8'),
],
'#default_value' => $csv_options['encoding'],
],
'utf8_bom' => [
'#type' => 'checkbox',
'#title' => $this
->t('Include unicode signature (<a href="@bom" target="_blank">BOM</a>).', [
'@bom' => 'https://www.w3.org/International/questions/qa-byte-order-mark',
]),
'#default_value' => $csv_options['utf8_bom'],
],
'use_serializer_encode_only' => [
'#type' => 'checkbox',
'#title' => $this
->t('Only use Symfony serializer->encode method'),
'#description' => $this
->t('Skips the symfony data normalize method when rendering data export to increase performance on large datasets. <strong>(Only use when not exporting nested data)</strong>'),
'#default_value' => $csv_options['use_serializer_encode_only'],
],
];
}
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();
$format = $this->displayHandler
->getContentType();
$this->view->feedIcons[] = [
'#theme' => 'export_icon',
'#url' => $url,
'#format' => mb_strtoupper($format),
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'class' => [
Html::cleanCssIdentifier($format) . '-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,
];
}
public function buildSortPost() {
$query = $this->view
->getRequest()->query;
$sort_field = $query
->get('order');
if (empty($sort_field) || empty($this->view->field[$sort_field])) {
return;
}
$sort_order = strtolower($query
->get('sort'));
if (empty($sort_order) || $sort_order != 'asc' && $sort_order != 'desc') {
$sort_order = 'asc';
}
$this->view->field[$sort_field]
->clickSort($sort_order);
}
public function render() {
$rows = [];
foreach ($this->view->result as $row_index => $row) {
$this->view->row_index = $row_index;
$rows[] = $this->view->rowPlugin
->render($row);
}
unset($this->view->row_index);
$format = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
if ($format === 'csv' && $this->options['csv_settings']['use_serializer_encode_only'] == 1) {
return $this->serializer
->encode($rows, $format, [
'views_style_plugin' => $this,
]);
}
else {
return $this->serializer
->serialize($rows, $format, [
'views_style_plugin' => $this,
]);
}
}
}