Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Scaffold formfields for File and Image using new API #613

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$field->setAllowedFileCategories('image/supported');
if ($field->hasMethod('setAllowedFileCategories')) {
$field->setAllowedFileCategories('image/supported');
}

Might as well do the same check as above since the FileHandleField could be changed with Injector

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setAllowedFileCategories() is defined in the FileHandleField interface, so we don't need to check for it.

setAllowedMaxFileNumber() isn't defined in that interface, which is why the condition for that method is there.

return $field;
}

public function getIsImage()
{
return true;
Expand Down
Loading