Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » how to call hooks from my module file?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 264 Views
  • kammo
    Avatar
    Community Member
    4 Posts

    how to call hooks from my module file? Link to this post

    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?

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: how to call hooks from my module file? Link to this post

    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.

  • kammo
    Avatar
    Community Member
    4 Posts

    Re: how to call hooks from my module file? Link to this post

    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.

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: how to call hooks from my module file? Link to this post

    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, silverstripe-googlesitemaps. It may help point you in the right direction.

  • kammo
    Avatar
    Community Member
    4 Posts

    Re: how to call hooks from my module file? Link to this post

    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
  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: how to call hooks from my module file? Link to this post

    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.

    264 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.