Skip to content
Snippets Groups Projects

Issue #3447833 by mogtofu33: Feature for dev, better ComponentValidator

Files

<?php
declare(strict_types=1);
namespace Drupal\ui_patterns_devel\Component;
use Drupal\Core\Plugin\Component;
use Drupal\Core\Render\Component\Exception\InvalidComponentException;
use Drupal\Core\Theme\Component\ComponentValidator as OriginalComponentValidator;
/**
* Override validation of a component.
*/
final class ComponentValidator extends OriginalComponentValidator {
/**
* Override the component metadata validation to always pass.
*
* @param array $definition
* The definition to validate.
* @param bool $enforce_schemas
* TRUE if schema definitions are mandatory.
*
* @return bool
* Always TRUE to display the error as a message.
*/
public function validateDefinition(array $definition, bool $enforce_schemas): bool {
try {
parent::validateDefinition($definition, $enforce_schemas);
}
catch (InvalidComponentException $e) {
$component_id = $definition['id'];
$message = \str_replace("/n", "<br>", $e->getMessage());
\Drupal::messenger()->addError(\sprintf("Component: <b>%s</b><br>%s", $component_id, $message));
}
return TRUE;
}
/**
* Override the props provided to the component to always pass.
*
* @param array $context
* The Twig context that contains the prop data.
* @param \Drupal\Core\Plugin\Component $component
* The component to validate the props against.
*
* @return bool
* Always TRUE to display the error as a message.
*/
public function validateProps(array $context, Component $component): bool {
try {
parent::validateProps($context, $component);
}
catch (InvalidComponentException $e) {
$component_id = $component->getPluginId();
$message = \str_replace("/n", "<br>", $e->getMessage());
\Drupal::messenger()->addError(\sprintf("Component: <b>%s</b><br>%s", $component_id, $message));
}
return TRUE;
}
}
Loading