function onBeforeWrite() { // Change MenuTitle, so date appears in CMS SiteTree $this->MenuTitle = $this->Date . ": " . $this->Title; // Move to News holder if created under something else if ($this->Parent()->ClassName != "NewsHolder") { $this->ParentID = NewsHolder::get()->first()->ID; } // Add Today's Date if None if (!$this->Date) { $this->Date = date('Y-m-d'); } parent::onBeforeWrite(); }
/** * getCMSFields * @return FieldList **/ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->dataFieldByName('Title')->setTitle('Article Title'); $fields->dataFieldByName('Content')->setTitle('Article Content'); $config = $this->config(); // publish date $fields->addFieldToTab('Root.Main', DateField::create('PublishDate')->setAttribute('placeholder', $this->dbObject('Created')->Format('M d, Y')), 'Content'); // tags if ($config->enable_tags) { $tagSource = function () { return NewsTag::get()->map()->toArray(); }; $fields->addFieldToTab('Root.Main', ListboxField::create('Tags', 'Tags', $tagSource(), null, null, true)->useAddnew('NewsTag', $tagSource), 'Content'); } // author if ($config->author_mode == 'string') { $fields->addFieldToTab('Root.Main', TextField::create('Author', 'Author'), 'Content'); } if ($config->author_mode == 'object') { $authorSource = function () { return NewsAuthor::get()->map('ID', 'Name')->toArray(); }; $fields->addFieldToTab('Root.Main', DropdownField::create('NewsAuthorID', 'Author', $authorSource())->useAddNew('NewsAuthor', $authorSource)->setHasEmptyDefault(true), 'Content'); } // featured if ($config->enable_featured_articles) { $fields->addFieldToTab('Root.Main', CheckboxField::create('Featured', _t('NewsArticle.FEATURED', 'Feature this article')), 'Content'); } // images if ($config->enable_images) { $fields->addFieldToTab('Root.FilesAndImages', UploadField::create('Image')->setAllowedFileCategories('image')->setAllowedMaxFileNumber(1)->setFolderName($config->get('image_folder'))); } // attachments if ($config->enable_attachments) { $fields->addFieldToTab('Root.FilesAndImages', UploadField::create('Attachment')->setAllowedFileCategories('doc')->setAllowedMaxFileNumber(1)->setFolderName($config->get('attachment_folder'))); } // summary if ($config->enable_summary) { $fields->addFieldToTab('Root.Main', HTMLEditorField::create('Summary', 'Article Summary'), 'Content'); } // parent $holders = NewsHolder::get(); if ($holders->count() > 1) { $fields->addFieldToTab('Root.Main', DropdownField::create('ParentID', 'News Section', $holders->map()->toArray()), 'Title'); } else { $fields->addFieldToTab('Root.Main', HiddenField::create('ParentID', 'News Section', $holders->first()->ID), 'Title'); } $this->extend('updateArticleCMSFields', $fields); return $fields; }
/** * Returns a list of sub news sections, if available * * @return DataObjectSet */ public function SubSections($allChildren = true) { $subs = null; $childHolders = NewsHolder::get()->filter('ParentID', $this->ID); if ($childHolders && $childHolders->count()) { $subs = new ArrayList(); foreach ($childHolders as $holder) { $subs->push($holder); if ($allChildren === true) { // see if there's any children to include $subSub = $holder->SubSections(); if ($subSub) { $subs->merge($subSub); } } } } return $subs; }