You are here

public function InsertView::process in Insert View 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/InsertView.php \Drupal\insert_view\Plugin\Filter\InsertView::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/InsertView.php, line 26

Class

InsertView
Provides a filter for insert view.

Namespace

Drupal\insert_view\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if (!empty($text)) {
    $match = [];
    if (preg_match_all("/\\[view:([^=\\]]+)=?([^=\\]]+)?=?([^=\\]]+)?=?(?:limit:)?([\\d]+)?\\]/i", $text, $match)) {
      $search = $replace = [];
      foreach ($match[0] as $key => $value) {
        $view_name = $match[1][$key];
        $display_id = $match[2][$key] && !is_numeric($match[2][$key]) ? $match[2][$key] : 'default';
        $args = $match[3][$key];
        $limit = $match[4][$key] ?? '';

        // Let's create a placeholder from the renderable array of the view.
        $view_output = $result
          ->createPlaceholder('\\Drupal\\insert_view\\Plugin\\Filter\\InsertView::build', [
          $view_name,
          $display_id,
          $args,
          $limit,
        ]);

        // Populate the replace statement.
        $search[] = $value;
        $replace[] = !empty($view_output) ? $view_output : '';
      }
      $text = str_replace($search, $replace, $text);

      // Add some more caching options.
      $result
        ->setProcessedText($text)
        ->addCacheTags([
        'insert_view',
      ])
        ->addCacheContexts([
        'url',
        'user.permissions',
      ]);
    }
  }
  return $result;
}