class DesignToken

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Theme/Entity/DesignToken.php \Drupal\Core\Theme\Entity\DesignToken

A token: distinguishable by containing a $value property.

Hierarchy

Expanded class hierarchy of DesignToken

3 files declare their use of DesignToken
DesignTokenPluginManagerTest.php in core/tests/Drupal/Tests/Core/Theme/DesignToken/DesignTokenPluginManagerTest.php
DesignTokenTest.php in core/tests/Drupal/Tests/Core/Theme/DesignToken/DesignTokenTest.php
GroupTest.php in core/tests/Drupal/Tests/Core/Theme/DesignToken/GroupTest.php

File

core/lib/Drupal/Core/Theme/DesignToken/DesignToken.php, line 13

Namespace

Drupal\Core\Theme\DesignToken
View source
class DesignToken extends PluginDefinition implements DtcgInterface {
  
  /**
   * DTCG: $type.
   *
   * Actual data type which ensures consistency, validation, and tool
   * compatibility across platforms.
   */
  public readonly ?string $type;
  
  /**
   * Construct a DTCG token.
   *
   * @param string $provider
   *   The Drupal extension providing the token.
   * @param string $name
   *   The token's machine name.
   * @param \Drupal\Core\Theme\DesignToken\DesignTokenValueInterface $value
   *   The token value.
   * @param ?\Drupal\Core\Theme\DesignToken\Group $parent
   *   The parent group.
   * @param ?\Drupal\Core\StringTranslation\TranslatableMarkup $description
   *   A plain text description explaining the token's purpose.
   * @param ?string $type
   *   The token type if present.
   */
  public function __construct(string $provider, public readonly string $name, public readonly DesignTokenValueInterface $value, public readonly ?Group $parent = NULL, public readonly ?TranslatableMarkup $description = NULL, ?string $type = NULL) {
    $this->type = $type ?? $parent->type ?? NULL;
    $this->provider = $provider;
    // Spaces are allowed path but not for plugin IDs.
    $this->id = \strtr($this->getPath(), [
      ' ' => '_',
    ]);
  }
  
  /**
   * {@inheritdoc}
   */
  public function getType() : ?string {
    return $this->type;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getPath() : string {
    return $this->parent ? $this->parent
      ->getPath() . '.' . $this->name : $this->name;
  }
  
  /**
   * {@inheritdoc}
   */
  public function toDtcg() : array {
    $data = [
      '$type' => $this->type,
      '$value' => $this->value
        ->toDtcg(),
    ];
    if ($this->description) {
      $data['$description'] = $this->description
        ->render();
    }
    return $data;
  }

}

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