Skip to content

Commit

Permalink
NEW Scaffold formfields for File and Image using new API
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jun 5, 2024
1 parent 159e152 commit 6b81cdc
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 6b81cdc

Please sign in to comment.