InvalidComponentException example

public function validateDefinition(array $definition, bool $enforce_schemas): bool {
    // First ensure there are no name collisions between props and slots.     $prop_names = array_keys($definition['props']['properties'] ?? []);
    $slot_names = array_keys($definition['slots'] ?? []);
    $collisions = array_intersect($prop_names$slot_names);
    if ($collisions) {
      $message = sprintf(
        'The component "%s" declared [%s] both as a prop and as a slot. Make sure to use different names.',
        $definition['id'],
        implode(', ', $collisions)
      );
      throw new InvalidComponentException($message);
    }
    // If the validator isn't set, then the validation library is not installed.     if (!$this->validator) {
      return TRUE;
    }
    // Detect the props with a type class, and validate that the class exists.     $schema = $definition['props'] ?? NULL;
    if (!$schema) {
      if ($enforce_schemas) {
        throw new InvalidComponentException(sprintf('The component "%s" does not provide schema information. Schema definitions are mandatory for components declared in modules. For components declared in themes, schema definitions are only mandatory if the "enforce_prop_schemas" key is set to "true" in the theme info file.', $definition['id']));
      }
      

  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);
    }
  }

}
public readonly array $library;

  /** * Component constructor. * * @throws \Drupal\sdc\Exception\InvalidComponentException */
  public function __construct(array $configuration$plugin_id$plugin_definition) {
    parent::__construct($configuration$plugin_id$plugin_definition);
    if (str_contains($plugin_id, '/')) {
      $message = sprintf('Component ID cannot contain slashes: %s', $plugin_id);
      throw new InvalidComponentException($message);
    }
    $template = $plugin_definition['template'] ?? NULL;
    if (!$template) {
      $message = sprintf(
        'Unable to find the Twig template for the component "%s".',
        $plugin_id
      );
      throw new InvalidComponentException($message);
    }
    $this->template = $template;
    $this->machineName = $plugin_definition['machineName'];
    
/** * Parse the schema information. * * @param array $metadata_info * The metadata information as decoded from the component definition file. * * @throws \Drupal\sdc\Exception\InvalidComponentException */
  private function parseSchemaInfo(array $metadata_info): void {
    if (empty($metadata_info['props'])) {
      if ($this->mandatorySchemas) {
        throw new InvalidComponentException(sprintf('The component "%s" does not provide schema information. Schema definitions are mandatory for components declared in modules. For components declared in themes, schema definitions are only mandatory if the "enforce_prop_schemas" key is set to "true" in the theme info file.', $metadata_info['id']));
      }
      $schema = NULL;
    }
    else {
      $schema = $metadata_info['props'];
      if (($schema['type'] ?? 'object') !== 'object') {
        throw new InvalidComponentException('The schema for the props in the component metadata is invalid. The schema should be of type "object".');
      }
      if ($schema['additionalProperties'] ?? FALSE) {
        throw new InvalidComponentException('The schema for the %s in the component metadata is invalid. Arbitrary additional properties are not allowed.');
      }
      
// Now build the error message.     $error_messages = [];
    if (!empty($undocumented_ids)) {
      $error_messages[] = sprintf(
        'We found an unexpected slot that is not declared: [%s]. Please declare them in "%s.component.yml".',
        implode(', ', $undocumented_ids),
        $component->machineName
      );
    }
    if (!empty($error_messages)) {
      $message = implode("\n", $error_messages);
      throw new InvalidComponentException($message);
    }
  }

  /** * Chooses an emoji representative for the input string. * * @param string $input * The input string. * * @return string * The emoji code. */
Home | Imprint | This part of the site doesn't use cookies.