Skip to content

Commit

Permalink
ENH Use appropriate formfields when auto-scaffolding files (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Jul 2, 2024
1 parent 5ca1e01 commit 94c3089
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\Assets;

use InvalidArgumentException;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Shortcodes\FileLink;
use SilverStripe\Assets\Shortcodes\FileLinkTracking;
use SilverStripe\Assets\Shortcodes\FileShortcodeProvider;
Expand Down Expand Up @@ -594,14 +595,44 @@ public function scaffoldFormFieldForHasOne(
?string $fieldTitle,
string $relationName,
DataObject $ownerRecord
): FormField&FileHandleField {
): FormField {
$field = Injector::inst()->create(FileHandleField::class, $relationName, $fieldTitle);
if ($field->hasMethod('setAllowedMaxFileNumber')) {
$field->setAllowedMaxFileNumber(1);
}
return $field;
}

public function scaffoldFormFieldForHasMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$field = Injector::inst()->create(FileHandleField::class, $relationName, $fieldTitle);
if ($field instanceof UploadField) {
$includeInOwnTab = false;
$field->setIsMultiUpload(true);
return $field;
}
return parent::scaffoldFormFieldForHasMany($relationName, $fieldTitle, $ownerRecord, $includeInOwnTab);
}

public function scaffoldFormFieldForManyMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$field = Injector::inst()->create(FileHandleField::class, $relationName, $fieldTitle);
if ($field instanceof UploadField) {
$includeInOwnTab = false;
$field->setIsMultiUpload(true);
return $field;
}
return parent::scaffoldFormFieldForManyMany($relationName, $fieldTitle, $ownerRecord, $includeInOwnTab);
}

/**
* Get title for current file status
*
Expand Down
33 changes: 33 additions & 0 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\TreeMultiselectField;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Versioned\Versioned;

Expand Down Expand Up @@ -94,6 +98,35 @@ public static function find_or_make($folderPath)
return $item;
}

public function scaffoldFormFieldForHasOne(
string $fieldName,
?string $fieldTitle,
string $relationName,
DataObject $ownerRecord
): FormField {
return TreeDropdownField::create($fieldName, $fieldTitle, static::class);
}

public function scaffoldFormFieldForHasMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = false;
return TreeMultiselectField::create($relationName, $fieldTitle, static::class);
}

public function scaffoldFormFieldForManyMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = false;
return TreeMultiselectField::create($relationName, $fieldTitle, static::class);
}

protected function onBeforeDelete()
{
foreach ($this->AllChildren() as $child) {
Expand Down
26 changes: 26 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ public function scaffoldFormFieldForHasOne(
return $field;
}

public function scaffoldFormFieldForHasMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$field = parent::scaffoldFormFieldForHasMany($relationName, $fieldTitle, $ownerRecord, $includeInOwnTab);
if ($field instanceof FileHandleField) {
$field->setAllowedFileCategories('image/supported');
}
return $field;
}

public function scaffoldFormFieldForManyMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$field = parent::scaffoldFormFieldForManyMany($relationName, $fieldTitle, $ownerRecord, $includeInOwnTab);
if ($field instanceof FileHandleField) {
$field->setAllowedFileCategories('image/supported');
}
return $field;
}

public function getIsImage()
{
return true;
Expand Down

0 comments on commit 94c3089

Please sign in to comment.