17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1770 Views |
-
adding function to Page_Controller from module

22 November 2008 at 11:08am Last edited: 22 November 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 -
Re: adding function to Page_Controller from module

22 November 2008 at 2:33pm
add your functions to the relevant page's Controllor.
then you can have it.
-
Re: adding function to Page_Controller from module

22 November 2008 at 3:04pm Last edited: 22 November 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
-
Re: adding function to Page_Controller from module

25 November 2008 at 6:32am
thanks a lot ajshort, nivanka.
i was looking for the method ajshort described. thanks!
-
Re: adding function to Page_Controller from module

25 November 2008 at 9:48am Last edited: 25 November 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();
-
Re: adding function to Page_Controller from module

25 November 2008 at 11:22am Last edited: 25 November 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: http://open.silverstripe.com/ticket/3129
| 1770 Views | ||
|
Page:
1
|
Go to Top |



