From f94fb8ae559a4ca47880a51725b90cf5347acf79 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Mon, 27 Aug 2018 15:01:25 -0500 Subject: [PATCH] refactor - SlideImage - move CMS fields to onBeforeUpdateCMSFields() (#141) --- src/SlideImage.php | 102 +++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/src/SlideImage.php b/src/SlideImage.php index f107e64..eb6c71e 100644 --- a/src/SlideImage.php +++ b/src/SlideImage.php @@ -102,57 +102,59 @@ class SlideImage extends DataObject implements PermissionProvider */ public function getCMSFields() { - $fields = parent::getCMSFields(); - - $fields->removeByName([ - 'ShowSlide', - 'SortOrder', - 'PageID', - 'Image', - ]); - - // Name - $fields->dataFieldByName('Name') - ->setTitle( - _t(__CLASS__ . '.NAME', 'Name') - ) - ->setDescription( - _t(__CLASS__ . '.INTERNAL_USE', 'for internal reference only') - ); - - // Headline - $fields->dataFieldByName('Headline') - ->setTitle( - _t(__CLASS__ . '.HEADLINE', 'Headline') - ) - ->setDescription( - _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') - ); - - // Description - $fields->dataFieldByName('Description') - ->setTitle( - _t(__CLASS__ . '.DESCRIPTION', 'Description') - ) - ->setDescription( - _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') - ); - - // Page link - $fields->dataFieldByName('PageLinkID') - ->setTitle( - _t(__CLASS__ . '.PAGE_LINK', "Choose a page to link to:") - ); + $this->beforeUpdateCMSFields(function ($fields) { + $fields->removeByName([ + 'ShowSlide', + 'SortOrder', + 'PageID', + 'Image', + ]); + + // Name + $fields->dataFieldByName('Name') + ->setTitle( + _t(__CLASS__ . '.NAME', 'Name') + ) + ->setDescription( + _t(__CLASS__ . '.INTERNAL_USE', 'for internal reference only') + ); + + // Headline + $fields->dataFieldByName('Headline') + ->setTitle( + _t(__CLASS__ . '.HEADLINE', 'Headline') + ) + ->setDescription( + _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') + ); + + // Description + $fields->dataFieldByName('Description') + ->setTitle( + _t(__CLASS__ . '.DESCRIPTION', 'Description') + ) + ->setDescription( + _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') + ); + + // Page link + $fields->dataFieldByName('PageLinkID') + ->setTitle( + _t(__CLASS__ . '.PAGE_LINK', "Choose a page to link to:") + ); + + // Image + $image = UploadField::create( + 'Image', + _t(__CLASS__ . '.IMAGE', 'Image') + )->setFolderName('Uploads/SlideImages'); + + $image->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']); + + $fields->insertAfter($image, 'Description'); + }); - // Image - $image = UploadField::create( - 'Image', - _t(__CLASS__ . '.IMAGE', 'Image') - )->setFolderName('Uploads/SlideImages'); - - $image->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']); - - $fields->insertAfter($image, 'Description'); + $fields = parent::getCMSFields(); $this->extend('updateSlideImageFields', $fields);