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;