You are here

protected function TooltipManager::addVocabularyReplacement in Tooltip Taxonomy 8

Add new taxonomy term pattern into the replacement array,.

That will be used to replace the field text with tooltip markup.

Parameters

array $vids: Vocabulary array.

array $pattern_map: Replacement pattern array.

Return value

int The number of taxonomy terms added.

1 call to TooltipManager::addVocabularyReplacement()
TooltipManager::addTooltip in src/Services/TooltipManager.php
Attach tooltip into a field text.

File

src/Services/TooltipManager.php, line 284

Class

TooltipManager
Tooltip filter condition manager class.

Namespace

Drupal\tooltip_taxonomy\Services

Code

protected function addVocabularyReplacement(array $vids, array &$pattern_map) {
  $count = 0;
  if (empty($vids)) {
    return 0;
  }
  foreach ($vids as $vid) {
    $term_tree = $this
      ->loadAllTaxonomyTerms($vid);
    if (!empty($term_tree)) {
      foreach ($term_tree as $term) {
        $des = strip_tags($term->description__value, "<b><i><strong><span><br><a><em>");
        if (empty($des)) {
          continue;
        }
        $tooltip_render = [
          '#theme' => 'tooltip_taxonomy',
          '#tooltip_id' => $term->vid . '-' . $term->tid,
          '#term_name' => $term->name,
          '#description' => $des,
        ];
        $name_pattern = '/\\b' . preg_quote($term->name, '/') . '\\b/';

        // Check if there is a same term name.
        $name_key = array_search($name_pattern, $pattern_map['search']);
        if ($name_key === FALSE) {

          // New term.
          $pattern_map['search'][] = $name_pattern;
          $pattern_map['replace'][] = $this->renderer
            ->renderPlain($tooltip_render);
        }
        else {

          // Overwrite existing term.
          // Conditions that has bigger weight value
          // will overwrite the same term name.
          $pattern_map['replace'][$name_key] = $this->renderer
            ->renderPlain($tooltip_render);
        }
        $count++;
      }
    }
  }
  return $count;
}