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.

Data Model Questions /

removeByName not working in decorators


Reply


3 Posts   531 Views

Avatar
Lou

Community Member, 8 Posts

18 February 2013 at 9:55pm

Edited: 18/02/2013 10:09pm

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

Avatar
kinglozzer

Community Member, 151 Posts

18 February 2013 at 10:58pm

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

Avatar
Lou

Community Member, 8 Posts

18 February 2013 at 11:58pm

Edited: 19/02/2013 12:00am

Thanks, kinglozzer.

That did work!