You are here

public function EntityEmbedByID::process in Embed 8

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

tests/embed_test/src/Plugin/Filter/EntityEmbedByID.php, line 76

Class

EntityEmbedByID
Renders a full node view from an embed code like node:NID.

Namespace

Drupal\embed_test\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  $matches = [];
  preg_match_all('/node:([0-9]+)/', $text, $matches);
  foreach ($matches[0] as $i => $search) {
    $replace = '';
    if ($node = $this->entityTypeManager
      ->getStorage('node')
      ->load($matches[1][$i])) {
      $build = $this->entityTypeManager
        ->getViewBuilder('node')
        ->view($node);
      $replace = $this->renderer
        ->executeInRenderContext(new RenderContext(), function () use (&$build) {
        return $this->renderer
          ->render($build);
      });
      $result = $result
        ->merge(BubbleableMetadata::createFromRenderArray($build));
    }
    $text = str_replace($search, $replace, $text);
  }
  $result
    ->setProcessedText($text);
  return $result;
}