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

Best method to add custom fields?


Go to End


1417 Views

Avatar
eightythree

Community Member, 12 Posts

18 April 2016 at 3:38am

Edited: 18/04/2016 3:40am

Hi there,
I was wondering which method to add custom fields in admin area is the best or maybe that depends on something?

Method 1:

function getCMSFields() {
  $fields = parent::getCMSFields();

  $fields->addFieldToTab('Root.Main', new TextField('MyTextField'));
  $fields->addFieldToTab('Root.Main', new TextareaField('MyTextareaField'));  
		
return $fields;

Method 2:

function getCMSFields() {
  $fields = parent::getCMSFields();

  $fields->addFieldToTab('Root.FleetPreview', TextField::create(‘MyTextField’));    
  $fields->addFieldToTab('Root.FleetPreview', TextareaField::create(‘MyTextareaField’));
		
return $fields;

Method 3:

function getCMSFields() {
  $fields = FieldList::create(

    TextField::create(‘MyTextField’),
    TextareaField::create(‘MyTextareaField’)

  );

return $fields;

What are the real differences besides syntax ?

Cheers,
Darek.