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.

Customising the CMS /

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

ModelAdmin, getCMSFields, getCMSFields_forPopup and updateCMSFields


Go to End


3 Posts   2388 Views

Avatar
azt3k

Community Member, 9 Posts

13 June 2012 at 10:24am

Hi,

So I have a couple of DataObjects that have getCMSFields and getCMSFields_forPopup implemented on them that I am managing with ModelAdmin. This by in large works fine, however these objects are also decorated and the decorated fields don't display in either a popup version or model admin interface. The decorator is also applied to the Page class and updateCMSFields works fine in that context.

I have a work around which is as follows:

    public function getCMSFields() {
	
        $fields = new FieldSet();

	// ... Add fields to FieldSet
	
	// work around for model admin
	try{ $this->updateCMSFields($fields); }catch(Exception $e){ /* do nothing */ }		

        return $fields;
    }

This works, but it does mean that Decorator::updateCMSFields() gets called twice in the CMS context.

My question is really why is updateCMSFields not getting called in the ModelAdmin or getCMSFields_forPopup context but it is in the CMS context.

I had a look at the DataObject Class and the default getCMSFields includes this line at the end of the method:

$this->extend('updateCMSFields', $fields);

Because this method is overloaded on the child object, a bigger question is perhaps why does updateCMSFields get called at all in the CMS context?

If I use that line instead of the try catch block, then updateCMSFields gets called 6 times on the execution of the page rather than twice.

As a note it seems to make little difference how many times it gets called, the additional fields are only added once luckily.

Avatar
cuSSter

Community Member, 56 Posts

13 June 2012 at 8:53pm

I have not encountered such problem when decorating classes in SilverStripe. Just have to make sure that in your decorator class, you have defined updateCMSFields method to add the new fields in your decorated class.

Avatar
azt3k

Community Member, 9 Posts

19 June 2012 at 9:43am

Yeah its defined, just not called in the non-cms context - I have to manually call it for it to work.