You are here

public function ApiTokensFilter::process in API Tokens 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Filter/ApiTokensFilter.php \Drupal\api_tokens\Plugin\Filter\ApiTokensFilter::process()

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

src/Plugin/Filter/ApiTokensFilter.php, line 88

Class

ApiTokensFilter
Provides the API tokens filter.

Namespace

Drupal\api_tokens\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if (preg_match_all(self::PATTERN, $text, $matches)) {
    list($tokens, $ids, $args) = $matches;
    $replacements = $tokens;
    foreach ($ids as $index => $id) {
      if ($this->apiTokenManager
        ->hasDefinition($id)) {
        $placeholder = $this->apiTokenManager
          ->createInstance($id, [
          'params' => $args[$index],
        ])
          ->placeholder();
        $result
          ->addAttachments([
          'placeholders' => [
            $tokens[$index] => $placeholder,
          ],
        ]);
      }
      elseif ('cutout' == $this->settings['unregistered_tokens_behavior']) {
        $replacements[$index] = '';
      }
    }
    $text = str_replace($tokens, $replacements, $text);
    $result
      ->setProcessedText($text);
  }
  return $result;
}