function CKEditor5PluginManager::processDefinition

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::processDefinition()
  2. 10 core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::processDefinition()

Overrides DefaultPluginManager::processDefinition

File

core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php, line 82

Class

CKEditor5PluginManager
Provides a CKEditor 5 plugin manager.

Namespace

Drupal\ckeditor5\Plugin

Code

public function processDefinition(&$definition, $plugin_id) {
    if (!$definition instanceof CKEditor5PluginDefinition) {
        throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" CKEditor 5 plugin definition must extend %s', $plugin_id, CKEditor5PluginDefinition::class));
    }
    // A derived plugin will still have the ID of the derivative, rather than
    // that of the derived plugin ID (`<base plugin ID>:<derivative ID>`).
    // Generate an updated CKEditor5PluginDefinition.
    // @see \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator::encodePluginId()
    // @todo Remove this in https://www.drupal.org/project/drupal/issues/2458769.
    $is_derived = $definition->id() !== $plugin_id;
    if ($is_derived) {
        $definition = new CKEditor5PluginDefinition([
            'id' => $plugin_id,
        ] + $definition->toArray());
    }
    $expected_prefix = sprintf("%s_", $definition->getProvider());
    $id = $definition->id();
    if (!str_starts_with($id, $expected_prefix)) {
        throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition must have a plugin ID that starts with "%s".', $id, $expected_prefix));
    }
    try {
        $definition->validateCKEditor5Aspects($id, $definition->toArray());
        $definition->validateDrupalAspects($id, $definition->toArray());
    } catch (InvalidPluginDefinitionException $e) {
        // If this exception is thrown for a derived CKEditor 5 plugin definition,
        // it means the deriver did not generate a valid plugin definition.
        // Re-throw the exception, but tweak the language for DX: clarify it is
        // for a derived plugin definition.
        if ($is_derived) {
            throw new InvalidPluginDefinitionException($e->getPluginId(), str_replace('plugin definition', 'derived plugin definition', $e->getMessage()));
        }
        // Otherwise, the exception was appropriate: re-throw it.
        throw $e;
    }
    parent::processDefinition($definition, $plugin_id);
}

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