Jump to:

5554 Posts in 1742 Topics by 1227 members

Customising the CMS

SilverStripe Forums » Customising the CMS » ModelAdmin, getCMSFields, getCMSFields_forPopup and updateCMSFields

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

Page: 1
Go to End
Author Topic: 1025 Views
  • azt3k
    Avatar
    Community Member
    9 Posts

    ModelAdmin, getCMSFields, getCMSFields_forPopup and updateCMSFields Link to this post

    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.

  • cuSSter
    Avatar
    Community Member
    56 Posts

    Re: ModelAdmin, getCMSFields, getCMSFields_forPopup and updateCMSFields Link to this post

    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.

  • azt3k
    Avatar
    Community Member
    9 Posts

    Re: ModelAdmin, getCMSFields, getCMSFields_forPopup and updateCMSFields Link to this post

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

    1025 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.