public function PlaceholderResolver::scan in Typed Data API enhancements 8
Builds a list of all placeholder tokens that appear in the text.
Parameters
string $text: The text to be scanned for possible tokens.
Return value
array An associative array of discovered placeholder tokens, grouped by data name. For each data name, the value is another associative array containing the completed, discovered placeholder and the main placeholder part as key; i.e. the placeholder without brackets and data name. For example, for the placeholder {{ data.property.property|filter }} the main placeholder part is 'property.property|filter'.
Overrides PlaceholderResolverInterface::scan
1 call to PlaceholderResolver::scan()
- PlaceholderResolver::resolvePlaceholders in src/
PlaceholderResolver.php - Replaces all placeholder tokens in a given string with appropriate values.
File
- src/
PlaceholderResolver.php, line 178
Class
- PlaceholderResolver
- Resolver for placeholder tokens based upon typed data.
Namespace
Drupal\typed_dataCode
public function scan($text) {
// Matches tokens with the following pattern: {{ $name.$property_path }}
// $name and $property_path may not contain { or } characters.
// $name may not contain . or whitespace characters, but $property_path may.
// $name may optionally contain a prefix of the form "@service_id:" which
// indicates it's a global context variable. In this case, the prefix
// starts with @, ends with :, and doesn't contain any whitespace.
$number_of_tokens = preg_match_all('/
\\{\\{\\s* # {{ - pattern start
((?:@\\S+:)?[^\\s\\{\\}.|]*) # $match[1] $name not containing whitespace . | { or }, with optional prefix
( # $match[2] begins
(
(\\.|\\s*\\|\\s*) # . with no spaces on either side, or | as separator
[^\\s\\{\\}.|] # after separator we need at least one character
)
([^\\{\\}]*) # but then almost anything goes up until pattern end
)? # $match[2] is optional
\\s*\\}\\} # }} - pattern end
/x', $text, $matches);
$names = $matches[1];
$tokens = $matches[2];
// Iterate through the matches, building an associative array containing
// $tokens grouped by $name, pointing to the version of the token found in
// the source text. For example,
// $results['node']['title'] = '{{node.title}}';
// where '{{node.title}}' is found in the source text.
$results = [];
for ($i = 0; $i < $number_of_tokens; $i++) {
// Remove leading whitespaces and ".", but not the | denoting a filter.
$main_part = trim($tokens[$i], ". \t\n\r\0\v");
$results[$names[$i]][$main_part] = $matches[0][$i];
}
return $results;
}