diff --git a/Plugin/AddCmsBlockIdentifierToMarkup.php b/Plugin/AddCmsBlockIdentifierToMarkup.php index db8a991..21ec88b 100644 --- a/Plugin/AddCmsBlockIdentifierToMarkup.php +++ b/Plugin/AddCmsBlockIdentifierToMarkup.php @@ -40,14 +40,23 @@ public function beforeSave( && ($identifier = $block->getIdentifier()) && ($content = $block->getContent()) ) { - $block->setContent( - substr_replace( - $content, - ' data-' . $this->moduleConfig->getBlockIdentifierDataAttributeName() . '="' . $identifier . '"', - strpos($content, '', explode('<', $content)[1])[0]; // parsing HTML with PHP is gross + + $block->setContent( + substr_replace( + $content, + ' data-' . $this->moduleConfig->getBlockIdentifierDataAttributeName() . '="' . $identifier . '"', + strpos($content, $tag) + strlen($tag), + 0 + ) + ); + } else if ($this->moduleConfig->useHtmlComments()) { + $block->setContent('' . "\n" . $content); + } } return [$block]; diff --git a/Scope/Config.php b/Scope/Config.php index 7c8c9ba..5df6dbc 100644 --- a/Scope/Config.php +++ b/Scope/Config.php @@ -14,6 +14,7 @@ class Config { const XML_PATH_CMS_IDENTIFIER_MARKUP_ENABLED = 'cms/pagebuilder/block_identifier_markup_enabled'; const XML_PATH_CMS_IDENTIFIER_DATA_ATTR_NAME = 'cms/pagebuilder/block_identifier_attribute_name'; + const XML_PATH_CMS_IDENTIFIER_HTML_COMMENT = 'cms/pagebuilder/block_identifier_comment'; /** @var ScopeConfigInterface */ private ScopeConfigInterface $scopeConfig; @@ -52,4 +53,17 @@ public function getBlockIdentifierDataAttributeName($scopeCode = null): string $scopeCode ); } + + /** + * @param int|string|null $scopeCode + * @return bool + */ + public function useHtmlComments($scopeCode = null): bool + { + return $this->scopeConfig->isSetFlag( + self::XML_PATH_CMS_IDENTIFIER_HTML_COMMENT, + ScopeInterface::SCOPE_STORE, + $scopeCode + ); + } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 6bbc2a9..1121399 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -41,6 +41,23 @@ 1 + + + + A HTML comment will be used when the block content does not contain a wrapping HTML tag to edit. + + Magento\Config\Model\Config\Source\Yesno + + 1 + + diff --git a/etc/config.xml b/etc/config.xml index d8cfe34..32ccae8 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -12,6 +12,7 @@ 1 block-identifier + 1