Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response::$defaultFormatters #13541

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
- Added `craft\web\CpScreenResponseBehavior::$errorSummary`, `errorSummary()`, and `errorSummaryTemplate()`. ([#12125](https://github.com/craftcms/cms/pull/12125))
- Added `craft\web\CpScreenResponseBehavior::$pageSidebar`, `pageSidebar()`, and `pageSidebarTemplate()`. ([#13019](https://github.com/craftcms/cms/pull/13019), [#12795](https://github.com/craftcms/cms/issues/12795))
- Added `craft\web\CpScreenResponseBehavior::$slideoutBodyClass`.
- Added `craft\web\Response::$defaultFormatters`. ([#13541](https://github.com/craftcms/cms/pull/13541))
- `craft\helpers\Cp::selectizeFieldHtml()`, `selectizeHtml()`, and `_includes/forms/selectize.twig` now support a `multi` param. ([#13176](https://github.com/craftcms/cms/pull/13176))
- `craft\helpers\Typecast::properties()` now supports backed enum values. ([#13371](https://github.com/craftcms/cms/pull/13371))
- `craft\services\Assets::getRootFolderByVolumeId()` now ensures the root folder actually exists, and caches its results internally, improving performance. ([#13297](https://github.com/craftcms/cms/issues/13297))
Expand Down
47 changes: 42 additions & 5 deletions src/web/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\web;

use Craft;
use craft\helpers\ArrayHelper;
use craft\helpers\UrlHelper;
use Throwable;
use yii\base\Application as BaseApplication;
Expand All @@ -28,6 +29,38 @@ class Response extends \yii\web\Response
*/
public const FORMAT_CSV = 'csv';

/**
* Default response formatter configurations.
*
* This could be set from `config/app.web.php` to append additional default response formatters, or modify existing ones.
*
* ```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' => chr(9),
* ],
* ],
* ]
* )),
* ],
* ];
* ```
*
* @see defaultFormatters()
* @since 4.5.0
*/
public array $defaultFormatters = [];


/**
* @var bool whether the response has been prepared.
*/
Expand Down Expand Up @@ -294,11 +327,15 @@ public function sendAndClose(): void
*/
protected function defaultFormatters(): array
{
$formatters = parent::defaultFormatters();
$formatters[self::FORMAT_CSV] = [
'class' => CsvResponseFormatter::class,
];
return $formatters;
return ArrayHelper::merge(
parent::defaultFormatters(),
[
self::FORMAT_CSV => [
'class' => CsvResponseFormatter::class,
],
],
$this->defaultFormatters,
);
}

/**
Expand Down