Skip to content

Instantly share code, notes, and snippets.

@angelflo
Forked from kanduvisla/ElephantData.php
Created March 23, 2018 16:40
Show Gist options
  • Save angelflo/fd283a943f64419edf90db8bb3734848 to your computer and use it in GitHub Desktop.
Save angelflo/fd283a943f64419edf90db8bb3734848 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment