Skip to content
New issue

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

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

Already on GitHub? # to your account

Allow individual DataObject to scaffold a FormField that should be used to managed them #11234

Closed
maxime-rainville opened this issue May 12, 2024 · 3 comments

Comments

@maxime-rainville
Copy link
Contributor

maxime-rainville commented May 12, 2024

Description

DBField can scaffold form field to manage what should be used to managed them. e.g.

public function scaffoldFormField($title = null, $params = null)
{
return MoneyField::create($this->getName(), $title)
->setLocale($this->getLocale());
}

HasOne relation use the DBForeignKey to scaffold their field which builds a SearchableDropdownField for most DataObject. It also has a bit of hard coded logic for File.

public function scaffoldFormField($title = null, $params = null)
{
if (empty($this->object)) {
return null;
}
$relationName = substr($this->name ?? '', 0, -2);
$hasOneClass = DataObject::getSchema()->hasOneComponent(get_class($this->object), $relationName);
if (empty($hasOneClass)) {
return null;
}
$hasOneSingleton = singleton($hasOneClass);
if ($hasOneSingleton instanceof File) {
$field = Injector::inst()->create(FileHandleField::class, $relationName, $title);
if ($hasOneSingleton instanceof Image) {
$field->setAllowedFileCategories('image/supported');
}
if ($field->hasMethod('setAllowedMaxFileNumber')) {
$field->setAllowedMaxFileNumber(1);
}
return $field;
}
$labelField = $hasOneSingleton->hasField('Title') ? 'Title' : 'Name';
$list = DataList::create($hasOneClass);
$threshold = self::config()->get('dropdown_field_threshold');
$overThreshold = $list->count() > $threshold;
$field = SearchableDropdownField::create($this->name, $title, $list, $labelField)
->setIsLazyLoaded($overThreshold)
->setLazyLoadLimit($threshold);
return $field;
}

They are many scenarios where SearchableDropdownField is not the best form field to use for a specific DataObject. e.g.: Link, SiteTree, Taxanomie Tags.

I'm thinking it would be trivial to update DBForeignKey::scaffoldFormField() to call a matching scaffoldFormField on singleton to retrieve its form field:

  • DataObject::scaffoldFormField() could SearchableDropdownField as a sensible default.
  • File could override that default to return an UploadField which would avoid tying DBForeignKey to the File class.
  • Specialised DataObjects like Link can specify their own custom field.

Additional context or points of discussion

Related issues

@GuySartorelli
Copy link
Member

Is this a duplicate of #11079?

@GuySartorelli
Copy link
Member

@maxime-rainville If this is the same as #11079 (and I'm 90% sure it is), it can be done in a minor release - in fact that issue is in the 5.3 milestone already.

@maxime-rainville
Copy link
Contributor Author

Closed as duplicate.

@maxime-rainville maxime-rainville closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants