From 5838772b193df35ee7665770ccd32bf19f65355b Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 14 Nov 2023 15:37:20 +1300 Subject: [PATCH] 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. --- .../GridFieldAddExistingAutocompleter.php | 26 ++++++++++++++++++- src/Forms/GridField/GridFieldLevelup.php | 7 +++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Forms/GridField/GridFieldAddExistingAutocompleter.php b/src/Forms/GridField/GridFieldAddExistingAutocompleter.php index ff6176d92..d8f83678c 100644 --- a/src/Forms/GridField/GridFieldAddExistingAutocompleter.php +++ b/src/Forms/GridField/GridFieldAddExistingAutocompleter.php @@ -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); diff --git a/src/Forms/GridField/GridFieldLevelup.php b/src/Forms/GridField/GridFieldLevelup.php index 8986d323c..32d22d30d 100644 --- a/src/Forms/GridField/GridFieldLevelup.php +++ b/src/Forms/GridField/GridFieldLevelup.php @@ -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; }