class RemovePageThemeWrapperSubscriber in Acquia Content Hub 8
Subscriber to Remove Page Theme Wrapper.
@package Drupal\acquia_contenthub\EventSubscriber\View
Hierarchy
- class \Drupal\Core\EventSubscriber\MainContentViewSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\acquia_contenthub\EventSubscriber\View\RemovePageThemeWrapperSubscriber
Expanded class hierarchy of RemovePageThemeWrapperSubscriber
1 file declares its use of RemovePageThemeWrapperSubscriber
File
- src/
EventSubscriber/ View/ RemovePageThemeWrapperSubscriber.php, line 14
Namespace
Drupal\acquia_contenthub\EventSubscriber\ViewView source
class RemovePageThemeWrapperSubscriber 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 D8.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* Controller result event.
*/
public function onViewRenderArray(GetResponseForControllerResultEvent $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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MainContentViewSubscriber:: |
protected | property | The class resolver service. | |
MainContentViewSubscriber:: |
protected | property | The available main content renderer services, keyed per format. | |
MainContentViewSubscriber:: |
protected | property | The current route match. | |
MainContentViewSubscriber:: |
constant | URL query attribute to indicate the wrapper used to render a request. | ||
MainContentViewSubscriber:: |
public | function | Constructs a new MainContentViewSubscriber object. | |
RemovePageThemeWrapperSubscriber:: |
public static | function |
Interact with onViewRenderArray event. Overrides MainContentViewSubscriber:: |
|
RemovePageThemeWrapperSubscriber:: |
public | function |
Modifies render array for D8. Overrides MainContentViewSubscriber:: |
|
RemovePageThemeWrapperSubscriber:: |
private static | function | Removes surrounding markup for html string. |