Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Plugin/AddCmsBlockIdentifierToMarkup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<div') + 4,
0
)
);
$firstWord = strtok($content, ' ');

// if tags are removed from the first "word" and the values are not equal then we have HTML to edit
if (strip_tags($firstWord) !== $firstWord) {
$tag = explode('>', 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('<!-- CMS identifier = ' . $identifier . ' -->' . "\n" . $content);
}
}

return [$block];
Expand Down
14 changes: 14 additions & 0 deletions Scope/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}
}
17 changes: 17 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@
<field id="cms/pagebuilder/block_identifier_markup_enabled">1</field>
</depends>
</field>
<field id="block_identifier_comment"
translate="label comment"
type="select"
sortOrder="3020"
showInDefault="1"
showInWebsite="1"
showInStore="1"
canRestore="1">
<label>Use HTML Comments for non-HTML Content</label>
<comment>
A HTML comment will be used when the block content does not contain a wrapping HTML tag to edit.
</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="cms/pagebuilder/block_identifier_markup_enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<pagebuilder>
<block_identifier_markup_enabled>1</block_identifier_markup_enabled>
<block_identifier_attribute_name>block-identifier</block_identifier_attribute_name>
<block_identifier_comment>1</block_identifier_comment>
</pagebuilder>
</cms>
</default>
Expand Down