You are here

public function InsertView::process in Advanced Insert View 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Filter/InsertView.php \Drupal\insert_view_adv\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 33

Class

InsertView
Provides a filter for insert view.

Namespace

Drupal\insert_view_adv\Plugin\Filter

Code

public function process($text, $langcode) {
  $matches = [];

  // Encode configuration to path to build method because lazy_loader only works with scalar arguments
  $encoded_configuration = Json::encode($this
    ->getConfiguration());
  $result = new FilterProcessResult($text);

  // Check first the direct input of shortcode.
  $count = preg_match_all("/\\[view:([^=\\]]+)=?([^=\\]]+)?=?([^\\]]*)?\\]/i", $text, $matches);

  // Keep track of the number of times a view was inserted.
  $insert_view_count = 0;
  if ($count) {
    $search = $replace = [];
    foreach ($matches[0] as $key => $value) {
      $view_name = $matches[1][$key];
      $display_id = $matches[2][$key] && !is_numeric($matches[2][$key]) ? $matches[2][$key] : 'default';
      $args = $matches[3][$key];

      // Do not allow arguments if they are forbidden to input.
      if (!empty($this->settings['hide_argument_input'])) {
        $args = '';
      }
      $view_output = $result
        ->createPlaceholder('\\Drupal\\insert_view_adv\\Plugin\\Filter\\InsertView::build', [
        $view_name,
        $display_id,
        $args,
        $encoded_configuration,
      ]);
      $search[] = $value;
      $replace[] = $view_output;
    }
    $text = str_replace($search, $replace, $text, $insert_view_count);
  }

  // Check the view inserted from the CKeditor plugin.
  $count = preg_match_all('/(<p>)?(?<json>{(?=.*inserted_view_adv\\b)(?=.*arguments\\b)(.*)})(<\\/p>)?/', $text, $matches);
  if ($count) {
    $search = $replace = [];
    foreach ($matches['json'] as $key => $value) {
      $inserted = Json::decode($value);
      if (!is_array($inserted) || empty($inserted)) {
        continue;
      }
      $view_parts = explode('=', $inserted['inserted_view_adv']);
      if (empty($view_parts)) {
        continue;
      }
      $view_name = $view_parts[0];
      $display_id = $view_parts[1] && !is_numeric($view_parts[1]) ? $view_parts[1] : 'default';
      $args = '';
      if (!empty($inserted['arguments']) && empty($this->settings['hide_argument_input'])) {
        $args = implode('/', $inserted['arguments']);
      }
      $view_output = $result
        ->createPlaceholder('\\Drupal\\insert_view_adv\\Plugin\\Filter\\InsertView::build', [
        $view_name,
        $display_id,
        $args,
        $encoded_configuration,
      ]);
      $search[] = $value;
      $replace[] = $view_output;
    }
    $text = str_replace($search, $replace, $text, $insert_view_count);
  }

  // If views were actually inserted, then update the processed text and add
  // cache tags and contexts. This check is important because cache tags and
  // contexts may be incorrectly added to a render array and cause
  // unnecessary cache variations.
  if ($insert_view_count > 0) {
    $result
      ->setProcessedText($text)
      ->addCacheTags([
      'insert_view_adv',
    ])
      ->addCacheContexts([
      'url',
      'user.permissions',
    ]);
  }
  return $result;
}