function CKEditor5PluginDefinition::validateDrupalAspects
Same name in other branches
- 10 core/modules/ckeditor5/src/Plugin/CKEditor5PluginDefinition.php \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition::validateDrupalAspects()
- 11.x core/modules/ckeditor5/src/Plugin/CKEditor5PluginDefinition.php \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition::validateDrupalAspects()
Validates the Drupal aspects of the CKEditor 5 plugin definition.
@internal
Parameters
string $id: The plugin ID, for use in exception messages.
array $definition: The plugin definition to validate.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
See also
\Drupal\ckeditor5\Plugin\CKEditor5PluginManager::processDefinition()
File
-
core/
modules/ ckeditor5/ src/ Plugin/ CKEditor5PluginDefinition.php, line 128
Class
- CKEditor5PluginDefinition
- Provides an implementation of a CKEditor 5 plugin definition.
Namespace
Drupal\ckeditor5\PluginCode
public function validateDrupalAspects(string $id, array $definition) : void {
if (!isset($definition['drupal'])) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition must contain a "drupal" key.', $id));
}
// Without a label, the CKEditor 5 UI, validation constraints et cetera
// cannot be as informative in guiding the end user.
if (!isset($definition['drupal']['label'])) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition must contain a "drupal.label" key.', $id));
}
elseif (!is_string($definition['drupal']['label']) && !$definition['drupal']['label'] instanceof TranslatableMarkup) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a "drupal.label" value that is not a string nor a TranslatableMarkup instance.', $id));
}
// Without accurate and complete metadata about what HTML elements a
// CKEditor 5 plugin supports, Drupal cannot ensure a complete and accurate
// upgrade path.
if (!isset($definition['drupal']['elements'])) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition must contain a "drupal.elements" key.', $id));
}
elseif ($definition['id'] === 'ckeditor5_sourceEditing') {
assert($definition['drupal']['elements'] === []);
}
elseif ($definition['drupal']['elements'] !== FALSE && !(is_array($definition['drupal']['elements']) && !empty($definition['drupal']['elements']) && Inspector::assertAllStrings($definition['drupal']['elements']))) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a "drupal.elements" value that is neither a list of HTML tags/attributes nor false.', $id));
}
elseif (is_array($definition['drupal']['elements'])) {
foreach ($definition['drupal']['elements'] as $index => $element) {
$parsed = HTMLRestrictions::fromString($element);
if ($parsed->allowsNothing()) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a value at "drupal.elements.%d" that is not an HTML tag with optional attributes: "%s". Expected structure: "<tag allowedAttribute="allowedValue1 allowedValue2">".', $id, $index, $element));
}
if (count($parsed->getAllowedElements()) > 1) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a value at "drupal.elements.%d": multiple tags listed, should be one: "%s".', $id, $index, $element));
}
}
}
if (isset($definition['drupal']['class']) && !class_exists($definition['drupal']['class'])) {
throw new InvalidPluginDefinitionException($id, sprintf('The CKEditor 5 "%s" provides a plugin class: "%s", but it does not exist.', $id, $definition['drupal']['class']));
}
elseif (isset($definition['drupal']['class']) && !in_array(CKEditor5PluginInterface::class, class_implements($definition['drupal']['class']))) {
throw new InvalidPluginDefinitionException($id, sprintf('CKEditor 5 plugins must implement \\Drupal\\ckeditor5\\Plugin\\CKEditor5PluginInterface. "%s" does not.', $id));
}
elseif (in_array(CKEditor5PluginConfigurableInterface::class, class_implements($definition['drupal']['class'], TRUE))) {
$default_configuration = (new \ReflectionClass($definition['drupal']['class']))->newInstanceWithoutConstructor()
->defaultConfiguration();
if (!empty($default_configuration)) {
$configuration_name = sprintf("ckeditor5.plugin.%s", $definition['id']);
if (!$this->getTypedConfig()
->hasConfigSchema($configuration_name)) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition is configurable, has non-empty default configuration but has no config schema. Config schema is required for validation.', $id));
}
$error_message = $this->validateConfiguration($default_configuration);
if ($error_message) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition is configurable, but its default configuration does not match its config schema. %s', $id, $error_message));
}
}
}
if ($definition['drupal']['conditions'] !== FALSE) {
// @see \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::isPluginDisabled()
// @see \Drupal\ckeditor5\Plugin\Validation\Constraint\ToolbarItemConditionsMetConstraintValidator::validate()
$supported_condition_types = [
'toolbarItem' => function ($value) : ?string {
return is_string($value) ? NULL : 'A string corresponding to a CKEditor 5 toolbar item must be specified.';
},
'imageUploadStatus' => function ($value) : ?string {
return is_bool($value) ? NULL : 'A boolean indicating whether image uploads must be enabled (true) or not (false) must be specified.';
},
'filter' => function ($value) : ?string {
return is_string($value) ? NULL : 'A string corresponding to a filter plugin ID must be specified.';
},
'requiresConfiguration' => function ($required_configuration, array $definition) : ?string {
if (!is_array($required_configuration)) {
return 'An array structure matching the required configuration for this plugin must be specified.';
}
if (!in_array(CKEditor5PluginConfigurableInterface::class, class_implements($definition['drupal']['class'], TRUE))) {
return 'This condition type is only available for CKEditor 5 plugins implementing CKEditor5PluginConfigurableInterface.';
}
$error_message = $this->validateConfiguration($required_configuration);
return is_string($error_message) ? sprintf('The required configuration does not match its config schema. %s', $error_message) : NULL;
},
'plugins' => function ($value) : ?string {
return is_array($value) && Inspector::assertAllStrings($value) ? NULL : 'A list of strings, each corresponding to a CKEditor 5 plugin ID must be specified.';
},
];
$unsupported_condition_types = array_keys(array_diff_key($definition['drupal']['conditions'], $supported_condition_types));
if (!empty($unsupported_condition_types)) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a "drupal.conditions" value that contains some unsupported condition types: "%s". Only the following conditions types are supported: "%s".', $id, implode(', ', $unsupported_condition_types), implode('", "', array_keys($supported_condition_types))));
}
foreach ($definition['drupal']['conditions'] as $condition_type => $value) {
$assessment = $supported_condition_types[$condition_type]($value, $definition);
if (is_string($assessment)) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has an invalid "drupal.conditions" item. "%s" is set to an invalid value. %s', $id, $condition_type, $assessment));
}
}
}
if ($definition['drupal']['admin_library'] !== FALSE) {
[
$extension,
$library,
] = explode('/', $definition['drupal']['admin_library'], 2);
if (\Drupal::service('library.discovery')->getLibraryByName($extension, $library) === FALSE) {
throw new InvalidPluginDefinitionException($id, sprintf('The "%s" CKEditor 5 plugin definition has a "drupal.admin_library" key whose asset library "%s" does not exist.', $id, $definition['drupal']['admin_library']));
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.