You are here

public function jQueryUiFilter::process in jQuery UI filter 8.2

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/jQueryUiFilter.php, line 62
Contains \Drupal\jquery_ui_filter\Plugin\Filter\jQueryUiFilter.

Class

jQueryUiFilter
Provides a filter to generate jQuery UI accordion and tabs widgets.

Namespace

Drupal\jquery_ui_filter\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);

  // Track if widget has been found so that we can attached the
  // jquery_ui_filter library and settings.
  $has_widget = FALSE;
  foreach (self::$widgets as $name => $widget) {
    if (strpos($text, '[' . $name) === FALSE) {
      continue;
    }
    $has_widget = TRUE;

    // Remove block tags around tokens.
    $text = preg_replace('#<(p|div)[^>]*>\\s*(\\[/?' . $name . '[^]]*\\])\\s*</\\1>#', '\\2', $text);

    // Convert opening [token] to opening <div data-ui-*> tag.
    $text = preg_replace_callback('#\\[' . $name . '([^]]*)?\\]#is', function ($match) use ($name) {

      // Set data-ui-* attributes from role and options.
      $attributes = new Attribute([
        'data-ui-role' => $name,
      ]);
      $options = $this
        ->parseOptions($match[1]);
      foreach ($options as $name => $value) {
        $attributes
          ->setAttribute('data-ui-' . $name, $value);
      }
      return "<div{$attributes}>";
    }, $text);

    // Convert closing [/token] to closing </div> tag.
    $text = str_replace('[/' . $name . ']', '</div>', $text);
  }
  if ($has_widget) {
    $result
      ->setAttachments([
      'library' => [
        'jquery_ui_filter/jquery_ui_filter',
      ],
      'drupalSettings' => [
        'jquery_ui_filter' => \Drupal::config('jquery_ui_filter.settings')
          ->get(),
      ],
    ]);
    $result
      ->addCacheableDependency(\Drupal::config('jquery_ui_filter.settings'));
  }
  return $result
    ->setProcessedText($text);
}