function EnabledConfigurablePluginsConstraintValidator::validate

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Plugin/Validation/Constraint/EnabledConfigurablePluginsConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\EnabledConfigurablePluginsConstraintValidator::validate()
  2. 10 core/modules/ckeditor5/src/Plugin/Validation/Constraint/EnabledConfigurablePluginsConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\EnabledConfigurablePluginsConstraintValidator::validate()

Throws

\Symfony\Component\Validator\Exception\UnexpectedTypeException Thrown when the given constraint is not supported by this validator.

File

core/modules/ckeditor5/src/Plugin/Validation/Constraint/EnabledConfigurablePluginsConstraintValidator.php, line 29

Class

EnabledConfigurablePluginsConstraintValidator
Enabled configurable plugin settings validator.

Namespace

Drupal\ckeditor5\Plugin\Validation\Constraint

Code

public function validate($settings, Constraint $constraint) : void {
    if (!$constraint instanceof EnabledConfigurablePluginsConstraint) {
        throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\EnabledConfigurablePluginsConstraint');
    }
    $configurable_enabled_definitions = $this->getConfigurableEnabledDefinitions();
    try {
        $plugin_settings = $this->context
            ->getRoot()
            ->get('settings.plugins')
            ->getValue();
    } catch (\InvalidArgumentException) {
        $plugin_settings = [];
    }
    foreach ($configurable_enabled_definitions as $id => $definition) {
        // Create a fresh instance of this CKEditor 5 plugin, not tied to a text
        // editor configuration entity.
        $plugin = $this->pluginManager
            ->getPlugin($id, NULL);
        // If this plugin is configurable but it has empty default configuration,
        // that means the configuration must be stored out of band.
        // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Image
        // @see editor_image_upload_settings_form()
        $default_configuration = $plugin->defaultConfiguration();
        if ($default_configuration === []) {
            continue;
        }
        if (!isset($plugin_settings[$id]) || empty($plugin_settings[$id])) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('%plugin_label', (string) $definition->label())
                ->setParameter('%plugin_id', $id)
                ->atPath("plugins.{$id}")
                ->addViolation();
        }
    }
}

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