Skip to content

Instantly share code, notes, and snippets.

@kanduvisla
Forked from TommyKolkman/ElephantData.php
Last active January 21, 2021 04:17
Show Gist options
  • Save kanduvisla/d010b36639f79641bb76 to your computer and use it in GitHub Desktop.
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
<?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*|&nbsp;|\n)<\/p>/', '', $content);
return $content;
}
}
<?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>
<?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);
}
}
<?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);
}
}
@TommyKolkman
Copy link

Solved the problem of having a widget in a block for me Giel, thanks. ObserverBlock.php line 29 should read $this->_elephantData->processContent( $content ); though. Thanks man!

@sandy6666
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment