-
-
Save kanduvisla/d010b36639f79641bb76 to your computer and use it in GitHub Desktop.
Observer to strip <p> tags for Magento 2 in pages and static blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Elephant\CFParent\Helper; | |
class ElephantData extends \Magento\Framework\App\Helper\AbstractHelper | |
{ | |
public function __construct( | |
\Magento\Framework\App\Helper\Context $context, | |
array $data = [] | |
) { | |
/** | |
* Data for further use in the widgets is collected here and can be instanced by injecting it in the child widget | |
* Example: \Elephant\CFParent\Helper\ElephantData $elephantData | |
**/ | |
parent::__construct( $context, $data ); | |
} | |
public function processContent($content) | |
{ | |
// Remove wrapping paragraphs around widgets: | |
$content = preg_replace('/\<p\>{{(.*?)}}\<\/p\>/', '{{$1}}', $content); | |
// Remove div around widgets | |
$content = preg_replace('/\<div\>{{(.*?)}}\<\/div\>/', '{{$1}}', $content); | |
// Remove empty paragraphs: | |
$content = preg_replace('/<p>(|\s*| |\n)<\/p>/', '', $content); | |
return $content; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | |
<event name="cms_page_render"> | |
<observer name="elephant_cfparent_controller_cms_page_render" instance="Elephant\CFParent\Model\ObserverPage" /> | |
</event> | |
<event name="cms_block_load_after"> | |
<observer name="elephant_cfparent_controller_cms_block_load_after" instance="Elephant\CFParent\Model\ObserverBlock" /> | |
</event> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Elephant\CFParent\Model; | |
use Magento\Framework\Event\ObserverInterface; | |
use Magento\Cms\Model\Block; | |
class ObserverBlock implements ObserverInterface { | |
/** | |
* @param Item $item | |
*/ | |
public function __construct( | |
\Elephant\CFParent\Helper\ElephantData $elephantData | |
) { | |
$this->_elephantData = $elephantData; | |
} | |
/** | |
* @override | |
* @see ObserverInterface::execute() | |
* @used-by \Magento\Framework\Event\Invoker\InvokerDefault::_callObserverMethod() | |
* @see \Magento\Framework\App\Action\Action::dispatch() | |
*/ | |
public function execute(\Magento\Framework\Event\Observer $observer) { | |
/** @var Block $block */ | |
$block = $observer->getObject(); | |
$content = $block->getContent(); | |
$content = $this->helper->removeWrappingParagraphs($content); | |
$block->setContent($content); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Elephant\CFParent\Model; | |
use Magento\Framework\Event\ObserverInterface; | |
class ObserverPage implements ObserverInterface { | |
/** | |
* @param Item $item | |
*/ | |
public function __construct( | |
\Elephant\CFParent\Helper\ElephantData $elephantData | |
) { | |
$this->_elephantData = $elephantData; | |
} | |
/** | |
* @override | |
* @see ObserverInterface::execute() | |
* @used-by \Magento\Framework\Event\Invoker\InvokerDefault::_callObserverMethod() | |
* @see \Magento\Framework\App\Action\Action::dispatch() | |
*/ | |
public function execute(\Magento\Framework\Event\Observer $observer) { | |
$page = $observer->getPage(); | |
$content = $page->getContent(); | |
$content = $this->_elephantData->processContent( $content ); | |
$page->setContent($content); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What can we do for graphql data, there also same data will be sent how to remove their and one more scenario is what if multiple widgets are there