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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

DataObject never calling init of its ContentController?


Go to End


8 Posts   2325 Views

Avatar
moniuch

Community Member, 11 Posts

11 April 2014 at 8:13pm

Edited: 11/04/2014 8:23pm

I was wondering as per general rule. If we look at the below code (v.3.1.3):

class MySymbol extends DataObject {
    private static $db = array(
        'Symbol' => 'Varchar(32)',
    );
    public function getCMSFields() {
        Requirements::javascript('mysite/js/MySymbol_CMSEdit.js');
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', new DropdownField('Symbol', 'Symbol', array(
            'some' => 'data',
        )));
        return $fields;
    }
}
class MySymbol_Controller extends ContentController {
    public function init() {
        parent::init();
        Requirements::customScript('alert("hello")');
    }
}

Should the init() be called when MySymbol instance is either being edited in admin or displayed on final page?

What I wanted to do was to attach custom javascript on admin side that reacts to DropdownField selection. This works fine when used in getCMSFields(), but doesn't seem to be ever called by the controller, which - I read - was the canonical place to use Requirements::

I even went further and added line: $this->httpError(500) - this did not fire either. So is the controller used at all?

Would anyone shed some light on me, please?

Avatar
martimiz

Forum Moderator, 1391 Posts

13 April 2014 at 4:54am

A controller's init() function is fired when the controller is instantiated. The controller you created will not be instantiated automatically - that only applies to pagecontrollers - you'd have to take care of that yourself.

There are other ways, but the common way to load requirements into you website is to either add them to your template, or to your pagecontrollers init() function. If you want to load requirements into the admin section, you could use getCMSFields().

Avatar
HenryP

Community Member, 5 Posts

15 June 2015 at 3:23pm

I'm having this problem as well. There's seems to be no recommended way to instantiate a controller to help with rendering a DataObject or managing other tasks a controller should do.

Avatar
Pyromanik

Community Member, 419 Posts

16 June 2015 at 10:32pm

Edited: 16/06/2015 10:43pm

There is a recommended way: YourController::create()
You may also want to read up on: http://docs.silverstripe.org/en/3.1/developer_guides/controllers/routing/

Do you have an example as to what you mean by 'other tasks a controller should do'?
A DataObject does not need (nor should need) a controller to render it.

Avatar
HenryP

Community Member, 5 Posts

17 June 2015 at 7:04am

Thanks for those tips.
An example as per your question is:
I have a controller that can create a form - how ever I also want it to handle form submissions.
I did find that information regarding using Director to configure the routing to this controller.
And that works - to a point. However since my controller is not a descendant of SiteTree I can't get a link to the current page.
I'd ideally like to like to /<page containing form>/ThankYou
That's my current stumbling block.

FYI: My Director config was added to :
`mysite/_config/routes.yml`

---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
rules:
'/$AnyThing/SubmitContactForm' : 'ContactForm_Controller'

Avatar
Pyromanik

Community Member, 419 Posts

17 June 2015 at 9:10pm

Nothing in your given example needs nor uses a DataObject.
A controller that creates a form will already handle its own submission.
http://api.silverstripe.org/3.1/class-Controller.html#_Link - although this requires a route named as the controller's classname (used to be enabled by default). If you're needing something different, you'll need to override the method in your controller.

Perhaps sharing a link to your example's full code (just the relevant controller, routes, templates, etc.) could help us clear up some concepts for you. Because if you're doing what I think you are (judging by your small example)... I think this would be the easiest way to help you.

Avatar
HenryP

Community Member, 5 Posts

18 June 2015 at 8:31am

I understand what you're saying. But my example is incomplete. The requirement is to be able to add a form to a collection of page components in a grid field. I can see that adding a form in a straight page is a doddle.

In the end I've added the form creation and processing logic into my Page class. Not the most elegant solution, but it gets the job done. I can use my dataobject simply for placement of the form now.

I'm sure there's a better way - but this is working for now.

Thanks for your help.

Avatar
Pyromanik

Community Member, 419 Posts

18 June 2015 at 11:25am

It still seems like you're confusing concerns and/or perhaps not doing things in an optimal manner.
I'd like to try to help you understand, but I'll still need an example or something to work with.