You are here

protected function DataFetcher::getAutocompleteSuggestion in Typed Data API enhancements 8

Generates autocomplete suggestions for a matched data definition.

Parameters

\Drupal\Core\TypedData\DataDefinitionInterface $data_definition: The data definition to inspect.

string $variable_name: The variable name or property path.

Return value

array[] A list of autocomplete suggestions - valid property paths for the provided data definition. Each entry is an array with the following keys:

  • value: the data selector property path.
  • label: the human readable label suggestion.
1 call to DataFetcher::getAutocompleteSuggestion()
DataFetcher::autocompletePropertyPath in src/DataFetcher.php
Provides autocomplete suggestions for an incomplete property path.

File

src/DataFetcher.php, line 293

Class

DataFetcher
Implementation of the data fetcher service.

Namespace

Drupal\typed_data

Code

protected function getAutocompleteSuggestion(DataDefinitionInterface $data_definition, $variable_name) {
  $label = $variable_name;
  if ($data_label = $data_definition
    ->getLabel()) {
    $label .= " ({$data_label})";
  }
  $results[] = [
    'value' => $variable_name,
    'label' => $label,
  ];

  // If the data definition is just a reference then directly dereference the
  // target.
  if ($data_definition instanceof DataReferenceDefinitionInterface) {
    $data_definition = $data_definition
      ->getTargetDefinition();
  }
  if ($data_definition instanceof ListDataDefinitionInterface || $data_definition instanceof ComplexDataDefinitionInterface) {
    $label = "{$variable_name}...";
    if ($data_label) {
      $label .= " ({$data_label})";
    }
    $results[] = [
      'value' => "{$variable_name}.",
      'label' => $label,
    ];
  }
  return $results;
}