Skip to content

Commit

Permalink
NEW Scaffold formfields for File and Image using new API (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Jun 7, 2024
1 parent c4081b9 commit 20cc083
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
15 changes: 15 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace SilverStripe\Assets;

use SilverStripe\Forms\FileHandleField;
use SilverStripe\Forms\FormField;
use SilverStripe\ORM\DataObject;

/**
* Represents an Image
*/
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 20cc083

Please sign in to comment.