Skip to content
Snippets Groups Projects

Issue #3371042: Drupal 10.1.0 new aggregation breaks InlineCssEmailAdjuster

1 file
+ 42
4
Compare changes
  • Side-by-side
  • Inline
@@ -7,6 +7,9 @@ use Drupal\Core\Asset\AttachedAssets;
@@ -7,6 +7,9 @@ use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\EmailInterface;
 
use Psr\Log\LoggerAwareInterface;
 
use Psr\Log\LoggerAwareTrait;
 
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
@@ -20,7 +23,9 @@ use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
@@ -20,7 +23,9 @@ use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
* weight = 900,
* weight = 900,
* )
* )
*/
*/
class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFactoryPluginInterface {
class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFactoryPluginInterface, LoggerAwareInterface {
 
 
use LoggerAwareTrait;
/**
/**
* The asset resolver.
* The asset resolver.
@@ -36,6 +41,13 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
@@ -36,6 +41,13 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
*/
*/
protected $cssInliner;
protected $cssInliner;
 
/**
 
* Drupal's app root path.
 
*
 
* @var string
 
*/
 
protected $appRoot;
 
/**
/**
* Constructor.
* Constructor.
*
*
@@ -47,11 +59,17 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
@@ -47,11 +59,17 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
* The plugin implementation definition.
* The plugin implementation definition.
* @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
* @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
* The asset resolver.
* The asset resolver.
 
* @param string $appRoot
 
* Drupal's app root path.
 
* @param Psr\Log\LoggerInterface $logger
 
* The logger instance.
*/
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AssetResolverInterface $asset_resolver) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, AssetResolverInterface $asset_resolver, string $appRoot, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->assetResolver = $asset_resolver;
$this->assetResolver = $asset_resolver;
$this->cssInliner = new CssToInlineStyles();
$this->cssInliner = new CssToInlineStyles();
 
$this->appRoot = $appRoot;
 
$this->logger = $logger;
}
}
/**
/**
@@ -62,7 +80,9 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
@@ -62,7 +80,9 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
$configuration,
$configuration,
$plugin_id,
$plugin_id,
$plugin_definition,
$plugin_definition,
$container->get('asset.resolver')
$container->get('asset.resolver'),
 
$container->getParameter('app.root'),
 
$container->get('logger.factory')->get('symfony_mailer')
);
);
}
}
@@ -75,7 +95,25 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
@@ -75,7 +95,25 @@ class InlineCssEmailAdjuster extends EmailAdjusterBase implements ContainerFacto
$assets = (new AttachedAssets())->setLibraries($email->getLibraries());
$assets = (new AttachedAssets())->setLibraries($email->getLibraries());
$css = '';
$css = '';
foreach ($this->assetResolver->getCssAssets($assets, TRUE) as $file) {
foreach ($this->assetResolver->getCssAssets($assets, TRUE) as $file) {
$css .= file_get_contents($file['data']);
$filePath = $file['data'];
 
 
// Only if we have a rooted path (which means it is web-rooted).
 
if (stripos($filePath, '/') === 0) {
 
// Correct the path to be linux-rooted.
 
$filePath = $this->appRoot . $filePath;
 
// Remove the query string which we don't need to load the file.
 
if (($pos = stripos($filePath, '?')) !== FALSE) {
 
$filePath = substr($filePath, 0, $pos);
 
}
 
}
 
 
try {
 
$this->logger->info('Loading CSS from file: %file.', ['$file' => $filePath, 'file' => $file]);
 
$css .= file_get_contents($filePath);
 
}
 
catch (\Exception $e) {
 
$this->logger->error('Failed to load CSS from file: %file. Error: %error', ['$file' => $filePath, 'file' => $file, '%error' => $e->getMessage(), 'error' => $e]);
 
}
}
}
if ($css) {
if ($css) {
Loading