You are here

public function TooltipManager::addTooltip in Tooltip Taxonomy 8

Attach tooltip into a field text.

Parameters

string $view_mode: View mode of the field.

\Drupal\Core\Entity\EntityInterface $entity: The entity type.

string $field_name: The field machine name.

array $field_value: The field value.

array $tag: Cache tags.

Return value

string New text with tooltip markup.

File

src/Services/TooltipManager.php, line 164

Class

TooltipManager
Tooltip filter condition manager class.

Namespace

Drupal\tooltip_taxonomy\Services

Code

public function addTooltip(string $view_mode, EntityInterface $entity, string $field_name, array $field_value, array &$tag) {
  $match_cons = $this
    ->checkPathAndContentType($entity);

  // Taxonomy terms replacement array.
  $pattern_map = [];

  // Search keywords.
  $pattern_map['search'] = [];

  // Replace array.
  $pattern_map['replace'] = [];
  foreach ($match_cons as $con) {
    $allowed_views = $con
      ->get('view');

    // Text formats this condition applied to.
    $formats = $con
      ->get('formats');

    // Only generate tooltip for certain text formats.
    if (!in_array($field_value['#format'], $formats)) {
      continue;
    }
    if (!empty($allowed_views)) {
      $not_all = FALSE;
      foreach ($allowed_views as $mode) {
        if ($mode !== '0') {
          $not_all = TRUE;
          break;
        }
      }
      if ($not_all && !in_array($view_mode, $allowed_views)) {

        // The current view mode doesn't match this condition.
        continue;
      }
    }
    $allowed_fields = $con
      ->get('field');

    // Is there any selected field within this condition?
    if (!empty($allowed_fields)) {
      $field_key = $entity
        ->getEntityTypeId() . '-' . $field_name;

      // Check if the field is a selected field in this condition.
      if (in_array($field_key, $allowed_fields)) {

        // Add the taxonomy terms from this condition into the pattern array.
        $this
          ->addVocabularyReplacement($con
          ->get('vids'), $pattern_map);

        // Cache tag.
        $tag[] = 'tooltip_taxonomy:' . $con
          ->id();
      }
    }
    else {

      // Add the taxonomy terms from this condition into the pattern array.
      $this
        ->addVocabularyReplacement($con
        ->get('vids'), $pattern_map);

      // Cache tag.
      $tag[] = 'tooltip_taxonomy:' . $con
        ->id();
    }
  }

  // Replace the taxonomy terms with tooltip markup.
  $pattern_check = $pattern_map['search'];
  foreach ($pattern_check as $k => $check) {
    if (!preg_match($check, $field_value['#text'])) {
      unset($pattern_check[$k]);
    }
  }
  if (count($pattern_check) > 0) {

    // patch to remove tooltip if already exist in other MATCHING tooltips
    foreach ($pattern_check as $small_key => $small) {
      foreach ($pattern_check as $big_key => $big) {
        if ($small_key == $big_key) {
          continue;
        }
        $big = preg_replace('(^\\/\\\\b|\\\\b\\/$)', '', $big);

        // remove tooltip if duplicated
        if (preg_match($small, $big) === 1) {
          unset($pattern_map['search'][$small_key]);
          unset($pattern_map['replace'][$small_key]);
        }
      }
    }
    $result = $this
      ->replaceContent($field_value['#text'], $pattern_map);
  }
  else {
    return '';
  }
  return $result;
}