You are here

public function PlaceholderResolver::resolvePlaceholders in Typed Data API enhancements 8

Replaces all placeholder tokens in a given string with appropriate values.

Parameters

string $text: An HTML string containing replaceable tokens. The caller is responsible for calling \Drupal\Component\Utility\Html::escape() in case the $text was plain text.

\Drupal\Core\TypedData\TypedDataInterface[] $data: The data to use for generating values for the placeholder, keyed by name.

\Drupal\Core\Render\BubbleableMetadata|null $bubbleable_metadata: (optional) An object to which required bubbleable metadata will be added. Refer to ::replacePlaceHolders() for further details.

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.
  • clear: A boolean flag indicating that tokens should be removed from the final text if no replacement value can be generated. Defaults to FALSE.

Return value

\Drupal\Component\Render\MarkupInterface[] An array of replacement values for the placeholders contained in the text, keyed by placeholder.

Overrides PlaceholderResolverInterface::resolvePlaceholders

1 call to PlaceholderResolver::resolvePlaceholders()
PlaceholderResolver::replacePlaceHolders in src/PlaceholderResolver.php
Replaces the placeholders in the given text.

File

src/PlaceholderResolver.php, line 48

Class

PlaceholderResolver
Resolver for placeholder tokens based upon typed data.

Namespace

Drupal\typed_data

Code

public function resolvePlaceholders($text, array $data = [], BubbleableMetadata $bubbleable_metadata = NULL, array $options = []) {
  $options += [
    'langcode' => NULL,
    'clear' => FALSE,
  ];
  $placeholder_by_data = $this
    ->scan($text);
  if (empty($placeholder_by_data)) {
    return [];
  }
  $replacements = [];
  foreach ($placeholder_by_data as $data_name => $placeholders) {
    foreach ($placeholders as $placeholder_main_part => $placeholder) {
      try {
        if (!isset($data[$data_name])) {
          throw new MissingDataException("There is no data with the name '{$data_name}' available.");
        }
        list($property_sub_paths, $filters) = $this
          ->parseMainPlaceholderPart($placeholder_main_part, $placeholder);
        $fetched_data = $this->dataFetcher
          ->fetchDataBySubPaths($data[$data_name], $property_sub_paths, $bubbleable_metadata, $options['langcode']);

        // Apply filters.
        if ($filters) {
          $value = $fetched_data
            ->getValue();
          $definition = $fetched_data
            ->getDataDefinition();
          foreach ($filters as $filter_data) {
            list($filter_id, $arguments) = $filter_data;
            $filter = $this->dataFilterManager
              ->createInstance($filter_id);
            if (!$filter
              ->allowsNullValues() && !isset($value)) {
              throw new MissingDataException("There is no data value for filter '{$filter_id}' to work on.");
            }
            $value = $filter
              ->filter($definition, $value, $arguments, $bubbleable_metadata);
            $definition = $filter
              ->filtersTo($definition, $arguments);
          }
        }
        else {
          $value = $fetched_data
            ->getString();
        }

        // Escape the tokens, unless they are explicitly markup.
        $replacements[$placeholder] = $value instanceof MarkupInterface ? $value : new HtmlEscapedText($value);
      } catch (InvalidArgumentException $e) {

        // Should we log warnings if there are problems other than missing
        // data, like syntactically invalid placeholders?
        if (!empty($options['clear'])) {
          $replacements[$placeholder] = '';
        }
      } catch (MissingDataException $e) {
        if (!empty($options['clear'])) {
          $replacements[$placeholder] = '';
        }
      }
    }
  }
  return $replacements;
}