Skip to content

Commit 0f10db5

Browse files
committed
Handle Non-Page Builder CMS Block Creation
1 parent 09cd525 commit 0f10db5

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

Plugin/AddCmsBlockIdentifierToMarkup.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ public function beforeSave(
4040
&& ($identifier = $block->getIdentifier())
4141
&& ($content = $block->getContent())
4242
) {
43-
$block->setContent(
44-
substr_replace(
45-
$content,
46-
' data-' . $this->moduleConfig->getBlockIdentifierDataAttributeName() . '="' . $identifier . '"',
47-
strpos($content, '<div') + 4,
48-
0
49-
)
50-
);
43+
$firstWord = strtok($content, ' ');
44+
45+
// if tags are removed from the first "word" and the values are not equal then we have HTML to edit
46+
if (strip_tags($firstWord) !== $firstWord) {
47+
$tag = explode('>', explode('<', $content)[1])[0]; // parsing HTML with PHP is gross
48+
49+
$block->setContent(
50+
substr_replace(
51+
$content,
52+
' data-' . $this->moduleConfig->getBlockIdentifierDataAttributeName() . '="' . $identifier . '"',
53+
strpos($content, $tag) + strlen($tag),
54+
0
55+
)
56+
);
57+
} else {
58+
$block->setContent('<!-- CMS identifier = ' . $identifier . ' -->' . "\n" . $content);
59+
}
5160
}
5261

5362
return [$block];

0 commit comments

Comments
 (0)