Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

how to call hooks from my module file?


Reply


6 Posts   350 Views

Avatar
kammo

Community Member, 4 Posts

24 June 2013 at 10:19pm

Edited: 24/06/2013 10:22pm

Hi

i want to add 2 hooks onAfterPublish and onAfterUnpublish to my modules. basically i want to update my xml file based on page published and unpublished.

my config file include following code

Director::addRules(10, array(
   SiteMapModule::$siteMapURLSegment => 'SiteMapModule',
));


and

class SiteMapModule extends Page_Controller {

      public function onAfterPublish() {
         $this->Content();
         parent::onAfterPublish();
      }
      
      public function onAfterUnpublish() {
         $this->Content();
         parent::onAfterUnpublish();
      }
      
      public function Content() {
         #code
         #code
      }

}


but this is not working? can anybody help me how to do this?

Avatar
IOTI

Community Member, 189 Posts

25 June 2013 at 12:25am

I think you may have your extension setup incorrectly. I think you would want to set it up like so

class PageExtension extends DataExtension {
//your code
}

and in _config.php

Object::add_extension('Page','PageExtension');

If that doesn't work check out the doPublish() and doUnpublish() functions in SiteTree.php.

Avatar
kammo

Community Member, 4 Posts

28 June 2013 at 12:13am

Edited: 28/06/2013 12:14am

thanks for reply. but it dosen't work for me

my problem is i have sitemap module installed which generate sitemap.xml file along with sitemap page.

now i want to update sitemap.xml file on content publish,un-pubhlish,delete, etc. i have made changes to sitemap module so it can generate sitemap.xml file.

i have following in my config file.

Director::addRules(10, array(
   SiteMapModule::$siteMapURLSegment => 'SiteMapModule',
));

and in sitemap module file following code

class SiteMapModule extends Page_Controller {
#code
}

i want to extend class DataExtension so that i can use hooks.
will you please suggest me appropriate way to achieve this.

Avatar
IOTI

Community Member, 189 Posts

28 June 2013 at 12:46am

Hey Kammo

You don't want to extend Page_Controller. I did some more investigating and did find out that I was mistaken about which class to extend. You actually want to extend the SiteTree class, not Page (Although Page may have worked)

your extension should be like

class SiteMapModuleExtension extends DataExtension {
public function onAfterPublish() {
//your code
}

public function onAfterUnpublish() {
//your code
}
}

and in your _config.php

Object::add_extension('SiteTree','SiteMapModuleExtension');

Check out this module on github, [url=https://github.com/silverstripe-labs/silverstripe-googlesitemaps]silverstripe-googlesitemaps[/url]. It may help point you in the right direction.

Avatar
kammo

Community Member, 4 Posts

28 June 2013 at 1:48am

Edited: 28/06/2013 1:54am

HI again thanks for reply. but it's not working. i am attaching code if you can help me.
i got following error when i apply changes suggested by you.

Fatal error: Object::add_extension() - Can't find extension class for "SiteMapModuleExtension" and also googlesitemap not working with my version giving some error related to composer.js

Attached Files
Avatar
IOTI

Community Member, 189 Posts

28 June 2013 at 3:44am

To fix the "Fatal error: Object::add_extension()..." you'll have to do a ?flush=all.

Not sure why your getting an error related to composer.js but as you don't need to install the module I wouldn't bother installing it. Just examine the code on git or through your local code editor as it may assist you.