You are here

public function LinkExtractorService::isLinkExists in Link checker 8

Checks if link was not removed from content.

If link becomes blacklisted this method will return false.

Parameters

\Drupal\linkchecker\LinkCheckerLinkInterface $link: Link to check.

Return value

bool TRUE if link exists in a content.

File

src/LinkExtractorService.php, line 286

Class

LinkExtractorService
Class LinkExtractor.

Namespace

Drupal\linkchecker

Code

public function isLinkExists(LinkCheckerLinkInterface $link) {
  $entity = $link
    ->getParentEntity();

  // If entity was removed.
  if (!isset($entity)) {
    return FALSE;
  }
  if ($entity instanceof TranslatableInterface) {
    if ($entity
      ->hasTranslation($link
      ->getParentEntityLangcode())) {
      $entity = $entity
        ->getTranslation($link
        ->getParentEntityLangcode());
    }
    else {
      return FALSE;
    }
  }

  // If field was removed - FALSE.
  if (!$entity
    ->hasField($link
    ->getParentEntityFieldName())) {
    return FALSE;
  }
  $links = $this
    ->extractFromField($entity
    ->get($link
    ->getParentEntityFieldName()));
  foreach ($links as $extractedLink) {
    if (LinkCheckerLink::generateHash($extractedLink
      ->getUrl()) == $link
      ->getHash()) {
      return TRUE;
    }
  }

  // Link was removed from content.
  return FALSE;
}