AddItemToToolbar.php

Namespace

Drupal\ckeditor5\Plugin\ConfigAction

File

core/modules/ckeditor5/src/Plugin/ConfigAction/AddItemToToolbar.php

View source
<?php

namespace Drupal\ckeditor5\Plugin\ConfigAction;

use Drupal\ckeditor5\Plugin\CKEditor5PluginManagerInterface;
use Drupal\Core\Config\Action\Attribute\ConfigAction;
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Config\Action\ConfigActionPluginInterface;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\editor\EditorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class AddItemToToolbar implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
    public function __construct(ConfigManagerInterface $configManager, CKEditor5PluginManagerInterface $pluginManager, string $pluginId) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($container->get(ConfigManagerInterface::class), $container->get(CKEditor5PluginManagerInterface::class), $plugin_id);
    }
    
    /**
     * {@inheritdoc}
     */
    public function apply(string $configName, mixed $value) : void {
        $editor = $this->configManager
            ->loadConfigEntityByName($configName);
        assert($editor instanceof EditorInterface);
        if ($editor->getEditor() !== 'ckeditor5') {
            throw new ConfigActionException(sprintf('The %s config action only works with editors that use CKEditor 5.', $this->pluginId));
        }
        $editor_settings = $editor->getSettings();
        if (is_string($value)) {
            $editor_settings['toolbar']['items'][] = $item_name = $value;
        }
        else {
            assert(is_array($value));
            $item_name = $value['item_name'];
            assert(is_string($item_name));
            $replace = $value['replace'] ?? FALSE;
            assert(is_bool($replace));
            $position = $value['position'] ?? NULL;
            if (is_int($position)) {
                // If we want to replace the item at this position, then `replace`
                // should be true. This would be useful if, for example, we wanted to
                // replace the Image button with the Media Library.
                array_splice($editor_settings['toolbar']['items'], $position, $replace ? 1 : 0, $item_name);
            }
            else {
                $editor_settings['toolbar']['items'][] = $item_name;
            }
        }
        // If we're just adding a vertical separator, there's nothing else we need
        // to do at this point.
        if ($item_name === '|') {
            return;
        }
        // If this item is associated with a plugin, ensure that it's configured
        // at the editor level, if necessary.
        
        /** @var \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition $definition */
        foreach ($this->pluginManager
            ->getDefinitions() as $id => $definition) {
            if (array_key_exists($item_name, $definition->getToolbarItems())) {
                // If plugin settings already exist, don't change them.
                if (array_key_exists($id, $editor_settings['plugins'])) {
                    break;
                }
                elseif ($definition->isConfigurable()) {
                    
                    /** @var \Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface $plugin */
                    $plugin = $this->pluginManager
                        ->getPlugin($id, NULL);
                    $editor_settings['plugins'][$id] = $plugin->defaultConfiguration();
                }
                // No need to examine any other plugins.
                break;
            }
        }
        $editor->setSettings($editor_settings)
            ->save();
    }

}

Classes

Title Deprecated Summary
AddItemToToolbar

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