class ComponentMetadata
Same name in this branch
- 10 core/modules/sdc/src/Component/ComponentMetadata.php \Drupal\sdc\Component\ComponentMetadata
Same name in other branches
- 11.x core/modules/sdc/src/Component/ComponentMetadata.php \Drupal\sdc\Component\ComponentMetadata
- 11.x core/lib/Drupal/Core/Theme/Component/ComponentMetadata.php \Drupal\Core\Theme\Component\ComponentMetadata
Component metadata.
Hierarchy
- class \Drupal\Core\Theme\Component\ComponentMetadata uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of ComponentMetadata
2 files declare their use of ComponentMetadata
- Component.php in core/
lib/ Drupal/ Core/ Plugin/ Component.php - ComponentMetadataTest.php in core/
tests/ Drupal/ Tests/ Core/ Theme/ Component/ ComponentMetadataTest.php
File
-
core/
lib/ Drupal/ Core/ Theme/ Component/ ComponentMetadata.php, line 12
Namespace
Drupal\Core\Theme\ComponentView source
class ComponentMetadata {
use StringTranslationTrait;
/**
* The absolute path to the component directory.
*
* @var string
*/
public readonly string $path;
/**
* The component documentation.
*
* @var string
*/
public readonly string $documentation;
/**
* The status of the component.
*
* @var string
*/
public readonly string $status;
/**
* The machine name for the component.
*
* @var string
*/
public readonly string $machineName;
/**
* The component's name.
*
* @var string
*/
public readonly string $name;
/**
* The PNG path for the component thumbnail.
*
* @var string
*/
private string $thumbnailPath;
/**
* The component group.
*
* @var string
*/
public readonly string $group;
/**
* Schema for the component props.
*
* @var array[]|null
* The schemas.
*/
public readonly ?array $schema;
/**
* The component description.
*
* @var string
*/
public readonly string $description;
/**
* TRUE if the schemas for props and slots are mandatory.
*
* @var bool
*/
public readonly bool $mandatorySchemas;
/**
* Slot information.
*
* @var array
*/
public readonly array $slots;
/**
* ComponentMetadata constructor.
*
* @param array $metadata_info
* The metadata info.
* @param string $app_root
* The application root.
* @param bool $enforce_schemas
* Enforces the definition of schemas for props and slots.
*
* @throws \Drupal\Core\Render\Component\Exception\InvalidComponentException
*/
public function __construct(array $metadata_info, string $app_root, bool $enforce_schemas) {
$path = $metadata_info['path'];
// Make the absolute path, relative to the Drupal root.
$app_root = rtrim($app_root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if (str_starts_with($path, $app_root)) {
$path = substr($path, strlen($app_root));
}
$this->mandatorySchemas = $enforce_schemas;
$this->path = $path;
[
,
$machine_name,
] = explode(':', $metadata_info['id'] ?? []);
$this->machineName = $machine_name;
$this->name = $metadata_info['name'] ?? mb_convert_case($machine_name, MB_CASE_TITLE);
$this->description = $metadata_info['description'] ?? $this->t('- Description not available -');
$this->status = ExtensionLifecycle::isValid($metadata_info['status'] ?? '') ? $metadata_info['status'] : ExtensionLifecycle::STABLE;
$this->documentation = $metadata_info['documentation'] ?? '';
$this->group = $metadata_info['group'] ?? $this->t('All Components');
// Save the schemas.
$this->parseSchemaInfo($metadata_info);
$this->slots = $metadata_info['slots'] ?? [];
}
/**
* Parse the schema information.
*
* @param array $metadata_info
* The metadata information as decoded from the component definition file.
*
* @throws \Drupal\Core\Render\Component\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.');
}
$schema['additionalProperties'] = FALSE;
// All props should also support "object" this allows deferring rendering
// in Twig to the render pipeline.
$schema_props = $metadata_info['props'];
foreach ($schema_props['properties'] ?? [] as $name => $prop_schema) {
$type = $prop_schema['type'] ?? '';
$schema['properties'][$name]['type'] = array_unique([
(array) $type,
'object',
]);
}
}
$this->schema = $schema;
}
/**
* Gets the thumbnail path.
*
* @return string
* The path.
*/
public function getThumbnailPath() : string {
if (!isset($this->thumbnailPath)) {
$thumbnail_path = sprintf('%s/thumbnail.png', $this->path);
$this->thumbnailPath = file_exists($thumbnail_path) ? $thumbnail_path : '';
}
return $this->thumbnailPath;
}
/**
* Normalizes the value object.
*
* @return array
* The normalized value object.
*/
public function normalize() : array {
return [
'path' => $this->path,
'machineName' => $this->machineName,
'status' => $this->status,
'name' => $this->name,
'group' => $this->group,
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ComponentMetadata::$description | public | property | The component description. | |
ComponentMetadata::$documentation | public | property | The component documentation. | |
ComponentMetadata::$group | public | property | The component group. | |
ComponentMetadata::$machineName | public | property | The machine name for the component. | |
ComponentMetadata::$mandatorySchemas | public | property | TRUE if the schemas for props and slots are mandatory. | |
ComponentMetadata::$name | public | property | The component's name. | |
ComponentMetadata::$path | public | property | The absolute path to the component directory. | |
ComponentMetadata::$schema | public | property | Schema for the component props. | |
ComponentMetadata::$slots | public | property | Slot information. | |
ComponentMetadata::$status | public | property | The status of the component. | |
ComponentMetadata::$thumbnailPath | private | property | The PNG path for the component thumbnail. | |
ComponentMetadata::getThumbnailPath | public | function | Gets the thumbnail path. | |
ComponentMetadata::normalize | public | function | Normalizes the value object. | |
ComponentMetadata::parseSchemaInfo | private | function | Parse the schema information. | |
ComponentMetadata::__construct | public | function | ComponentMetadata constructor. | |
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.