You are here

private static function RemovePageThemeWrapperSubscriber::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 RemovePageThemeWrapperSubscriber::removeSurroundingMarkup()
RemovePageThemeWrapperSubscriber::onViewRenderArray in src/EventSubscriber/View/RemovePageThemeWrapperSubscriber.php
Modifies render array for D8.

File

src/EventSubscriber/View/RemovePageThemeWrapperSubscriber.php, line 57

Class

RemovePageThemeWrapperSubscriber
Subscriber to Remove Page Theme Wrapper.

Namespace

Drupal\acquia_contenthub\EventSubscriber\View

Code

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();
}