You are here

class InlineLinkExtractor in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x src/LinkExtractor/InlineLinkExtractor.php \Drupal\printable\LinkExtractor\InlineLinkExtractor

Link extractor.

Hierarchy

Expanded class hierarchy of InlineLinkExtractor

1 string reference to 'InlineLinkExtractor'
printable.services.yml in ./printable.services.yml
printable.services.yml
1 service uses InlineLinkExtractor
printable.link_extractor in ./printable.services.yml
Drupal\printable\LinkExtractor\InlineLinkExtractor

File

src/LinkExtractor/InlineLinkExtractor.php, line 13

Namespace

Drupal\printable\LinkExtractor
View source
class InlineLinkExtractor implements LinkExtractorInterface {

  /**
   * The DomCrawler object.
   *
   * @var \Wa72\HtmlPageDom\HtmlPageCrawler
   */
  protected $crawler;

  /**
   * The url generator service.
   *
   * @var \Drupal\Core\Render\MetadataBubblingUrlGenerator
   */
  protected $urlGenerator;

  /**
   * The alias manager service.
   *
   * @var \Drupal\Core\Path\AliasManager
   */
  protected $aliasMnager;

  /**
   * Constructs a new InlineLinkExtractor object.
   */
  public function __construct(HtmlPageCrawler $crawler, MetadataBubblingUrlGenerator $urlGenerator, AliasManager $aliasMnager) {
    $this->crawler = $crawler;
    $this->urlGenerator = $urlGenerator;
    $this->aliasMnager = $aliasMnager;
  }

  /**
   * {@inheritdoc}
   */
  public function extract($string) {
    $this->crawler
      ->addContent($string);
    $this->crawler
      ->filter('a')
      ->each(function (HtmlPageCrawler $anchor, $uri) {
      $href = $anchor
        ->attr('href');
      if ($href) {
        $url = $this
          ->urlFromHref($href);
        $anchor
          ->append(' (' . $url
          ->toString() . ')');
      }
    });
    return (string) $this->crawler;
  }

  /**
   * {@inheritdoc}
   */
  public function removeAttribute($content, $attr) {
    $this->crawler
      ->addContent($content);
    $this->crawler
      ->filter('a')
      ->each(function (HtmlPageCrawler $anchor, $uri) {
      $anchor
        ->removeAttribute('href');
    });
    return (string) $this->crawler;
  }

  /**
   * {@inheritdoc}
   */
  public function listAttribute($content) {
    $this->crawler
      ->addContent($content);
    $this->links = [];
    $this->crawler
      ->filter('a')
      ->each(function (HtmlPageCrawler $anchor, $uri) {
      global $base_url;
      $href = $anchor
        ->attr('href');
      try {
        $this->links[] = $base_url . $this->aliasMnager
          ->getAliasByPath($href);
      } catch (\Exception $e) {
        $this->links[] = $this
          ->urlFromHref($href)
          ->toString();
      }
    });
    $this->crawler
      ->remove();
    return implode(',', $this->links);
  }

  /**
   * Generate a URL object given a URL from the href attribute.
   *
   * Tries external URLs first, if that fails it will attempt
   * generation from a relative URL.
   *
   * @param string $href
   *   The URL from the href attribute.
   *
   * @return \Drupal\Core\Url
   *   The created URL object.
   */
  private function urlFromHref($href) {
    try {
      $url = Url::fromUri($href, [
        'absolute' => TRUE,
      ]);
    } catch (\InvalidArgumentException $e) {
      $url = Url::fromUserInput($href, [
        'absolute' => TRUE,
      ]);
    }
    return $url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InlineLinkExtractor::$aliasMnager protected property The alias manager service.
InlineLinkExtractor::$crawler protected property The DomCrawler object.
InlineLinkExtractor::$urlGenerator protected property The url generator service.
InlineLinkExtractor::extract public function Highlight hrefs from links in the given HTML string. Overrides LinkExtractorInterface::extract
InlineLinkExtractor::listAttribute public function List the links at the bottom of page. Overrides LinkExtractorInterface::listAttribute
InlineLinkExtractor::removeAttribute public function Remove href from links in the given HTML string. Overrides LinkExtractorInterface::removeAttribute
InlineLinkExtractor::urlFromHref private function Generate a URL object given a URL from the href attribute.
InlineLinkExtractor::__construct public function Constructs a new InlineLinkExtractor object.