function TypedDataResolver::getTokensFromComplexData

Same name and namespace in other branches
  1. 4.0.x src/TypedDataResolver.php \Drupal\ctools\TypedDataResolver::getTokensFromComplexData()

Returns tokens for a complex data definition.

Parameters

\Drupal\Core\TypedData\ComplexDataDefinitionInterface $complex_data_definition: Complex Data Definition.

Return value

array An array of token keys and corresponding labels.

1 call to TypedDataResolver::getTokensFromComplexData()
TypedDataResolver::getTokensForContexts in src/TypedDataResolver.php
Extracts an array of tokens and labels.

File

src/TypedDataResolver.php, line 231

Class

TypedDataResolver
Typed Data Resolver Service.

Namespace

Drupal\ctools

Code

protected function getTokensFromComplexData(ComplexDataDefinitionInterface $complex_data_definition) {
  $tokens = [];
  // Loop over all properties.
  foreach ($complex_data_definition->getPropertyDefinitions() as $property_name => $property_definition) {
    // Item definitions do not always have a label. Use the list definition
    // label if the item does not have one.
    $property_label = $property_definition->getLabel();
    if ($property_definition instanceof ListDataDefinitionInterface) {
      $property_definition = $property_definition->getItemDefinition();
      $property_label = $property_definition->getLabel() ?: $property_label;
    }
    // If the property is complex too, recurse to find child properties.
    if ($property_definition instanceof ComplexDataDefinitionInterface) {
      $property_tokens = $this->getTokensFromComplexData($property_definition);
      foreach ($property_tokens as $token => $label) {
        $tokens[$property_name . ':' . $token] = count($property_tokens) > 1 ? $property_label . ': ' . $label : $property_label;
      }
    }
    // Only expose references as tokens.
    // @todo Consider to expose primitive and non-reference typed data
    //   definitions too, like strings, integers and dates. The current UI
    //   will not scale to that.
    if ($property_definition instanceof DataReferenceDefinitionInterface) {
      $tokens[$property_name] = $property_definition->getLabel();
    }
  }
  return $tokens;
}