function ComponentsTwigExtension::doValidateProps

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Template/ComponentsTwigExtension.php \Drupal\Core\Template\ComponentsTwigExtension::doValidateProps()
  2. 10 core/lib/Drupal/Core/Template/ComponentsTwigExtension.php \Drupal\Core\Template\ComponentsTwigExtension::doValidateProps()

Performs the actual validation of the schema for the props.

Parameters

array $context: The context provided to the component.

string $component_id: The component ID.

Return value

bool TRUE if it's valid.

Throws

\Drupal\Core\Render\Component\Exception\InvalidComponentException

1 call to ComponentsTwigExtension::doValidateProps()
ComponentsTwigExtension::validateProps in core/lib/Drupal/Core/Template/ComponentsTwigExtension.php
Validates the props in development environments.

File

core/lib/Drupal/Core/Template/ComponentsTwigExtension.php, line 123

Class

ComponentsTwigExtension
The twig extension so Drupal can recognize the new code.

Namespace

Drupal\Core\Template

Code

protected function doValidateProps(array $context, string $component_id) : bool {
  try {
    return $this->componentValidator
      ->validateProps($context, $this->pluginManager
      ->find($component_id));
  } catch (ComponentNotFoundException $e) {
    throw new InvalidComponentException($e->getMessage(), $e->getCode(), $e);
  } catch (InvalidComponentException $e) {
    // Inspect the backtrace to find the template that embedded the component.
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 20);
    $calling_template = 'Unknown Template';
    foreach ($backtrace as $trace) {
      if (isset($trace['object']) && $trace['object'] instanceof Template) {
        $template_name = $trace['object']->getTemplateName();
        // Skip the component's own template to find its parent container.
        if ($template_name === $component_id) {
          continue;
        }
        $calling_template = $template_name;
        break;

      }
    }
    throw new InvalidComponentException(sprintf('Component validation failed in Twig file [%s] for component [%s]: %s', $calling_template, $component_id, $e->getMessage()), $e->getCode(), $e);
  }
}

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