From 20cc083e353205d741044553c60d3c5f581e8fe3 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Fri, 7 Jun 2024 17:06:16 +1200 Subject: [PATCH] NEW Scaffold formfields for File and Image using new API (#613) --- 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..53e021fb 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 scaffoldFormFieldForHasOne( + 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..573a3175 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 scaffoldFormFieldForHasOne( + string $fieldName, + ?string $fieldTitle, + string $relationName, + DataObject $ownerRecord + ): FormField&FileHandleField { + $field = parent::scaffoldFormFieldForHasOne($fieldName, $fieldTitle, $relationName, $ownerRecord); + $field->setAllowedFileCategories('image/supported'); + return $field; + } + public function getIsImage() { return true;