-
Notifications
You must be signed in to change notification settings - Fork 824
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
ENH Various changes to support SiteTree form field scaffolding #11327
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,16 @@ class FormScaffolder | |
use Injectable; | ||
|
||
/** | ||
* @var DataObject $obj The object defining the fields to be scaffolded | ||
* The object defining the fields to be scaffolded | ||
* through its metadata like $db, $searchable_fields, etc. | ||
*/ | ||
protected $obj; | ||
protected DataObject $obj; | ||
|
||
/** | ||
* @var boolean $tabbed Return fields in a tabset, with all main fields in the path "Root.Main", | ||
* Return fields in a tabset, with all main fields in the path "Root.Main", | ||
* relation fields in "Root.<relationname>" (if {@link $includeRelations} is enabled). | ||
*/ | ||
public $tabbed = false; | ||
public bool $tabbed = false; | ||
|
||
/** | ||
* Only set up the "Root.Main" tab, but skip scaffolding actual FormFields. | ||
|
@@ -35,32 +35,26 @@ class FormScaffolder | |
public bool $mainTabOnly = false; | ||
|
||
/** | ||
* @var boolean $ajaxSafe | ||
* @deprecated 5.3.0 Will be removed without equivalent functionality. | ||
* Array of field names to use as an allow list. | ||
* If left blank, all fields from {@link DataObject->db()} will be included unless explicitly ignored. | ||
*/ | ||
public $ajaxSafe = false; | ||
|
||
/** | ||
* @var array $restrictFields Numeric array of a field name whitelist. | ||
* If left blank, all fields from {@link DataObject->db()} will be included. | ||
*/ | ||
public $restrictFields; | ||
public array $restrictFields = []; | ||
|
||
/** | ||
* Numeric array of field names and has_one relations to explicitly not scaffold. | ||
*/ | ||
public array $ignoreFields = []; | ||
|
||
/** | ||
* @var array $fieldClasses Optional mapping of fieldnames to subclasses of {@link FormField}. | ||
* Optional mapping of fieldnames to subclasses of {@link FormField}. | ||
* By default the scaffolder will determine the field instance by {@link DBField::scaffoldFormField()}. | ||
*/ | ||
public $fieldClasses; | ||
public array $fieldClasses = []; | ||
|
||
/** | ||
* @var boolean $includeRelations Include has_many and many_many relations | ||
* Include has_many and many_many relations | ||
*/ | ||
public $includeRelations = false; | ||
public bool|array $includeRelations = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the code that uses this, you could use it to explicitly only allow |
||
|
||
/** | ||
* Array of relation names to use as an allow list. | ||
|
@@ -106,15 +100,15 @@ public function getFieldList() | |
// Add logical fields directly specified in db config | ||
foreach ($this->obj->config()->get('db') as $fieldName => $fieldType) { | ||
// Skip fields that aren't in the allow list | ||
if ($this->restrictFields && !in_array($fieldName, $this->restrictFields ?? [])) { | ||
if (!empty($this->restrictFields) && !in_array($fieldName, $this->restrictFields)) { | ||
Comment on lines
-109
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must be an array, so we can tidy this a little now. Applies to similar changes below as well. |
||
continue; | ||
} | ||
// Skip ignored fields | ||
if (in_array($fieldName, $this->ignoreFields)) { | ||
continue; | ||
} | ||
|
||
if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { | ||
if (isset($this->fieldClasses[$fieldName])) { | ||
$fieldClass = $this->fieldClasses[$fieldName]; | ||
$fieldObject = new $fieldClass($fieldName); | ||
} else { | ||
|
@@ -138,7 +132,7 @@ public function getFieldList() | |
// add has_one relation fields | ||
if ($this->obj->hasOne()) { | ||
foreach ($this->obj->hasOne() as $relationship => $component) { | ||
if ($this->restrictFields && !in_array($relationship, $this->restrictFields ?? [])) { | ||
if (!empty($this->restrictFields) && !in_array($relationship, $this->restrictFields)) { | ||
continue; | ||
} | ||
if (in_array($relationship, $this->ignoreFields)) { | ||
|
@@ -147,7 +141,7 @@ public function getFieldList() | |
$fieldName = $component === 'SilverStripe\\ORM\\DataObject' | ||
? $relationship // Polymorphic has_one field is composite, so don't refer to ID subfield | ||
: "{$relationship}ID"; | ||
if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { | ||
if (isset($this->fieldClasses[$fieldName])) { | ||
$fieldClass = $this->fieldClasses[$fieldName]; | ||
$hasOneField = new $fieldClass($fieldName); | ||
} else { | ||
|
@@ -305,7 +299,6 @@ protected function getParamsArray() | |
'restrictFields' => $this->restrictFields, | ||
'ignoreFields' => $this->ignoreFields, | ||
'fieldClasses' => $this->fieldClasses, | ||
'ajaxSafe' => $this->ajaxSafe | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms\Tests; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\Tab; | ||
|
||
class TabTest extends SapphireTest | ||
{ | ||
protected $usesDatabase = false; | ||
|
||
public function testNameAndID(): void | ||
{ | ||
// ID is set on instantiation based on the name | ||
$tab = new Tab('MyName _-()!@#$'); | ||
$this->assertSame('MyName _-()!@#$', $tab->getName()); | ||
$this->assertSame('MyName_-', $tab->ID()); | ||
|
||
// Changing the name changes the ID | ||
$tab->setName('NewName'); | ||
$this->assertSame('NewName', $tab->getName()); | ||
$this->assertSame('NewName', $tab->ID()); | ||
|
||
// If ID is explicitly set, changing the name doesn't override it | ||
$tab->setID('Custom-ID'); | ||
$tab->setName('AnotherName'); | ||
$this->assertSame('AnotherName', $tab->getName()); | ||
$this->assertSame('Custom-ID', $tab->ID()); | ||
} | ||
Comment on lines
+12
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't use a data provider for this - the scenarios need to be run sequentially. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added strict typing for the properties as a matter of course - it's unrelated refactoring.