From 432eacdd9bf698e5435b4ea79b20d65847628b8b Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Wed, 14 Nov 2018 11:45:58 -0600 Subject: [PATCH] REFACTOR header image getCMSFields --- src/Model/HeaderImage.php | 59 ++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/src/Model/HeaderImage.php b/src/Model/HeaderImage.php index 4059217..8861919 100644 --- a/src/Model/HeaderImage.php +++ b/src/Model/HeaderImage.php @@ -7,6 +7,7 @@ use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Assets\Image; use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\Forms\FieldList; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Security; @@ -46,35 +47,35 @@ class HeaderImage extends DataObject */ public function getCMSFields() { - $fields = parent::getCMSFields(); - - $fields->removeByName([ - 'HeaderLinkID', - 'Image', - 'PageID', - ]); - - $fields->insertAfter( - 'Content', - LinkField::create('HeaderLinkID', 'Link') - ); - - $image_field = UploadField::create('Image', 'Header Image') - ->setFolderName('Uploads/HeaderImages') - ->setIsMultiUpload(false); - $image_field->getValidator()->allowedExtensions = array( - 'jpg', - 'jpeg', - 'gif', - 'png', - ); - - $fields->insertBefore( - 'Title', - $image_field - ); - - return $fields; + $this->beforeUpdateCMSFields(function (FieldList $fields) { + $fields->removeByName([ + 'HeaderLinkID', + 'Image', + 'PageID', + ]); + + $fields->insertAfter( + 'Content', + LinkField::create('HeaderLinkID', 'Link') + ); + + $image_field = UploadField::create('Image', 'Header Image') + ->setFolderName('Uploads/HeaderImages') + ->setIsMultiUpload(false); + $image_field->getValidator()->allowedExtensions = [ + 'jpg', + 'jpeg', + 'gif', + 'png', + ]; + + $fields->insertBefore( + 'Title', + $image_field + ); + }); + + return parent::getCMSFields(); } /**