1

Magento 2.4

Is it somehow possible to remove parts of the content of a CMS page before it is shown to the user? Is there a renderer I could overwrite?

Example: The content of my CMS page is this: hello world, what a nice day today!

Before it is shown to the user, I want to remove the "today", but not from the page itself, just when the page is being viewed.

Possible?

1 Answer 1

1

There will be several ways to do this, I am sharing here 2 methods which I am aware of

  • Replace the content in the server itself
  • Replace the content in the client

Method 1:

You can do this either by after plugin or by using observer here I am proving an ex using observer

Create a events.xml file to subscribe cms_page_render event. This event will be triggered after the cms content is generated.

<event name="cms_page_render">
    <observer name="your_module_cms" instance="Your\Module\Observer\AffectCmsPageRender" />
</event>

Create the Observer Model where you can modify the content as per your need

class AffectCmsPageRender implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        $page = $observer->getPage();
        $content = $page->getContent();
        /**
         * Modify Content as per your need
         */
        $page->setContent($content);
        return $this;
    }
}

Method 2:

Use JavaScript to modify the content before its visible to the user using require js 'domReady!'

1
  • I went with the observer method, works like a charm, BIG THANKS! Oct 27, 2022 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.