class CKEditor5StylesheetsMessage

Same name in other branches
  1. 10 core/modules/ckeditor5/src/CKEditor5StylesheetsMessage.php \Drupal\ckeditor5\CKEditor5StylesheetsMessage
  2. 11.x core/modules/ckeditor5/src/CKEditor5StylesheetsMessage.php \Drupal\ckeditor5\CKEditor5StylesheetsMessage

Messaging for themes using the ckeditor_stylesheets setting.

Messaging is provided when themes are found that use ckeditor_stylesheets without a corresponding ckeditor5-stylesheets setting.

@internal This class may change at any time. It is not for use outside this module. @todo Remove in Drupal 11: https://www.drupal.org/project/ckeditor5/issues/3239012

Hierarchy

Expanded class hierarchy of CKEditor5StylesheetsMessage

1 file declares its use of CKEditor5StylesheetsMessage
CKEditor5.php in core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php
1 string reference to 'CKEditor5StylesheetsMessage'
ckeditor5.services.yml in core/modules/ckeditor5/ckeditor5.services.yml
core/modules/ckeditor5/ckeditor5.services.yml
1 service uses CKEditor5StylesheetsMessage
ckeditor5.stylesheets.message in core/modules/ckeditor5/ckeditor5.services.yml
Drupal\ckeditor5\CKEditor5StylesheetsMessage

File

core/modules/ckeditor5/src/CKEditor5StylesheetsMessage.php, line 21

Namespace

Drupal\ckeditor5
View source
final class CKEditor5StylesheetsMessage {
    use StringTranslationTrait;
    
    /**
     * The theme handler.
     *
     * @var \Drupal\Core\Extension\ThemeHandlerInterface
     */
    protected $themeHandler;
    
    /**
     * The config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * Constructs a new CKEditor5StylesheetsMessage.
     *
     * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
     *   The theme handler.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory to get the installed themes.
     */
    public function __construct(ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory) {
        $this->themeHandler = $theme_handler;
        $this->configFactory = $config_factory;
    }
    
    /**
     * Generates a warning related to ckeditor_stylesheets.
     *
     * Identifies themes using ckeditor_stylesheets without an equivalent
     * ckeditor5-stylesheets setting. If such themes are found, a warning message
     * is returned.
     *
     * @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup|null
     *   A warning message where appropriate, otherwise null.
     */
    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;
        }
        // Collect information on which themes/base themes have ckeditor_stylesheets
        // configuration, but do not have corresponding ckeditor5-stylesheets
        // configuration.
        $ckeditor_stylesheets_use = [];
        foreach ($themes as $theme_info) {
            $this->checkForStylesheetsEquivalent($theme_info, $ckeditor_stylesheets_use);
        }
        if (!empty($ckeditor_stylesheets_use)) {
            // A subtheme may unnecessarily appear multiple times.
            $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;
    }
    
    /**
     * Checks themes using ckeditor_stylesheets for CKEditor 5 equivalents.
     *
     * @param array $theme_info
     *   The config of the theme to check.
     * @param string[] $ckeditor_stylesheets_use
     *   Themes using ckeditor_stylesheets without a CKEditor 5 equivalent.
     */
    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 the primary theme has ckeditor5-stylesheets configured, do not check
        // base themes. The primary theme can potentially provide the
        // ckeditor5-stylesheets config for itself and its base themes, so we err
        // on the side of not showing a warning if this is possibly the case.
        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;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
CKEditor5StylesheetsMessage::$configFactory protected property The config factory.
CKEditor5StylesheetsMessage::$themeHandler protected property The theme handler.
CKEditor5StylesheetsMessage::checkForStylesheetsEquivalent private function Checks themes using ckeditor_stylesheets for CKEditor 5 equivalents.
CKEditor5StylesheetsMessage::getWarning public function Generates a warning related to ckeditor_stylesheets.
CKEditor5StylesheetsMessage::__construct public function Constructs a new CKEditor5StylesheetsMessage.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.