Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » adding function to Page_Controller from module

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2160 Views
  • marcink
    Avatar
    Community Member
    89 Posts

    adding function to Page_Controller from module Link to this post

    hi,

    is it possible to add a function to Page_Controller when installing a module? - if yes, how?
    i mean, without manipulating the Page_Controller itself...

    after installing the module, i want to be able to call this function from every page type that extends page...

    thanks
    marcin

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: adding function to Page_Controller from module Link to this post

    add your functions to the relevant page's Controllor.

    then you can have it.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: adding function to Page_Controller from module Link to this post

    Nivanka, I think you misunderstood the question:

    You can add extra methods to any SilverStripe class (one that extends Object) with the use of Extensions. To this, all you need to do is create a class like this:

    <?php
    /**
    * Adds an extra method to a controlller
    */
    class ControllerExtension extends Extension {
       
       public function extra_method() {
          return 'It Works!';
       }
       
    }

    You then apply this extension to your class using this in your modules _config.php file:

    Object::add_extension('Page_Controller', 'ControllerExtension');

    For more information, see http://doc.silverstripe.com/doku.php?id=dataobjectdecorator - note that DataObjectDecorator is a subclass of Extensions designed for applying to DataObject's

  • marcink
    Avatar
    Community Member
    89 Posts

    Re: adding function to Page_Controller from module Link to this post

    thanks a lot ajshort, nivanka.

    i was looking for the method ajshort described. thanks!

  • marcink
    Avatar
    Community Member
    89 Posts

    Re: adding function to Page_Controller from module Link to this post

    ajshort:

    the tip with the decorator was just what i was looking for.
    execpt, it doesn't work...

    decorating the page controller went fine.

    but when i wanted to decorate the page data object, i run into errors.

    here's my PageDecorator class:

    class PageDecorator extends DataObjectDecorator {
       
       function extraDBFields() {
          return array(
             'has_many' => array(
                'FlashObjects' => 'FlashDataObject'
             )
          );
       }

    }

    this code i have in the _config.php:

    Object::add_extension('Page_Controller', 'PageControllerExtension');
    DataObject::add_extension('Page', array_push(Page::$extensions, 'PageDecorator'));


    and this errors i get, when i try to db/build?flush=1 my page:

    Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in D:\_projekte\xtreme-diving\silverstripe\sapphire\core\Object.php(86) : eval()'d code on line 1

    Fatal error: Call to a member function setOwner() on a non-object in D:\_projekte\xtreme-diving\silverstripe\sapphire\core\Object.php on line 87

    could you help me out with this?

    thanks!

    edit: in Page.php i added: static $has_many = array();

  • marcink
    Avatar
    Community Member
    89 Posts

    Re: adding function to Page_Controller from module Link to this post

    update:

    i got rid of that error writing:

    DataObject::add_extension('Page', 'PageDecorator');


    in the _config.php instead.

    it should work now. but after adding a new has_many relation and reloading, i get en error that is, i think, related to the ticket: http://open.silverstripe.com/ticket/3129

    2160 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.