You are here

class RemovePageThemeWrapperSubscriberD9 in Acquia Content Hub 8

Subscriber to Remove Page Theme Wrapper.

@package Drupal\acquia_contenthub\EventSubscriber\View

Hierarchy

Expanded class hierarchy of RemovePageThemeWrapperSubscriberD9

1 file declares its use of RemovePageThemeWrapperSubscriberD9
AcquiaContentHubServiceProvider.php in src/AcquiaContentHubServiceProvider.php

File

src/EventSubscriber/View/RemovePageThemeWrapperSubscriberD9.php, line 14

Namespace

Drupal\acquia_contenthub\EventSubscriber\View
View source
class RemovePageThemeWrapperSubscriberD9 extends MainContentViewSubscriber {

  /**
   * Interact with onViewRenderArray event.
   *
   * @return array
   *   Subscribed events.
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[KernelEvents::VIEW][] = [
      'onViewRenderArray',
      10,
    ];
    return $events;
  }

  /**
   * Modifies render array for D9.
   *
   * @param \Symfony\Component\HttpKernel\Event\ViewEvent $event
   *   Controller result event.
   */
  public function onViewRenderArray(ViewEvent $event) {
    $request = $event
      ->getRequest();
    if ($request->attributes
      ->all()['_route'] === 'acquia_contenthub.content_entity_display.entity') {
      parent::onViewRenderArray($event);
      $event
        ->getResponse()
        ->setContent(self::removeSurroundingMarkup('dialog-off-canvas-main-canvas', $event
        ->getResponse()
        ->getContent()));
    }
  }

  /**
   * Removes surrounding markup for html string.
   *
   * @param \Drupal\acquia_contenthub\EventSubscriber\View\string $class_selector
   *   Selector for DOM modification.
   * @param \Drupal\acquia_contenthub\EventSubscriber\View\string $input
   *   HTML to modify.
   *
   * @return string
   *   Modified HTML.
   */
  private static function removeSurroundingMarkup(string $class_selector, string $input) : string {
    $doc = new \DOMDocument();
    $doc->preserveWhiteSpace = FALSE;
    $encoded_input = mb_convert_encoding($input, 'HTML-ENTITIES', "UTF-8");
    @$doc
      ->loadHTML($encoded_input);
    $entries = (new \DOMXPath($doc))
      ->query("//div[contains(concat(' ', normalize-space(@class), ' '), ' {$class_selector} ')]");
    foreach ($entries as $node) {
      if (!$node->parentNode) {
        continue;
      }
      $child_nodes = [];
      foreach ($node->childNodes as $ch_node) {
        $child_nodes[] = $ch_node;
      }
      foreach ($child_nodes as $ch_node) {
        $node->parentNode
          ->insertBefore($ch_node, $node);
      }
      $node->parentNode
        ->removeChild($node);
    }
    return $doc
      ->saveHTML();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MainContentViewSubscriber::$classResolver protected property The class resolver service.
MainContentViewSubscriber::$mainContentRenderers protected property The available main content renderer services, keyed per format.
MainContentViewSubscriber::$routeMatch protected property The current route match.
MainContentViewSubscriber::WRAPPER_FORMAT constant URL query attribute to indicate the wrapper used to render a request.
MainContentViewSubscriber::__construct public function Constructs a new MainContentViewSubscriber object.
RemovePageThemeWrapperSubscriberD9::getSubscribedEvents public static function Interact with onViewRenderArray event. Overrides MainContentViewSubscriber::getSubscribedEvents
RemovePageThemeWrapperSubscriberD9::onViewRenderArray public function Modifies render array for D9. Overrides MainContentViewSubscriber::onViewRenderArray
RemovePageThemeWrapperSubscriberD9::removeSurroundingMarkup private static function Removes surrounding markup for html string.