From 57a9026fa5309481e0c82196077aa0c6378a3d10 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Wed, 5 Jun 2024 11:33:05 +1200 Subject: [PATCH] NEW Scaffold formfields for File and Image using new API --- src/File.php | 15 +++++++++++++++ src/Image.php | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/File.php b/src/File.php index 7f085d46..901b3ee1 100644 --- a/src/File.php +++ b/src/File.php @@ -16,6 +16,8 @@ use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\Core\Resettable; use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\FileHandleField; +use SilverStripe\Forms\FormField; use SilverStripe\Forms\HTMLReadonlyField; use SilverStripe\Forms\TextField; use SilverStripe\ORM\ArrayList; @@ -587,6 +589,19 @@ public function getCMSFields() return $fields; } + public function scaffoldFieldForHasOne( + string $fieldName, + string $fieldTitle, + string $relationName, + DataObject $ownerRecord + ): FormField&FileHandleField { + $field = Injector::inst()->create(FileHandleField::class, $relationName, $fieldTitle); + if ($field->hasMethod('setAllowedMaxFileNumber')) { + $field->setAllowedMaxFileNumber(1); + } + return $field; + } + /** * Get title for current file status * diff --git a/src/Image.php b/src/Image.php index 91a30c1f..8bb4024d 100644 --- a/src/Image.php +++ b/src/Image.php @@ -2,6 +2,10 @@ namespace SilverStripe\Assets; +use SilverStripe\Forms\FileHandleField; +use SilverStripe\Forms\FormField; +use SilverStripe\ORM\DataObject; + /** * Represents an Image */ @@ -40,6 +44,17 @@ public function __construct($record = null, $isSingleton = false, $queryParams = $this->File->setAllowedCategories('image/supported'); } + public function scaffoldFieldForHasOne( + string $fieldName, + string $fieldTitle, + string $relationName, + DataObject $ownerRecord + ): FormField&FileHandleField { + $field = parent::scaffoldFieldForHasOne($fieldName, $fieldTitle, $relationName, $ownerRecord); + $field->setAllowedFileCategories('image/supported'); + return $field; + } + public function getIsImage() { return true;