/**
  * Upload Dir Rules message and fields to display in the CMS.
  *
  * @param bool $dirExists
  *
  * @return LiteralField|null
  */
 public function getAssetsFolderField($dirExists)
 {
     return AssetsFolderCmsFieldsHelper::assetsFolderField($this->owner, $dirExists);
 }
 /**
  * @param FieldList $fields
  * @return FieldList
  */
 public static function updateAssetsFolderCMSField($obj, FieldList $fields)
 {
     //prepopulating object with asset folder - if allowed
     if ($obj->AssetsFolderID == 0) {
         $url = $obj->assetsFolderUrlToBeWritten();
         if ($url) {
             //this creates the directory, and attaches it to the page,
             //as well as saving the object one more time - with the attached folder
             $obj->findOrMakeAssetsFolder($url, false);
         }
     }
     $dirName = $obj->getAssetsFolderDirName();
     $dirExists = false;
     if ($dirName) {
         $dirExists = true;
         //Setting and showing the uploads folder
         //This doesn't work for iframe uploads, there we need
         //the AssetsFolderAdmin extension to LeftAndMain
         Upload::config()->uploads_folder = $dirName;
         //Cookie fallback for moments where it's impossible to figure
         //out the uploads folder through the leftandmain controller.
         //e.g. ModelAdmin - {@see AssetsFolderAdmin}
         if (Cookie::get('cms-uploaddirrules-uploads-folder') != $dirName) {
             Cookie::set('cms-uploaddirrules-uploads-folder', $dirName);
         }
     }
     //The Upload Directory field
     //TODO make it configurable if field should be shown
     //TODO make field placement configurable
     $field = AssetsFolderCmsFieldsHelper::assetsFolderField($obj, $dirExists);
     $fields->removeByName('AssetsFolder');
     //Adding fields - to tab or just pushing
     $isPage = false;
     $ancestry = $obj->getClassAncestry();
     foreach ($ancestry as $c) {
         if ($c == 'SiteTree') {
             $isPage = true;
         }
     }
     if ($dirExists) {
         $field = ToggleCompositeField::create('UploadDirRulesNotes', 'Upload Rules (' . $dirName . ')', [$field]);
         //configurable tab
         $tab = $obj->config()->uploaddirrules_fieldtab;
         if (isset($tab)) {
             $fields->addFieldToTab($tab, $field);
         } else {
             if ($isPage) {
                 //$fields->addFieldToTab('Root.Main', $htmlField, 'Content');
                 $fields->addFieldToTab('Root.Main', $field);
             } else {
                 //TODO this should be configurable
                 switch ($obj->ClassName) {
                     case 'Subsite':
                         $fields->addFieldToTab('Root.Configuration', $field);
                         break;
                     case 'SiteConfig':
                     case 'GenericContentBlock':
                         $fields->addFieldToTab('Root.Main', $field);
                         break;
                     default:
                         if ($fields->fieldByName('Root.Main')) {
                             $fields->addFieldToTab('Root.Main', $field);
                         } else {
                             $fields->push($field);
                         }
                 }
             }
         }
     } else {
         $noteTab = new Tab('Note', 'Note', $field);
         $fields->insertBefore($noteTab, 'Main');
     }
     return $fields;
 }