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   2329 Views

Avatar
marcink

22 November 2008 at 11:08am (Last edited: 22 November 2008 11:09am), Community Member, 89 Posts

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

22 November 2008 at 2:33pm Community Member, 395 Posts

add your functions to the relevant page's Controllor.

then you can have it.

Avatar
ajshort

22 November 2008 at 3:04pm (Last edited: 22 November 2008 3:04pm), Community Member, 244 Posts

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

25 November 2008 at 6:32am Community Member, 89 Posts

thanks a lot ajshort, nivanka.

i was looking for the method ajshort described. thanks!

Avatar
marcink

25 November 2008 at 9:48am (Last edited: 25 November 2008 9:52am), Community Member, 89 Posts

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

25 November 2008 at 11:22am (Last edited: 25 November 2008 11:29am), Community Member, 89 Posts

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]