Issue #3605794 by dydave: Made configuration schema fully validatable.
https://www.drupal.org/project/block_class/issues/3605794
Add CSS class validation to the classes field
Summary
Introduces input validation for the CSS classes field at both the config schema level and the block configuration form level, ensuring that only valid CSS identifiers can be saved.
Changes
1. New CssClasses validation constraint plugin
New files:
src/Plugin/Validation/Constraint/CssClassesConstraint.phpsrc/Plugin/Validation/Constraint/CssClassesConstraintValidator.php
A Drupal/Symfony constraint plugin that validates each space-separated token is a valid CSS identifier per the CSS 2.1 specification. The validator compares each token against Html::cleanCssIdentifier() — the same function already used to sanitise classes at render time — ensuring consistency between input validation and output rendering. Invalid tokens are reported individually (e.g. '1invalid' is not a valid CSS class name.).
2. Config schema — enforce validation on save
Modified: config/schema/block_class.schema.yml
- Enabled the
FullyValidatableconstraint on theblock.block.*.third_party.block_classmapping (previously commented out), so the full mapping is validated at config save time - Replaced the
# TODOcomment on theclassesproperty withCssClasses: ~, wiring the new constraint to the field
3. Block configuration form — user-facing validation error
Modified: src/Hook/BlockClassHooks.php
- Injected
TypedDataManagerInterfaceintoBlockClassHooks(autowired, noservices.ymlchange needed) - Added
#element_validateon theclassestextfield - Added
validateCssClasses()method that runs the value through the sameCssClassesconstraint viaTypedDataManagerand maps any violations to form errors — no logic duplication between form and schema validation
4. Automated tests
| File | Type | What it covers |
|---|---|---|
tests/src/Unit/Plugin/Validation/Constraint/CssClassesConstraintValidatorTest.php |
Unit | Validator logic in isolation: 14 valid inputs (BEM, prefixes, spaces…) + 4 invalid inputs + multi-violation case (~100 ms, no Drupal bootstrap) |
tests/src/Kernel/Plugin/Validation/Constraint/CssClassesConstraintValidatorTest.php |
Kernel | Constraint plugin discovery and wiring via TypedDataManager |
tests/src/Kernel/BlockClassConfigSchemaTest.php |
Kernel | Full block config schema validation with FullyValidatable + CssClasses constraints; also validates the negative case (invalid classes fail schema validation) |
tests/src/Functional/BlockClassTest.php |
Functional | 3 new data-provider cases asserting invalid classes show a field-level form error and do not save the block |