You are here

public function CustomFilterBaseFilter::process in Custom filter 8

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

Overrides FilterInterface::process

File

src/Plugin/Filter/CustomFilterBaseFilter.php, line 26

Class

CustomFilterBaseFilter
Provides a base filter for Custom Filter.

Namespace

Drupal\customfilter\Plugin\Filter

Code

public function process($text, $langcode) {

  // If the text passed is an empty string, then return it immediately.
  if (empty($text)) {
    return '';
  }
  $entity = CustomFilter::load($this->settings['id']);
  $globals =& static::getGlobals('push');
  $globals->text = $text;
  $rules = $entity
    ->getRulesTree();
  if (is_array($rules) && count($rules)) {

    // Reset the object containing the global variables.
    static::getCodeVars(TRUE);

    // Prepare the stack used to save the parent rule.
    $globals->stack = array();
    foreach ($rules as $rule) {
      if ($rule['enabled']) {
        $globals->stack[] = $rule;
        $globals->text = preg_replace_callback($rule['pattern'], [
          CustomFilterBaseFilter::class,
          'applyRules',
        ], $globals->text);
        array_pop($globals->stack);
      }
    }
  }
  $text = $globals->text;
  static::getGlobals('pop');
  $result = new FilterProcessResult($text);
  $cache_tags = array(
    'customfilter:' . $this->settings['id'],
  );
  $result
    ->addCacheTags($cache_tags);
  return $result;
}