tabsfieldhiddensilverstripe

silverstripe 3 changeFieldOrder API makes menu Tabs vanish


Good afternoon,

I'm trying to change the ordering of fields within my DataObject as displayed by the CMS. I've been successful in changing the order after digging through the API. However, I've noticed that my Tabs have disappeared.

See code below:

class MyDbObj extends DataObject{
   public static $db = array(
        'Title' => 'Varchar',
        'Desc' => 'Text',
        'Weight' => 'Int',
        'Status' => "Enum('Enable, Disable', 'Disable')",
        'Help' => 'HTMLText',
   );

   private static $has_one = array(
        'FileUpload' => 'File'
   );

   private static $has_many = array(
        'Contacts' => 'Contact'
   );


   /**
    * \brief Interesting part here!!!
    *
    * Note: This works great, but it removes my Tabs.
    *
    * Example: This object in the CMS has a default Tab 'Main'
    * The has_many relationship creates another Tab 'Contacts'
    * 
    * Problem: After calling changeFieldOrder, the Tabs are all gone!
    * How to get them back? Thanks.
    */
   public function getCMSFields(){
       $fields = parent::getCMSFields();
       //The next line basically puts FileUpload before the Help (WYISWYG)
       $field_order = array('Title', 'Desc', 'Weight', 'Status', 'FileUpload', 'Help');
       $fields->changeFieldOrder($field_order); //Call to API
       return $fields;
   }
}//class

Note: I've even implemented the entire thing using the method shown in the link here: SilverStripe: changing the order of GridField input elements

and I'm still getting the same issue with the Tabs disappearing.

Thanks for your assistance.


Solution

  • It seems if you call $fields->changeFieldOrder($field_order) on a FieldList that contains a TabSet it will remove the tabs.

    When you have tabs what happens is your main FieldList contains a TabSet, which contains multiple Tab objects. Each Tab object contains its own FieldList.

    What you can do is get the FieldList from your Root.Main tab and call changeFieldOrder() on this FieldList:

    public function getCMSFields() {
        $fields = parent::getCMSFields();
    
        $mainFields = $fields;
        
        if ($fields->hasTabSet()) {
            if ($mainTab = $fields->fieldByName('Root.Main')) {
                $mainFields = $mainTab->Fields();
            }
        }
    
        $mainFields->changeFieldOrder([
            'Title',
            'Desc',
            'Weight',
            'Status',
            'FileUpload',
            'Help',
        ]);
        
        return $fields;
    }
    

    Alternatively you can set your fields manually giving you total control of the fields and tabs.