class Typography

Typography design token value.

Attributes

#[DesignTokenValue(id: 'typography', label: new TranslatableMarkup('Typography'))]

Hierarchy

Expanded class hierarchy of Typography

2 files declare their use of Typography
DesignTokenValuePluginManagerKernelTest.php in core/tests/Drupal/KernelTests/Core/Theme/DesignToken/DesignTokenValuePluginManagerKernelTest.php
TypographyTest.php in core/tests/Drupal/Tests/Core/Theme/DesignToken/Plugin/TypographyTest.php
6 string references to 'Typography'
core.design_token_values.schema.yml in core/config/schema/core.design_token_values.schema.yml
core/config/schema/core.design_token_values.schema.yml
DesignTokenConfigEntityKernelTest::testConfigSchema in core/tests/Drupal/KernelTests/Core/Theme/DesignToken/DesignTokenConfigEntityKernelTest.php
Tests config schema.
DesignTokenPluginManagerKernelTest::testDetectedPlugins in core/tests/Drupal/KernelTests/Core/Theme/DesignToken/DesignTokenPluginManagerKernelTest.php
Tests that plugins can be provided by YAML files.
DesignTokenValuePluginManagerKernelTest::testDetectedPlugins in core/tests/Drupal/KernelTests/Core/Theme/DesignToken/DesignTokenValuePluginManagerKernelTest.php
Tests detected plugins provided by Core.
DesignTokenValuePluginManagerKernelTest::testDetectedPlugins in core/tests/Drupal/KernelTests/Core/Theme/DesignToken/DesignTokenValuePluginManagerKernelTest.php
Tests detected plugins provided by Core.

... See full list

File

core/lib/Drupal/Core/Theme/Plugin/DesignTokenValue/Typography.php, line 16

Namespace

Drupal\Core\Theme\Plugin\DesignTokenValue
View source
class Typography extends DesignTokenValuePluginBase {
  
  /**
   * The typography's properties.
   *
   * @var array|string[]
   */
  protected array $properties = [
    'fontFamily' => 'fontFamily',
    'fontSize' => 'dimension',
    'fontWeight' => 'fontWeight',
    'letterSpacing' => 'dimension',
    'lineHeight' => 'number',
  ];
  
  /**
   * The typography's font.
   */
  protected FontFamily $fontFamily;
  
  /**
   * The size of the typography.
   */
  protected Dimension $fontSize;
  
  /**
   * The weight of the typography.
   */
  protected FontWeight $fontWeight;
  
  /**
   * The horizontal spacing between characters.
   */
  protected Dimension $letterSpacing;
  
  /**
   * The vertical spacing between lines of typography.
   */
  protected Number $lineHeight;
  public function __construct(array $configuration, $plugin_id, $plugin_definition, #[Autowire(service: 'plugin.manager.design_token_value')] protected DesignTokenValuePluginManagerInterface $designTokenValuePluginManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }
  
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() : array {
    return [
      'fontFamily' => [],
      'fontSize' => [
        'value' => 0,
        'unit' => 'px',
      ],
      'fontWeight' => '',
      'letterSpacing' => [
        'value' => 0,
        'unit' => 'px',
      ],
      'lineHeight' => 0,
    ] + parent::defaultConfiguration();
  }
  
  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) : static {
    parent::setConfiguration($configuration);
    foreach ($this->properties as $property => $type) {
      $propertyConfig = static::prepareConfiguration($this->configuration[$property]);
      $this->{$property} = $this->designTokenValuePluginManager
        ->createInstance($type, $propertyConfig);
    }
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  public function toDtcg() : mixed {
    $data = [];
    foreach ($this->properties as $property => $type) {
      $data[$property] = $this->{$property}
        ->toDtcg();
    }
    return $data;
  }
  
  /**
   * {@inheritdoc}
   */
  public function toCss() : string {
    // letterSpacing is not allowed in the font shortcut.
    return implode(' ', [
      $this->fontWeight
        ->toCss(),
      $this->fontSize
        ->toCss() . '/' . $this->lineHeight
        ->toCss(),
      $this->fontFamily
        ->toCss(),
    ]);
  }

}

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