Skip to content

[4.x]: Changing delimiter in CSV exporter does not effect the first row (headers) #14855

@mgambella

Description

@mgambella

What happened?

Description

I needed to change the delimiter for the CSV exporter from comma to semicolon, because it was messing up the data imports into Excel. I could find some reference to this issue by reading this file vendor/craftcms/cms/src/web/Response.php

Steps to reproduce

  1. create the file config/app.web.php with this content:
<?php

use craft\helpers\App;
use craft\helpers\ArrayHelper;
use craft\web\Response;

return [
    'components' => [
        'response' => fn() => Craft::createObject(ArrayHelper::merge(
            App::webResponseConfig(),
            [
                'defaultFormatters' => [
                    Response::FORMAT_CSV => [
                        'delimiter' => ';',
                    ],
                ],
            ]
        )),
    ],
];
  1. go to control panel to an element index and export data as CSV

Note: the issue could be related to the cms/src/web/CsvResponseFormatter.php line 108

if ($this->includeHeaderRow) {
    fputcsv($fp, $headers, ',');
}

instead, it should probably be:

if ($this->includeHeaderRow) {
    fputcsv($fp, $headers, $this->delimiter);
}

Expected behavior

Both the headers and data rows are delimited by the same delimiter char (a semicolon in my case).

Actual behavior

The header is still using a commas as delimiter.

Craft CMS version

4.8.10

PHP version

8.1

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions