ENH Explicitly require DataObject for some gridfield components

These components simply cannot work with non-DataObjects. They have
explicit DataObject queries, and allowing arbitrary callbacks for these
components would be a case of diminishing returns.
This commit is contained in:
Guy Sartorelli 2023-11-14 15:37:20 +13:00
parent b1295af281
commit 5838772b19
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
2 changed files with 32 additions and 1 deletions

View File

@ -32,6 +32,8 @@ use LogicException;
*
* For easier setup, have a look at a sample configuration in
* {@link GridFieldConfig_RelationEditor}.
*
* The modelClass of the GridField this component is in must be a DataObject subclass.
*/
class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator, GridField_URLHandler
{
@ -106,6 +108,10 @@ class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent imple
{
$dataClass = $gridField->getModelClass();
if (!is_a($dataClass, DataObject::class, true)) {
throw new LogicException(__CLASS__ . " must be used with DataObject subclasses. Found '$dataClass'");
}
$forTemplate = new ArrayData([]);
$forTemplate->Fields = new FieldList();
@ -191,11 +197,17 @@ class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent imple
*/
public function getManipulatedData(GridField $gridField, SS_List $dataList)
{
$dataClass = $gridField->getModelClass();
if (!is_a($dataClass, DataObject::class, true)) {
throw new LogicException(__CLASS__ . " must be used with DataObject subclasses. Found '$dataClass'");
}
$objectID = $gridField->State->GridFieldAddRelation(null);
if (empty($objectID)) {
return $dataList;
}
$object = DataObject::get_by_id($gridField->getModelClass(), $objectID);
$object = DataObject::get_by_id($dataClass, $objectID);
if ($object) {
$dataList->add($object);
}
@ -227,6 +239,10 @@ class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent imple
$searchStr = $request->getVar('gridfield_relationsearch');
$dataClass = $gridField->getModelClass();
if (!is_a($dataClass, DataObject::class, true)) {
throw new LogicException(__CLASS__ . " must be used with DataObject subclasses. Found '$dataClass'");
}
$searchFields = ($this->getSearchFields())
? $this->getSearchFields()
: $this->scaffoldSearchFields($dataClass);
@ -337,6 +353,10 @@ class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent imple
*/
public function scaffoldSearchFields($dataClass)
{
if (!is_a($dataClass, DataObject::class, true)) {
throw new LogicException(__CLASS__ . " must be used with DataObject subclasses. Found '$dataClass'");
}
$obj = DataObject::singleton($dataClass);
$fields = null;
if ($fieldSpecs = $obj->searchableFields()) {
@ -387,6 +407,10 @@ class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent imple
*/
public function getPlaceholderText($dataClass)
{
if (!is_a($dataClass, DataObject::class, true)) {
throw new LogicException(__CLASS__ . " must be used with DataObject subclasses. Found '$dataClass'");
}
$searchFields = ($this->getSearchFields())
? $this->getSearchFields()
: $this->scaffoldSearchFields($dataClass);

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Forms\GridField;
use LogicException;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\Hierarchy\Hierarchy;
@ -13,6 +14,8 @@ use SilverStripe\View\SSViewer;
* Adds a "level up" link to a GridField table, which is useful when viewing
* hierarchical data. Requires the managed record to have a "getParent()"
* method or has_one relationship called "Parent".
*
* The modelClass of the GridField this component is in must be a DataObject subclass.
*/
class GridFieldLevelup extends AbstractGridFieldComponent implements GridField_HTMLProvider
{
@ -53,6 +56,10 @@ class GridFieldLevelup extends AbstractGridFieldComponent implements GridField_H
$modelClass = $gridField->getModelClass();
$parentID = 0;
if (!is_a($modelClass, DataObject::class, true)) {
throw new LogicException(__CLASS__ . " must be used with DataObject subclasses. Found '$modelClass'");
}
if (!$this->currentID) {
return null;
}