You are here

public function FilterOnomasticon::replaceTerms in Onomasticon 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Filter/FilterOnomasticon.php \Drupal\onomasticon\Plugin\Filter\FilterOnomasticon::replaceTerms()

This is the actual filter function.

Parameters

$text String containing a #text DOMNode value.:

Return value

string Processed string.

1 call to FilterOnomasticon::replaceTerms()
FilterOnomasticon::processChildren in src/Plugin/Filter/FilterOnomasticon.php
Traverses the DOM tree (recursive). If current DOMNode element has children, this function calls itself. #text nodes never have any children, there Onomasticon filter is applied.

File

src/Plugin/Filter/FilterOnomasticon.php, line 261

Class

FilterOnomasticon
Plugin annotation @Filter( id = "filter_onomasticon", title = @Translation("Onomasticon Filter"), description = @Translation("Adds glossary information to words."), type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE, settings…

Namespace

Drupal\onomasticon\Plugin\Filter

Code

public function replaceTerms($text) {
  if ($this->settings['onomasticon_repetition']) {
    $preg_limit = 1;
  }
  else {
    $preg_limit = -1;
  }

  // Cycle through terms and search for occurrence.
  $replacements = array();
  $language = \Drupal::languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  $terms = $this
    ->getTaxonomyTerms();
  if (is_array($terms) && count($terms) > 0) {
    foreach ($this
      ->getTaxonomyTerms() as $term) {
      if ($this->settings['onomasticon_repetition'] && array_key_exists($term
        ->id(), $this->termCache)) {
        continue;
      }
      if (!$term
        ->hasTranslation($language)) {
        continue;
      }
      $term = $term
        ->getTranslation($language);
      $term_name = $term
        ->label();
      if ($this->settings['onomasticon_ignorecase']) {

        // Get position of term in text independent of case.
        $pos = strpos(strtolower($text), strtolower($term_name));
      }
      else {

        // Get position of term in text.
        $pos = strpos($text, $term_name);

        // If not found try capitalized (e.g. beginning of sentence).
        if ($pos === FALSE) {
          $pos = strpos($text, ucfirst($term_name));
        }
      }
      if ($pos === FALSE) {
        continue;
      }

      // Set the correct cased needle.
      $needle = substr($text, $pos, strlen($term_name));
      if (!array_key_exists($term
        ->id(), $this->termCache)) {
        $this->termCache[$term
          ->id()] = TRUE;
      }
      $description = $term
        ->getDescription();

      // Set the implementation method.
      $implement = $this->settings['onomasticon_implement'];
      if ($implement == 'attr_title') {
        $description = strip_tags($description);

        // Title attribute is enclosed in double quotes,
        // we need to escape double quotes in description.
        // TODO: Instead of removing double quotes altogether, escape them.
        $description = str_replace('"', '', $description);

        // Replace no-breaking spaces with normal ones.
        $description = str_replace(' ', ' ', $description);

        // Trim multiple white-space characters with single space.
        $description = preg_replace('/\\s+/m', ' ', $description);
      }

      // Get the aliased term path
      $aliasManager = \Drupal::service('path_alias.manager');
      $termpath = $aliasManager
        ->getAliasByPath('/taxonomy/term/' . $term
        ->id());
      $onomasticon = [
        '#theme' => 'onomasticon',
        '#tag' => $this->settings['onomasticon_tag'],
        '#needle' => $needle,
        '#description' => $description,
        '#implement' => $implement,
        '#orientation' => $this->settings['onomasticon_orientation'],
        '#cursor' => $this->settings['onomasticon_cursor'],
        '#termlink' => $this->settings['onomasticon_termlink'],
        '#termpath' => $termpath,
      ];
      $placeholder = '###' . $term
        ->id() . '###';
      $replacements[$placeholder] = trim(\Drupal::service('renderer')
        ->render($onomasticon));
      $text = preg_replace("/(?<![a-zA-Z0-9_äöüÄÖÜ])" . preg_quote($needle, '/') . "(?![a-zA-Z0-9_äöüÄÖÜ])/", $placeholder, $text, $preg_limit);
    }
    foreach ($replacements as $placeholder => $replacement) {
      $text = str_replace($placeholder, $replacement, $text, $preg_limit);
    }
  }
  return $text;
}