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.

Archive /

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

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

adding function to Page_Controller from module


Reply


6 Posts   2477 Views

Avatar
marcink

Community Member, 89 Posts

22 November 2008 at 11:08am

Edited: 22/11/2008 11:09am

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

Avatar
Nivanka

Community Member, 400 Posts

22 November 2008 at 2:33pm

add your functions to the relevant page's Controllor.

then you can have it.

Avatar
ajshort

Community Member, 244 Posts

22 November 2008 at 3:04pm

Edited: 22/11/2008 3:04pm

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

Avatar
marcink

Community Member, 89 Posts

25 November 2008 at 6:32am

thanks a lot ajshort, nivanka.

i was looking for the method ajshort described. thanks!

Avatar
marcink

Community Member, 89 Posts

25 November 2008 at 9:48am

Edited: 25/11/2008 9:52am

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();

Avatar
marcink

Community Member, 89 Posts

25 November 2008 at 11:22am

Edited: 25/11/2008 11:29am

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: [url]http://open.silverstripe.com/ticket/3129[/url]