You are here

class MediaOembedPathSubscriber in Tome 8

Converts Media OEmbed query parameters to static paths.

@internal

Hierarchy

  • class \Drupal\tome_static\EventSubscriber\MediaOembedPathSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MediaOembedPathSubscriber

1 string reference to 'MediaOembedPathSubscriber'
tome_static.services.yml in modules/tome_static/tome_static.services.yml
modules/tome_static/tome_static.services.yml
1 service uses MediaOembedPathSubscriber
tome_static.media_oembed_path_subscriber in modules/tome_static/tome_static.services.yml
Drupal\tome_static\EventSubscriber\MediaOembedPathSubscriber

File

modules/tome_static/src/EventSubscriber/MediaOembedPathSubscriber.php, line 15

Namespace

Drupal\tome_static\EventSubscriber
View source
class MediaOembedPathSubscriber implements EventSubscriberInterface {

  /**
   * Reacts to a modify destination event.
   *
   * @param \Drupal\tome_static\Event\ModifyDestinationEvent $event
   *   The event.
   */
  public function modifyDestination(ModifyDestinationEvent $event) {
    $destination = $event
      ->getDestination();
    $destination = $this
      ->modifyUrl($destination);
    $event
      ->setDestination($destination);
  }

  /**
   * Reacts to a modify HTML event.
   *
   * @param \Drupal\tome_static\Event\ModifyHtmlEvent $event
   *   The event.
   */
  public function modifyHtml(ModifyHtmlEvent $event) {
    $html = $event
      ->getHtml();
    $document = new \DOMDocument();
    @$document
      ->loadHTML($html);
    $xpath = new \DOMXPath($document);

    /** @var \DOMElement $node */
    foreach ($xpath
      ->query('//iframe[contains(@src,"/media/oembed?url=")]') as $node) {
      $original_src = $node
        ->getAttribute('src');
      $new_src = $this
        ->modifyUrl($original_src);
      $event
        ->addInvokePath($original_src);
      $event
        ->addExcludePath($new_src);
      $html = str_replace($original_src, $new_src, $html);
      $html = str_replace(htmlentities($original_src), $new_src, $html);
    }
    $event
      ->setHtml($html);
  }

  /**
   * Modifies a URL to replace pager query parameters with paths.
   *
   * @param string $url
   *   A URL.
   *
   * @return string
   *   The modified URL.
   */
  protected function modifyUrl($url) {
    $query = parse_url($url, PHP_URL_QUERY);
    if (!empty($query) && preg_match('|.*/media/oembed\\?.*|', $url)) {
      $base_path = preg_replace('/\\?.*/', '', $url);
      $url = "{$base_path}/" . md5(urldecode($query));
    }
    return $url;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[TomeStaticEvents::MODIFY_DESTINATION][] = [
      'modifyDestination',
    ];
    $events[TomeStaticEvents::MODIFY_HTML][] = [
      'modifyHtml',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaOembedPathSubscriber::getSubscribedEvents public static function
MediaOembedPathSubscriber::modifyDestination public function Reacts to a modify destination event.
MediaOembedPathSubscriber::modifyHtml public function Reacts to a modify HTML event.
MediaOembedPathSubscriber::modifyUrl protected function Modifies a URL to replace pager query parameters with paths.