GithubHelp home page GithubHelp logo

Comments (3)

mooror avatar mooror commented on August 18, 2024

This would be a marvelous addition to the DataObject infrastructure 💯

from silverstripe-framework.

maxime-rainville avatar maxime-rainville commented on August 18, 2024

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.

Related issues

from silverstripe-framework.

emteknetnz avatar emteknetnz commented on August 18, 2024

Linked PRs have been merged

from silverstripe-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.