function DesignTokenPluginManager::parseDtcg

Parse W3C DTCG format.

Parameters

string $provider: The Drupal extension providing the token.

string $name: The token name.

array $data: The raw DTCG data.

?\Drupal\Core\Theme\DesignToken\Group $parent: The parent group. NULL if root.

Return value

\Drupal\Core\Theme\DesignToken\DtcgInterface|null A DTCG object.

File

core/lib/Drupal/Core/Theme/DesignToken/DesignTokenPluginManager.php, line 107

Class

DesignTokenPluginManager
Defines a design tokens plugin manager.

Namespace

Drupal\Core\Theme\DesignToken

Code

protected function parseDtcg(string $provider, string $name, array $data, ?Group $parent = NULL) : ?DtcgInterface {
  // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
  $description = isset($data['$description']) ? new TranslatableMarkup($data['$description']) : NULL;
  // An array with a $value property is a token.
  if (isset($data['$value'])) {
    try {
      $data['$value'] = DesignTokenValuePluginBase::prepareConfiguration($data['$value']);
      /** @var \Drupal\Core\Theme\DesignToken\DesignTokenValueInterface $value */
      $value = $this->designTokenValuePluginManager
        ->createInstance($data['$type'] ?? $parent?->type, $data['$value']);
      return new DesignToken($provider, $name, $value, $parent, $description, $data['$type'] ?? NULL);
    } catch (PluginException) {
      // Type does not have a design token value plugin or has an invalid
      // structure.
      return NULL;
    }
  }
  // An array without a $value property is a group.
  $group = new Group($provider, $name, [], $parent, $description, $data['$type'] ?? NULL);
  $children = [];
  foreach ($data as $childName => $childData) {
    if (str_starts_with($childName, '$')) {
      continue;
    }
    $child = $this->parseDtcg($provider, $childName, $childData, $group);
    if ($child) {
      $children[$childName] = $child;
    }
  }
  $group->setChildren($children);
  return $group;
}

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