You are here

public function MappyFilter::process in Mappy 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/MappyFilter.php, line 24

Class

MappyFilter
Provides a filter to display any HTML as plain text.

Namespace

Drupal\mappy\Plugin\Filter

Code

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

  // First, we find all mappy tokens.
  $pattern = "/\\[mappy(\\:(.+))?( .+)?\\]/isU";
  preg_match_all($pattern, $text, $matches);

  // In founded tokens, we get parameters.
  foreach ($matches[0] as $ci => $token) {

    // This is the real magic of Hogwarts. I spent totaly about 5-6 hours
    // to make this pattern work.
    preg_match_all("/(\\s)+(\\w+):(((\\s)*(\\w+))|(\\'(?:\\.|[^\\'\\\\])*\\'))/i", str_replace("'", "'", $token), $parameters);

    // And write parameters to an array.
    $att = [];
    foreach ($parameters['2'] as $key => $name) {

      // Additional, we remove quotes.
      $att[$parameters['2'][$key]] = str_replace("'", "", $parameters['3'][$key]);
    }

    // Now we generate HTML Mappy tag.
    // Which service are using. Google or Yandex.
    $service = $matches[2][$ci];

    // Generate attributes.
    $attributes = "";
    foreach ($att as $key => $value) {
      $attributes .= " {$key}=\"{$value}\"";
    }

    // And add all to tag.
    // Without '<mappy:' because drupal remove colons from tag.
    $tag = "<{$service} {$attributes}></{$service}>";

    // Replace token for tag.
    $text = str_replace($token, $tag, $text);
  }
  $result
    ->setProcessedText($text);
  return $result;
}