From e30f369339939748f91d40afbe99b95f40479858 Mon Sep 17 00:00:00 2001 From: pykettk Date: Sun, 23 Apr 2023 20:47:21 +0100 Subject: [PATCH 1/2] Handle Non-Page Builder CMS Block Creation --- Plugin/AddCmsBlockIdentifierToMarkup.php | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Plugin/AddCmsBlockIdentifierToMarkup.php b/Plugin/AddCmsBlockIdentifierToMarkup.php index db8a991..8afe36a 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 { + $block->setContent('' . "\n" . $content); + } } return [$block]; From 9deb4c35da8f258f40dde96482dfdc2a6932cce6 Mon Sep 17 00:00:00 2001 From: pykettk Date: Sun, 23 Apr 2023 20:51:13 +0100 Subject: [PATCH 2/2] Use HTML Comments Based on Admin Config --- Plugin/AddCmsBlockIdentifierToMarkup.php | 2 +- Scope/Config.php | 14 ++++++++++++++ etc/adminhtml/system.xml | 17 +++++++++++++++++ etc/config.xml | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Plugin/AddCmsBlockIdentifierToMarkup.php b/Plugin/AddCmsBlockIdentifierToMarkup.php index 8afe36a..21ec88b 100644 --- a/Plugin/AddCmsBlockIdentifierToMarkup.php +++ b/Plugin/AddCmsBlockIdentifierToMarkup.php @@ -54,7 +54,7 @@ public function beforeSave( 0 ) ); - } else { + } else if ($this->moduleConfig->useHtmlComments()) { $block->setContent('' . "\n" . $content); } } 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