class VboExportCsv in VBO export 8.3
Same name and namespace in other branches
- 8 src/Plugin/Action/VboExportCsv.php \Drupal\vbo_export\Plugin\Action\VboExportCsv
- 8.2 src/Plugin/Action/VboExportCsv.php \Drupal\vbo_export\Plugin\Action\VboExportCsv
Generates csv.
Plugin annotation
@Action(
id = "vbo_export_generate_csv_action",
label = @Translation("Generate csv from selected view results"),
type = ""
)
Hierarchy
- class \Drupal\vbo_export\Plugin\Action\VboExportBase extends \Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase implements ContainerFactoryPluginInterface
- class \Drupal\vbo_export\Plugin\Action\VboExportCsv
Expanded class hierarchy of VboExportCsv
File
- src/
Plugin/ Action/ VboExportCsv.php, line 16
Namespace
Drupal\vbo_export\Plugin\ActionView source
class VboExportCsv extends VboExportBase {
const EXTENSION = 'csv';
/**
* {@inheritdoc}
*
* Add csv separator setting to preliminary config.
*/
public function buildPreConfigurationForm(array $form, array $values, FormStateInterface $form_state) {
$form = parent::buildPreConfigurationForm($form, $values, $form_state);
$form['separator'] = [
'#title' => $this
->t('CSV separator'),
'#type' => 'radios',
'#options' => [
';' => $this
->t('semicolon ";"'),
',' => $this
->t('comma ","'),
'|' => $this
->t('pipe "|"'),
],
'#default_value' => isset($values['separator']) ? $values['separator'] : ';',
];
return $form;
}
/**
* Generate output string.
*/
protected function generateOutput() {
$config = $this->configuration;
$header = $this->context['sandbox']['header'];
$rows = $this
->getCurrentRows();
// Sanitize data.
foreach ($header as $key => $item) {
$header[$key] = strtr($item, [
$config['separator'] => ' ',
]);
}
$content_replacements = [
"\r\n" => ' ',
"\n\r" => ' ',
"\r" => ' ',
"\n" => ' ',
"\t" => ' ',
$config['separator'] => ' ',
];
// Generate output.
$csv_rows = [];
$csv_rows[] = implode($config['separator'], $header);
foreach ($rows as $row_index => $row) {
foreach ($row as $cell_key => $cell) {
$row[$cell_key] = strtr($cell, $content_replacements);
}
$csv_rows[] = implode($config['separator'], $row);
unset($rows[$row_index]);
}
$csv_string = implode(PHP_EOL, $csv_rows);
if (!empty($config['strip_tags'])) {
$csv_string = strip_tags($csv_string);
}
// BOM needs to be added to UTF-8 encoded csv file
// to make it easier to read by Excel.
$output = chr(0xef) . chr(0xbb) . chr(0xbf) . (string) $csv_string;
return $output;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
VboExportBase:: |
protected | property | The stream wrapper object. | |
VboExportBase:: |
protected | property | The tempstore object. | |
VboExportBase:: |
protected | property | The time service. | |
VboExportBase:: |
public | function | ||
VboExportBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
VboExportBase:: |
public | function | ||
VboExportBase:: |
public | function | Execute multiple handler. | |
VboExportBase:: |
protected | function | Gets Cache ID for current batch. | |
VboExportBase:: |
protected | function | Current rows to be processed. | |
VboExportBase:: |
protected | function | Prepares sandbox data (header and cache ID). | |
VboExportBase:: |
protected | function | Get rows from views results. | |
VboExportBase:: |
protected | function | Saves batch data into Private storage. | |
VboExportBase:: |
protected | function | Output generated string to file. Message user. | |
VboExportBase:: |
protected | function | Sets table header from view header. | |
VboExportBase:: |
public | function | ||
VboExportCsv:: |
public | function |
Add csv separator setting to preliminary config. Overrides VboExportBase:: |
|
VboExportCsv:: |
constant |
Overrides VboExportBase:: |
||
VboExportCsv:: |
protected | function |
Generate output string. Overrides VboExportBase:: |