public function CodeMirrorEditor::process in The CodeMirror Editor 8
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/ CodeMirrorEditor.php, line 40
Class
- CodeMirrorEditor
- Provides a 'CodeMirror' filter.
Namespace
Drupal\codemirror_editor\Plugin\FilterCode
public function process($text, $langcode) {
$options = $this->settings;
$options['readOnly'] = TRUE;
$options['toolbar'] = FALSE;
$pattern = '#<code\\s+?data-mode\\s*?=\\s*?"([a-z0-9_/\\-]*?)"[^>]*?>(.*?)</\\s*code\\s*>#is';
$processor = function ($matches) use ($options) {
$options['mode'] = self::normalizeMode($matches[1]);
$options = Html::escape(json_encode($options));
$code = Html::escape($matches[2]);
return '<code data-codemirror="' . $options . '">' . $code . '</code>';
};
$output = preg_replace_callback($pattern, $processor, $text, -1, $count);
if ($count > 0) {
$build['#attached']['library'][] = 'codemirror_editor/formatter';
\Drupal::service('renderer')
->render($build);
}
return new FilterProcessResult($output);
}