You are here

public function PagerPathSubscriber::modifyHtml in Tome 8

Reacts to a modify HTML event.

Parameters

\Drupal\tome_static\Event\ModifyHtmlEvent $event: The event.

File

modules/tome_static/src/EventSubscriber/PagerPathSubscriber.php, line 35

Class

PagerPathSubscriber
Converts pager query parameters to static paths.

Namespace

Drupal\tome_static\EventSubscriber

Code

public function modifyHtml(ModifyHtmlEvent $event) {
  $html = $event
    ->getHtml();
  $path = $event
    ->getPath();
  $document = new \DOMDocument();
  @$document
    ->loadHTML($html);
  $xpath = new \DOMXPath($document);

  /** @var \DOMElement $node */
  foreach ($xpath
    ->query('//a[(contains(@href,"?page=") or contains(@href,"&page="))]') as $node) {
    $original_href = $node
      ->getAttribute('href');
    if ($original_href[0] === '?') {
      $new_href = strtok($path, '?') . $original_href;
    }
    else {
      $new_href = $original_href;
    }
    $event
      ->addInvokePath($new_href);
    $new_href = $this
      ->modifyUrl($new_href);
    $event
      ->addExcludePath($new_href);
    $html = str_replace($original_href, $new_href, $html);
    $html = str_replace(htmlentities($original_href), $new_href, $html);
  }
  $event
    ->setHtml($html);
}