You are here

public function AceFilter::process in Ace Code Editor 8

Processing the filters and return the processed result.

Overrides FilterInterface::process

File

src/Plugin/Filter/AceFilter.php, line 115

Class

AceFilter
Filters implementation for Ace Editor.

Namespace

Drupal\ace_editor\Plugin\Filter

Code

public function process($text, $langcode) {
  $text = html_entity_decode($text);
  if (preg_match_all("/<ace.*?>(.*?)\\s*<\\/ace>/s", $text, $match)) {
    $js_settings = [
      'instances' => [],
      'theme_settings' => $this
        ->getConfiguration()['settings'],
    ];
    foreach ($match[0] as $key => $value) {
      $element_id = 'ace-editor-inline' . $key;
      $content = trim($match[1][$key], "\n\r\0\v");
      $replace = '<pre id="' . $element_id . '"></pre>';

      // Override settings with attributes on the tag.
      $settings = $this
        ->getConfiguration()['settings'];
      $attach_lib = [];
      foreach ($this
        ->tagAttributes('ace', $value) as $attribute_key => $attribute_value) {
        $settings[$attribute_key] = $attribute_value;
        if ($attribute_key == "theme" && \Drupal::service('library.discovery')
          ->getLibraryByName('ace_editor', 'theme.' . $attribute_value)) {
          $attach_lib[] = "ace_editor/theme." . $attribute_value;
        }
        if ($attribute_key == "syntax" && \Drupal::service('library.discovery')
          ->getLibraryByName('ace_editor', 'mode.' . $attribute_value)) {
          $attach_lib[] = "ace_editor/mode." . $attribute_value;
        }
      }
      $js_settings['instances'][] = [
        'id' => $element_id,
        'content' => $content,
        'settings' => $settings,
      ];
      $text = $this
        ->strReplaceOnce($value, $replace, $text);
    }
    $result = new FilterProcessResult($text);
    $attach_lib[] = 'ace_editor/filter';
    $result
      ->setAttachments([
      'library' => $attach_lib,
      'drupalSettings' => [
        // Pass settings variable ace_formatter to javascript.
        'ace_filter' => $js_settings,
      ],
    ]);
    return $result;
  }
  $result = new FilterProcessResult($text);
  return $result;
}