0

I have a custom observer to trigger some code when a page is saved/created. It's working great when I save a page - but it cannot see the new page when I create one - it is running the code but the getPage() is not returning a page within the observer.

Should it work that way or have I misunderstood - the code is clearly running the observer but as I say get Page isn't returning the new page?

Here is the events.xml file for the module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="cms_page_prepare_save">
        <observer name="test_name" instance="BlackRhino\BlackForest\Observer\Pagesaveafter" />
    </event>
</config>

Here is the Pagesaveafter.php

<?php

namespace BlackRhino\BlackForest\Observer;

use Magento\Framework\Event\ObserverInterface;

class Pagesaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_page = $observer->getPage();  // you will get page object
        
        fopen("page-".$_page->getId().".txt","w+");
        

    }   
}

When I create a new page - page-.txt is modified - but when I resave the page - then page-id.txt is created.

Any help would be much appriciated.

Is it because I could be using a better event than "cms_page_prepare_save" - I couldn't see one that stands out on this list: https://meetanshi.com/blog/magento-2-events/

1
  • I also have similar implementations for Products and Categories - interestingly Products do not have the same outcome but Categories do, and the observer for that also cannot load the new category when created to retreive the id. Mar 31, 2021 at 8:07

1 Answer 1

1

I would suggest to switch to the cms_page_save_before event and retrieve the data as $observer->getDataObject();

LE:

Change the event to cms_page_save_after if you need to retrieve the page's ID.

4
  • Hi @diana-botean - is it as simple as decalring cms_page_save_before instead of cms_page_prepare_save in the events.xml and then replacing code$_page = $observer->getPage(); with $_page = $observer->getDataObject(); because I get the same outcome - it saves but no pageId is gained unless your saving a page rather than creating. Apr 18, 2021 at 10:14
  • well yes, your initial post mentioned the fact that you aren't able to retrieve the page object. the 'cms_page_save_before' event allows you to change the object before it gets saved in the database, so obviously, you don't have the ID just yet. so what exactly are you looking for to do?
    – Diana
    Apr 18, 2021 at 11:39
  • hI @diana-botean I want to be able to create a text file called: "page-".$_page->getId().".txt" via an observer which will work for both existing and new pages. Apr 18, 2021 at 13:38
  • roger that, please check the update in my answer.
    – Diana
    Apr 18, 2021 at 15:28

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.