private static function RemovePageThemeWrapperSubscriberD9::removeSurroundingMarkup in Acquia Content Hub 8
Removes surrounding markup for html string.
Parameters
\Drupal\acquia_contenthub\EventSubscriber\View\string $class_selector: Selector for DOM modification.
\Drupal\acquia_contenthub\EventSubscriber\View\string $input: HTML to modify.
Return value
string Modified HTML.
1 call to RemovePageThemeWrapperSubscriberD9::removeSurroundingMarkup()
- RemovePageThemeWrapperSubscriberD9::onViewRenderArray in src/
EventSubscriber/ View/ RemovePageThemeWrapperSubscriberD9.php - Modifies render array for D9.
File
- src/
EventSubscriber/ View/ RemovePageThemeWrapperSubscriberD9.php, line 57
Class
- RemovePageThemeWrapperSubscriberD9
- Subscriber to Remove Page Theme Wrapper.
Namespace
Drupal\acquia_contenthub\EventSubscriber\ViewCode
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();
}