Skip to content
Snippets Groups Projects
Commit 524c257e authored by Adam Shepherd's avatar Adam Shepherd Committed by Adam Shepherd
Browse files

Issue #3371042 by Niklan, Mingsong, AdamPS: Drupal 10.1.0 new aggregation...

Issue #3371042 by Niklan, Mingsong, AdamPS: Drupal 10.1.0 new aggregation breaks InlineCssEmailAdjuster
parent bcba6ae2
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,12 @@
namespace Drupal\symfony_mailer\Plugin\EmailAdjuster;
use Drupal\Core\Asset\AssetOptimizerInterface;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
......@@ -36,6 +37,13 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
*/
protected $cssInliner;
/**
* The CSS collection optimizer.
*
* @var \Drupal\Core\Asset\AssetOptimizerInterface
*/
protected $cssOptimizer;
/**
* Constructor.
*
......@@ -47,11 +55,14 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
* The plugin implementation definition.
* @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
* The asset resolver.
* @param \Drupal\Core\Asset\AssetOptimizerInterface $cssOptimizer
* The Drupal CSS optimizer.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AssetResolverInterface $asset_resolver) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, AssetResolverInterface $asset_resolver, AssetOptimizerInterface $cssOptimizer = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->assetResolver = $asset_resolver;
$this->cssInliner = new CssToInlineStyles();
$this->cssOptimizer = $cssOptimizer ?: \Drupal::service('asset.css.optimizer');
}
/**
......@@ -62,7 +73,8 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
$configuration,
$plugin_id,
$plugin_definition,
$container->get('asset.resolver')
$container->get('asset.resolver'),
$container->get('asset.css.optimizer'),
);
}
......@@ -70,12 +82,17 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
* {@inheritdoc}
*/
public function postRender(EmailInterface $email) {
// Inline CSS. Request optimization so that the CssOptimizer performs
// essential processing such as @import.
// Inline CSS.
$assets = (new AttachedAssets())->setLibraries($email->getLibraries());
$css = '';
foreach ($this->assetResolver->getCssAssets($assets, TRUE) as $file) {
$css .= file_get_contents($file['data']);
foreach ($this->assetResolver->getCssAssets($assets, FALSE) as $asset) {
if (($asset['type'] == 'file') && $asset['preprocess']) {
// Optimize to process @import.
$css .= $this->cssOptimizer->optimize($asset);
}
else {
$css .= file_get_contents($asset['data']);
}
}
if ($css) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment