Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » removeByName not working in decorators

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

Page: 1
Go to End
Author Topic: 497 Views
  • Lou
    Avatar
    Community Member
    8 Posts

    removeByName not working in decorators Link to this post

    Hi,

    I'm using a global ArticlePage pagetype for multiple websites. I use a decorator for each site to tweak the functionality as needed.

    In some of the sites, some fields are not necessary so I have to remove them. But using $fields->removeByName('NameOfField') or $fields->removeFieldFromTab('Root.Content.Main','NameOfField') on the decorator doesn't seem to work.

    Is this a bug? I'm using 2.4.7.

    ArticlePageDecorator extends DataObjectDecorator {

       function updateCMSFields($fields){

          // not working
          $fields->removeByName('NameOfField');

          // oddly, this is working
          $fields->addFieldToTab('Root.Content.Main', new TextField('TestField'));
       }
    }

    :: I also found this 2-year-old page that looks to have the same issue: http://www.silverstripe.org/dataobjectmanager-module-forum/show/15402

  • kinglozzer
    Avatar
    Community Member
    139 Posts

    Re: removeByName not working in decorators Link to this post

    Hi Lou,

    Something similar came up on the IRC channel last week. In that case, it was because the code to call the extension is actually called before the field in question (the field to be removed) was added - it couldn't remove a field that didn't exist (yet).

    The solution was, at the bottom of the getCMSFields() function in Page.php, to add:

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

    Good luck

  • Lou
    Avatar
    Community Member
    8 Posts

    Re: removeByName not working in decorators Link to this post

    Thanks, kinglozzer.

    That did work!

    497 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.