Skip to content

Commit

Permalink
Simplify email site overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Nov 22, 2024
1 parent 9789352 commit dc7323e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
- Added `craft\fields\data\LinkData::getUrl()`.
- Added `craft\mail\Mailer::$siteId`.
- Added `craft\mail\Mailer::$siteOverrides`.
- Added `craft\models\MailSettings::setSiteOverrides()`.
- Added `craft\models\MailSettings::$siteOverrides`.
- `craft\models\Site` now implements `craft\base\Chippable`.

### System
Expand Down
5 changes: 4 additions & 1 deletion src/controllers/SystemSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ private function _createMailSettingsFromPost(): MailSettings
$settings->template = $this->request->getBodyParam('template');
$settings->transportType = $this->request->getBodyParam('transportType');
$settings->transportSettings = Component::cleanseConfig($this->request->getBodyParam(sprintf('transportTypes.%s', Html::id($settings->transportType))) ?? []);
$settings->setSiteOverrides($this->request->getBodyParam('siteOverrides') ?? []);
$settings->siteOverrides = array_filter(array_map(
fn(array $overrides) => array_filter($overrides),
$this->request->getBodyParam('siteOverrides') ?? [],
));

return $settings;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ public static function mailerConfig(?MailSettings $settings = null): array
],
'replyTo' => App::parseEnv($settings->replyToEmail),
'template' => App::parseEnv($settings->template),
'siteOverrides' => $settings->getSiteOverrides(),
'siteOverrides' => $settings->siteOverrides,
'transport' => $adapter->defineTransport(),
];
}
Expand Down
38 changes: 1 addition & 37 deletions src/models/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use craft\mail\transportadapters\Sendmail;
use craft\mail\transportadapters\TransportAdapterInterface;
use craft\validators\TemplateValidator;
use Illuminate\Support\Collection;
use yii\validators\EmailValidator;

/**
Expand Down Expand Up @@ -49,7 +48,7 @@ class MailSettings extends Model
* @var array Site-specific overrides
* @since 5.6.0
*/
private array $siteOverrides = [];
public array $siteOverrides = [];

/**
* @var class-string<TransportAdapterInterface>|null The transport type that should be used
Expand Down Expand Up @@ -79,27 +78,6 @@ protected function defineBehaviors(): array
];
}

/**
* @inheritdoc
*/
public function attributes()
{
return [
...parent::attributes(),
'siteOverrides',
];
}

/**
* @inheritdoc
*/
public function fields(): array
{
$fields = parent::fields();
$fields['siteOverrides'] = fn() => $this->siteOverrides;
return $fields;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -150,20 +128,6 @@ protected function defineRules(): array
return $rules;
}

/**
* Returns the site overrides.
*
* @return array
* @since 5.6.0
*/
public function getSiteOverrides(): array
{
return Collection::make(Craft::$app->getSites()->getAllSites())
->keyBy(fn(Site $site) => $site->uid)
->map(fn(Site $site) => $this->siteOverrides[$site->uid] ?? [])
->all();
}

/**
* Sets the site overrides.
*
Expand Down
10 changes: 7 additions & 3 deletions src/templates/settings/email/_index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@
code: true,
}
}|filter,
rows: settings.siteOverrides|map((overrides, siteUid) => overrides|merge({
heading: craft.app.sites.getSiteByUid(siteUid).getUiLabel(),
})),
rows: collect(craft.app.sites.getAllSites())
.keyBy(site => site.uid)
.map(site => {
heading: site.getUiLabel(),
...(settings.siteOverrides[site.uid] ?? {})
})
.all(),
fullWidth: true,
allowAdd: false,
allowDelete: false,
Expand Down

0 comments on commit dc7323e

Please sign in to comment.