CKEditor5StylesheetsMessage.php in Drupal 10
File
core/modules/ckeditor5/src/CKEditor5StylesheetsMessage.php
View source
<?php
declare (strict_types=1);
namespace Drupal\ckeditor5;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
final class CKEditor5StylesheetsMessage {
use StringTranslationTrait;
protected $themeHandler;
protected $configFactory;
public function __construct(ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory) {
$this->themeHandler = $theme_handler;
$this->configFactory = $config_factory;
}
public function getWarning() {
$themes = [];
$default_theme = $this->configFactory
->get('system.theme')
->get('default');
if (!empty($default_theme)) {
$themes[$default_theme] = $this->themeHandler
->listInfo()[$default_theme]->info;
}
$admin_theme = $this->configFactory
->get('system.theme')
->get('admin');
if (!empty($admin_theme) && $admin_theme !== $default_theme) {
$themes[$admin_theme] = $this->themeHandler
->listInfo()[$admin_theme]->info;
}
$ckeditor_stylesheets_use = [];
foreach ($themes as $theme_info) {
$this
->checkForStylesheetsEquivalent($theme_info, $ckeditor_stylesheets_use);
}
if (!empty($ckeditor_stylesheets_use)) {
$ckeditor_stylesheets_use = array_unique($ckeditor_stylesheets_use);
$last_item = array_pop($ckeditor_stylesheets_use);
$stylesheets_warning = $this
->formatPlural(count($ckeditor_stylesheets_use) + 1, 'The %last_item theme has ckeditor_stylesheets configured without a corresponding ckeditor5-stylesheets configuration. See the <a href=":change_record">change record</a> for details.', 'The %first_items and %last_item themes have ckeditor_stylesheets configured, but without corresponding ckeditor5-stylesheets configurations. See the <a href=":change_record">change record</a> for details.', [
'%last_item' => $last_item,
'%first_items' => implode(', ', $ckeditor_stylesheets_use),
':change_record' => 'https://www.drupal.org/node/3259165',
]);
return $stylesheets_warning;
}
return NULL;
}
private function checkForStylesheetsEquivalent(array $theme_info, array &$ckeditor_stylesheets_use) {
$theme_has_ckeditor5_stylesheets = isset($theme_info['ckeditor5-stylesheets']);
if (!empty($theme_info['ckeditor_stylesheets']) && !$theme_has_ckeditor5_stylesheets) {
$ckeditor_stylesheets_use[] = $theme_info['name'];
}
if ($theme_has_ckeditor5_stylesheets) {
return;
}
$base_theme = $theme_info['base theme'] ?? FALSE;
while ($base_theme) {
$base_theme_info = $this->themeHandler
->listInfo()[$base_theme]->info;
$base_theme_has_ckeditor5_stylesheets = isset($base_theme_info['ckeditor5-stylesheets']);
if (!empty($base_theme_info['ckeditor_stylesheets']) && !$base_theme_has_ckeditor5_stylesheets) {
$ckeditor_stylesheets_use[] = $base_theme_info['name'];
}
$base_theme = $base_theme_info['base theme'] ?? FALSE;
}
}
}