public function CustomFilterBaseFilter::process in Custom filter 2.0.x
Same name and namespace in other branches
- 8 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\FilterCode
public function process($text, $langcode) {
// If the text passed is an empty string, then return it immediately.
if (empty($text)) {
return new FilterProcessResult();
}
/** @var \Drupal\customfilter\Entity\CustomFilter $entity */
$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 = [];
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);
$result
->addCacheTags($entity
->getCacheTagsToInvalidate());
if (!$entity
->getCache()) {
$result
->setCacheMaxAge(0);
}
return $result;
}