protected function BlockComponentPreviewFormReplace::convertFormsToDiv in Dashboards with Layout Builder 8
Convert form tags to div when displayed in the Layout Builder UI form.
Parameters
array $content: The render array of the block.
1 call to BlockComponentPreviewFormReplace::convertFormsToDiv()
- BlockComponentPreviewFormReplace::onBuildRender in src/
EventSubscriber/ BlockComponentPreviewFormReplace.php - Change forms to divs if in layout builder's preview mode.
File
- src/
EventSubscriber/ BlockComponentPreviewFormReplace.php, line 78
Class
- BlockComponentPreviewFormReplace
- Replaces form elements with divs in layout builder previews.
Namespace
Drupal\dashboards\EventSubscriberCode
protected function convertFormsToDiv(array $content) {
// Render the block content early so we can parse the HTML.
// We keep an original copy around so we can return that if no forms are
// found. This allows us to only perform the early rendering when
// necessary.
$orginal_content = $content;
$markup = $this->renderer
->render($content);
$html = Html::load((string) $markup);
$forms = $html
->getElementsByTagName('form');
if ($forms->length) {
// Iteratve over each form and swap the form element with a div.
while ($form = $forms
->item($forms->length - 1)) {
$form_children = [];
foreach ($form->childNodes as $form_child) {
$form_children[] = $form_child;
}
$replacement_div = $html
->createElement('div');
foreach ($form_children as $form_child) {
$replacement_div
->appendChild($form_child);
}
foreach ($form->attributes as $form_attr_name => $form_attr) {
$replacement_div
->setAttribute($form_attr_name, $form_attr->nodeValue);
}
$form->parentNode
->replaceChild($replacement_div, $form);
}
$content['#markup'] = $html
->saveHTML($html
->getElementsByTagName('body')
->item(0));
return $content;
}
else {
return $orginal_content;
}
}