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.

Data Model Questions /

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

removeByName not working in decorators


Go to End


5 Posts   3822 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, 187 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!

Avatar
Jemmyyy

Community Member, 4 Posts

10 May 2016 at 12:33pm

Edited: 10/05/2016 2:22pm

Hey, I have got the same problem but on DataExtension;

On the module CarouselSlide.php:

 public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab(
			'Root.Main', 
			LinkField::create('LinkID', 'Link to page or file')
		);

But then I want to remove this and insert my own
CarouselExtension.php


<?php
class CarouselExtension extends DataExtension {

    public static $db = array(
        'Description' => 'Varchar(99)',
        'LinkTitle' => 'Varchar(20)',
    );

    public static $has_one = array(
        'LinkTo'    => 'SiteTree'
    );
  public function getCMSFields() {
	   $fields = parent::getCMSFields();
	   $this->extend('updateCMSFields', $fields);
	   return $fields;
	}
    public function updateCMSFields(FieldList $fields) {
        
       $fields->removeFieldFromTab('Root.Main','LinkID'); //THIS DOESN'T WORK 
        $fields->addFieldToTab('Root.Main', new TreeDropdownField('LinkToID', 'Page link to', 'SiteTree'));
    }
}

I do not have getCMSFields() on my Page.php as kinglozzer was suggesting.

Apparently, the UpdateCMSFields comes before the getCMSFields;..
Any help on this?

Avatar
martimiz

Forum Moderator, 1391 Posts

18 May 2016 at 7:16am

The getCMSFields() function in your DataExtension will never be called - functions in DataExtensions cannot override functions in their owner.

There being no getCMSFields() function in your Page.php is no problem, you can add it without problem to your Page class like this:

      public function getCMSFields() {
	   $fields = parent::getCMSFields();
	   $this->extend('updateCMSFields', $fields);
	   return $fields;
	}