You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UploadField::getIsMultiUpload() has auto-detection capability - it can guess if multiple fields are allowed or not based on the related DB record.
To reproduce this, create a has_one relation to File on a new elemental block, and add an UploadField without explicitly setting isMultiUpload. It will accept multiple files.
More specific details
publicfunctiongetIsMultiUpload()
{
if (isset($this->multiUpload)) {
return$this->multiUpload;
}
// Guess from record$record = $this->getRecord();
$name = $this->getName(); // <------- this breaks// Disabled for has_one componentsif ($record && DataObject::getSchema()->hasOneComponent(get_class($record), $name)) {
returnfalse;
}
returntrue;
}
Using the upload field within a content block that uses inline editing will break this feature unfortunately. This is caused by EditFormFactory::namespaceFields() which changes the field names.
This will change for example Image to PageElements_153_Image. This will break the auto-detection feature of the UploadField as it can no longer recognise the field by name, so it falls back to default.
Workaround
This is my workaround - sublcass upload field and override the getIsMultiUpload() method.
/** * Can't use directly @see EditFormFactory::FIELD_NAMESPACE_TEMPLATE */privateconstNAMESPACE_PREFIX = 'PageElements_';
/** * Copied from @see UploadField::getIsMultiUpload() * Fixed the issue when this field is used in the elemental inline editor context * The field names are prefixed which breaks the auto-detection feature of this field * Issue caused by @see EditFormFactory::namespaceFields() * GitHub issue raised: https://github.com/silverstripe/silverstripe-elemental/issues/1104 * * @return bool */publicfunctiongetIsMultiUpload(): bool
{
if ($this->multiUpload !== null) {
// Explicit settingsreturn (bool) $this->multiUpload;
}
// Guess from record$record = $this->getRecord();
$name = $this->getName();
$name = $this->removeElementalNamespace($name);
// Disabled for has_one componentsif ($record && DataObject::getSchema()->hasOneComponent($record::class, $name)) {
returnfalse;
}
returntrue;
}
privatefunctionremoveElementalNamespace(string$fieldName): string
{
if (mb_strpos($fieldName, self::NAMESPACE_PREFIX) === 0) {
// Extract the last segment of the namespaced field which is the original field name$segments = explode('_', $fieldName);
returnarray_pop($segments);
}
return$fieldName;
}
CMS version:
4.13
Elemental version
4.9.0
UploadField::getIsMultiUpload()
has auto-detection capability - it can guess if multiple fields are allowed or not based on the related DB record.To reproduce this, create a
has_one
relation toFile
on a new elemental block, and add anUploadField
without explicitly settingisMultiUpload
. It will accept multiple files.More specific details
Using the upload field within a content block that uses inline editing will break this feature unfortunately. This is caused by
EditFormFactory::namespaceFields()
which changes the field names.This will change for example
Image
toPageElements_153_Image
. This will break the auto-detection feature of theUploadField
as it can no longer recognise the field by name, so it falls back to default.Workaround
This is my workaround - sublcass upload field and override the
getIsMultiUpload()
method.Test for this:
Acceptance criteria
PRs
The text was updated successfully, but these errors were encountered: