public function TokenResolver::replace in FillPDF 5.0.x
Same name and namespace in other branches
- 8.4 src/TokenResolver.php \Drupal\fillpdf\TokenResolver::replace()
Replaces all tokens in a given string with appropriate values.
This is basically a replacement for \Drupal\Core\Utility\Token::replace(), only that it resolves image tokens, applies form and field replacements after token replacement, and returns FieldMapping objects.
Parameters
string $text: An plain text string containing replaceable tokens.
\Drupal\Core\Entity\EntityInterface[][] $data: (optional) Multidimensional array of entities, keyed by entity ID and grouped by entity type.
array $options: (optional) A keyed array of settings and flags to control the token replacement process. Supported options are:
- langcode: A language code to be used when generating locale-sensitive tokens.
- callback: A callback function that will be used to post-process the array of token replacements after they are generated.
Return value
\Drupal\fillpdf\FieldMapping An instance of a FieldMapping.
Overrides TokenResolverInterface::replace
See also
\Drupal\Core\Utility\Token::replace()
File
- src/
TokenResolver.php, line 64
Class
- TokenResolver
- Class TokenResolver.
Namespace
Drupal\fillpdfCode
public function replace($text, array $data = [], array $options = []) {
// Initialize with defaults.
$options += [
'content' => '',
'replacements' => [],
'prefix' => '',
'suffix' => '',
];
$tokens = $this->tokenService
->scan($text);
if (empty($tokens)) {
return new TextFieldMapping(PlainTextOutput::renderFromHtml($text));
}
// Content may be marked as either 'text', 'image' or '' (= unknown).
// @todo: Revisit when enforcing FillPdfFields to be one or the other.
// See: https://www.drupal.org/project/fillpdf/issues/3049368
$maybe_image = $options['content'] !== 'text';
$maybe_text = $options['content'] !== 'image';
// Loop through the token types.
$bubbleable_metadata = new BubbleableMetadata();
$replacements = [];
foreach ($tokens as $token_type => $type_tokens) {
$token_entity_type = $this->tokenEntityMapper
->getEntityTypeForTokenType($token_type, FALSE);
if ($token_entity_type && isset($data[$token_entity_type])) {
// At least one provided entity matches this token type. If there's
// more than one entity of this type, make sure the last one matching
// this token wins.
foreach (array_reverse($data[$token_entity_type]) as $entity) {
// Only fieldable entities may supply image tokens.
if ($maybe_image && $entity instanceof FieldableEntityInterface) {
if ($token_entity_type === 'webform_submission' && $this->moduleHandler
->moduleExists('webform')) {
$image_mapping = static::parseImageWebformElementTokens(array_keys($type_tokens), $entity);
}
elseif ($this->moduleHandler
->moduleExists('image')) {
$image_mapping = static::parseImageFieldTokens(array_keys($type_tokens), $entity);
}
if (!empty($image_mapping)) {
// Early return if we matched an image token.
return $image_mapping;
}
}
if ($maybe_text) {
$replacements += $this->tokenService
->generate($token_type, $type_tokens, [
$token_type => $entity,
], $options, $bubbleable_metadata);
}
}
}
elseif ($maybe_text) {
// None of the provided entities matches this token type. It may however
// still be a global token.
$replacements += $this->tokenService
->generate($token_type, $type_tokens, [], $options, $bubbleable_metadata);
}
// Clear any unresolved tokens of this type from the string.
$replacements += array_fill_keys($tokens[$token_type], '');
}
// Apply token replacements.
$resolved_string = str_replace(array_keys($replacements), array_values($replacements), $text);
// Replace <br /> occurrences with newlines.
$resolved_string = preg_replace('|<br />|', "\n", $resolved_string);
// Apply transformation replacements.
if (isset($options['replacements'][$resolved_string])) {
$resolved_string = $options['replacements'][$resolved_string];
}
// Apply prefix and suffix, unless empty.
if (!empty($resolved_string)) {
$resolved_string = $options['prefix'] . $resolved_string . $options['suffix'];
}
return new TextFieldMapping(PlainTextOutput::renderFromHtml($resolved_string));
}