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

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

Open
maxime-rainville opened this issue May 12, 2024 · 1 comment

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants