-
Notifications
You must be signed in to change notification settings - Fork 821
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
NEW Make CMSFields scaffolding configurable, plus new options #11328
Merged
emteknetnz
merged 1 commit into
silverstripe:5
from
creative-commoners:pulls/5/configurable-scaffolding-options
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,15 @@ class FormScaffolder | |
*/ | ||
public $tabbed = false; | ||
|
||
/** | ||
* Only set up the "Root.Main" tab, but skip scaffolding actual FormFields. | ||
* If $tabbed is false, an empty FieldList will be returned. | ||
*/ | ||
public bool $mainTabOnly = false; | ||
|
||
/** | ||
* @var boolean $ajaxSafe | ||
* @deprecated 5.3.0 Will be removed without equivalent functionality. | ||
*/ | ||
public $ajaxSafe = false; | ||
Comment on lines
37
to
41
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. This isn't actually used at all |
||
|
||
|
@@ -39,17 +46,33 @@ class FormScaffolder | |
*/ | ||
public $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}. | ||
* By default the scaffolder will determine the field instance by {@link DBField::scaffoldFormField()}. | ||
*/ | ||
public $fieldClasses; | ||
|
||
/** | ||
* @var boolean $includeRelations Include has_one, has_many and many_many relations | ||
* @var boolean $includeRelations Include has_many and many_many relations | ||
Comment on lines
-49
to
+61
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. This was intentionally changed to not include has_one in 524d7a9 |
||
*/ | ||
public $includeRelations = false; | ||
|
||
/** | ||
* Array of relation names to use as an allow list. | ||
* If left blank, all has_many and many_many relations will be scaffolded unless explicitly ignored. | ||
*/ | ||
public array $restrictRelations = []; | ||
|
||
/** | ||
* Numeric array of has_many and many_many relations to explicitly not scaffold. | ||
*/ | ||
public array $ignoreRelations = []; | ||
|
||
/** | ||
* @param DataObject $obj | ||
*/ | ||
|
@@ -76,12 +99,20 @@ public function getFieldList() | |
$mainTab->setTitle(_t(__CLASS__ . '.TABMAIN', 'Main')); | ||
} | ||
|
||
if ($this->mainTabOnly) { | ||
return $fields; | ||
} | ||
|
||
// Add logical fields directly specified in db config | ||
foreach ($this->obj->config()->get('db') as $fieldName => $fieldType) { | ||
// Skip restricted fields | ||
// Skip fields that aren't in the allow list | ||
if ($this->restrictFields && !in_array($fieldName, $this->restrictFields ?? [])) { | ||
continue; | ||
} | ||
// Skip ignored fields | ||
if (in_array($fieldName, $this->ignoreFields)) { | ||
continue; | ||
} | ||
|
||
if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { | ||
$fieldClass = $this->fieldClasses[$fieldName]; | ||
|
@@ -110,6 +141,9 @@ public function getFieldList() | |
if ($this->restrictFields && !in_array($relationship, $this->restrictFields ?? [])) { | ||
continue; | ||
} | ||
if (in_array($relationship, $this->ignoreFields)) { | ||
continue; | ||
} | ||
$fieldName = $component === 'SilverStripe\\ORM\\DataObject' | ||
? $relationship // Polymorphic has_one field is composite, so don't refer to ID subfield | ||
: "{$relationship}ID"; | ||
|
@@ -138,6 +172,12 @@ public function getFieldList() | |
&& ($this->includeRelations === true || isset($this->includeRelations['has_many'])) | ||
) { | ||
foreach ($this->obj->hasMany() as $relationship => $component) { | ||
if (!empty($this->restrictRelations) && !in_array($relationship, $this->restrictRelations)) { | ||
continue; | ||
} | ||
if (in_array($relationship, $this->ignoreRelations)) { | ||
continue; | ||
} | ||
$includeInOwnTab = true; | ||
$fieldLabel = $this->obj->fieldLabel($relationship); | ||
$fieldClass = (isset($this->fieldClasses[$relationship])) | ||
|
@@ -177,6 +217,12 @@ public function getFieldList() | |
&& ($this->includeRelations === true || isset($this->includeRelations['many_many'])) | ||
) { | ||
foreach ($this->obj->manyMany() as $relationship => $component) { | ||
if (!empty($this->restrictRelations) && !in_array($relationship, $this->restrictRelations)) { | ||
continue; | ||
} | ||
if (in_array($relationship, $this->ignoreRelations)) { | ||
continue; | ||
} | ||
static::addManyManyRelationshipFields( | ||
$fields, | ||
$relationship, | ||
|
@@ -252,8 +298,12 @@ protected function getParamsArray() | |
{ | ||
return [ | ||
'tabbed' => $this->tabbed, | ||
'mainTabOnly' => $this->mainTabOnly, | ||
'includeRelations' => $this->includeRelations, | ||
'restrictRelations' => $this->restrictRelations, | ||
'ignoreRelations' => $this->ignoreRelations, | ||
'restrictFields' => $this->restrictFields, | ||
'ignoreFields' => $this->ignoreFields, | ||
'fieldClasses' => $this->fieldClasses, | ||
'ajaxSafe' => $this->ajaxSafe | ||
]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This isn't strictly needed, but it makes the test easier.
It seemed silly to be throwing an exception when
$currentPointer
is null, because in this scenario the tab doesn't exist so we can just returnnull
from the method.