You are here

public function ViewsEmbedFilter::process in Views entity embed 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Filter/ViewsEmbedFilter.php \Drupal\views_entity_embed\Plugin\Filter\ViewsEmbedFilter::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/ViewsEmbedFilter.php, line 68

Class

ViewsEmbedFilter
Provides a filter to display embedded entities based on data attributes.

Namespace

Drupal\views_entity_embed\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if (strpos($text, 'data-view-name') !== FALSE && strpos($text, 'data-view-display') !== FALSE) {
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    foreach ($xpath
      ->query('//drupal-views[@data-view-name and @data-view-display]') as $node) {
      $render_view = '';
      try {
        $build = $this
          ->buildViewsEmbed($node);
        $render_view = $this->renderer
          ->executeInRenderContext(new RenderContext(), function () use (&$build) {
          return $this->renderer
            ->render($build);
        });
        $result = $result
          ->merge(BubbleableMetadata::createFromRenderArray($build));
      } catch (\Exception $e) {
        throw new EntityNotFoundException(sprintf('Unable to load embedded %s view with %s display.', $view_name, $view_display));
      }
      $this
        ->replaceNodeContent($node, $render_view);
    }
    $result
      ->setProcessedText(Html::serialize($dom));
  }
  return $result;
}