function ComponentMetadata::parseSchemaInfo

Same name in this branch
  1. 11.x core/modules/sdc/src/Component/ComponentMetadata.php \Drupal\sdc\Component\ComponentMetadata::parseSchemaInfo()
Same name and namespace in other branches
  1. 10 core/modules/sdc/src/Component/ComponentMetadata.php \Drupal\sdc\Component\ComponentMetadata::parseSchemaInfo()
  2. 10 core/lib/Drupal/Core/Theme/Component/ComponentMetadata.php \Drupal\Core\Theme\Component\ComponentMetadata::parseSchemaInfo()

Parse the schema information.

Parameters

array $metadata_info: The metadata information as decoded from the component definition file.

Return value

array|null The schema for the component props.

Throws

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

1 call to ComponentMetadata::parseSchemaInfo()
ComponentMetadata::__construct in core/lib/Drupal/Core/Theme/Component/ComponentMetadata.php
ComponentMetadata constructor.

File

core/lib/Drupal/Core/Theme/Component/ComponentMetadata.php, line 156

Class

ComponentMetadata
Component metadata.

Namespace

Drupal\Core\Theme\Component

Code

private function parseSchemaInfo(array $metadata_info) : ?array {
  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.', $this->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.');
    }
    $schema['additionalProperties'] = FALSE;
    foreach ($schema['properties'] ?? [] as $name => $prop_schema) {
      if (isset($prop_schema['enum'])) {
        // Ensure all enum values are also in meta:enum.
        $enum = array_combine($prop_schema['enum'], $prop_schema['enum']);
        $prop_schema['meta:enum'] = array_replace($enum, $prop_schema['meta:enum'] ?? []);
        // Remove meta:enum values which are not in enum.
        $prop_schema['meta:enum'] = array_intersect_key($prop_schema['meta:enum'], $enum);
        // Make meta:enum label translatable.
        $translation_context = $prop_schema['x-translation-context'] ?? '';
        $prop_schema['meta:enum'] = array_map(fn($label) => new TranslatableMarkup((string) $label, [], [
          'context' => $translation_context,
        ]), $prop_schema['meta:enum']);
        $schema['properties'][$name] = $prop_schema;
      }
      // All props should also support "object" this allows deferring
      // rendering in Twig to the render pipeline.
      $type = $prop_schema['type'] ?? '';
      $schema['properties'][$name]['type'] = array_unique([
        (array) $type,
        'object',
      ]);
    }
  }
  return $schema;
}

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