Skip to content

Commit 4b89f29

Browse files
jules0xJulian ThomsonCambis
authored
Move to Silverstripe 6 (#9)
* Move to Silverstripe 6 * Compatibility * Compatibility * ✨ feat: add further cms6 updates --------- Co-authored-by: Julian Thomson <julian.thomson@Julian-M4.local> Co-authored-by: Cameron Bryers <12287346+Cambis@users.noreply.github.com>
1 parent 4da957a commit 4b89f29

12 files changed

Lines changed: 303 additions & 149 deletions

.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# IDE
2+
.editorconfig export-ignore
3+
4+
# Git
5+
.gitattributes export-ignore
6+
.gitignore export-ignore
7+
.github export-ignore
8+
9+
# Coding standards
10+
ecs.php export-ignore
11+
phpstan.neon.dist export-ignore
12+
phpstan-baseline.neon export-ignore
13+
rector.php export-ignore

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# OS
2+
.DS_Store
3+
4+
# IDE
5+
.idea
6+
.vscode
7+
8+
# Silverstripe
9+
public/
10+
app/
11+
12+
# Dependencies
13+
node_modules/
14+
vendor/
15+
composer.lock
16+
yarn-error.log
17+
yarn.lock
18+
pnpm-lock.yaml
19+
20+
# Tests
21+
.phpunit.result.cache
22+
coverage/

_config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
use SilverStripe\Taxonomy\TaxonomyAdmin;
55

66
// Remove
7-
CMSMenu::remove_menu_class(TaxonomyAdmin::class);
7+
CMSMenu::remove_menu_class(TaxonomyAdmin::class); // @phpstan-ignore class.notFound

composer.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@
99
],
1010
"license": "MIT",
1111
"require": {
12-
"silverstripe/framework": "^4.0 || ^5.0",
13-
"silverstripe/vendor-plugin": "^1.0 || ^2.0",
14-
"symbiote/silverstripe-gridfieldextensions": "^3.2 || ^4.0"
12+
"php": "^8.3",
13+
"silverstripe/admin": "^3.0",
14+
"silverstripe/framework": "^6.0",
15+
"symbiote/silverstripe-gridfieldextensions": "^5.0"
16+
},
17+
"require-dev": {
18+
"cambis/silverstan": "^2.1",
19+
"cambis/silverstripe-rector": "^2.1",
20+
"phpstan/extension-installer": "^1.4",
21+
"phpstan/phpstan": "^2.1",
22+
"rector/rector": "^2.3"
1523
},
1624
"autoload": {
1725
"psr-4": {
@@ -20,5 +28,13 @@
2028
},
2129
"extra": {
2230
"installer-name": "silverstripe-tagurit"
31+
},
32+
"config": {
33+
"allow-plugins": {
34+
"composer/installers": true,
35+
"phpstan/extension-installer": true,
36+
"silverstripe/vendor-plugin": true
37+
},
38+
"sort-packages": true
2339
}
2440
}

phpstan.neon.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
includes:
2+
- vendor/cambis/silverstan/bleedingEdge.neon
3+
parameters:
4+
level: 5
5+
treatPhpDocTypesAsCertain: false
6+
paths:
7+
- _config.php
8+
- src

rector.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeLevelSetList;
6+
use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeSetList;
7+
use Cambis\SilverstripeRector\Silverstripe413\Rector\Class_\AddBelongsToPropertyAndMethodAnnotationsToDataObjectRector;
8+
use Cambis\SilverstripeRector\Silverstripe413\Rector\Class_\AddDBFieldPropertyAnnotationsToDataObjectRector;
9+
use Cambis\SilverstripeRector\Silverstripe413\Rector\Class_\AddExtensionMixinAnnotationsToExtensibleRector;
10+
use Cambis\SilverstripeRector\Silverstripe413\Rector\Class_\AddHasOnePropertyAndMethodAnnotationsToDataObjectRector;
11+
use Cambis\SilverstripeRector\Silverstripe52\Rector\Class_\AddBelongsManyManyMethodAnnotationsToDataObjectRector;
12+
use Cambis\SilverstripeRector\Silverstripe52\Rector\Class_\AddExtendsAnnotationToContentControllerRector;
13+
use Cambis\SilverstripeRector\Silverstripe52\Rector\Class_\AddExtendsAnnotationToExtensionRector;
14+
use Cambis\SilverstripeRector\Silverstripe52\Rector\Class_\AddHasManyMethodAnnotationsToDataObjectRector;
15+
use Cambis\SilverstripeRector\Silverstripe52\Rector\Class_\AddManyManyMethodAnnotationsToDataObjectRector;
16+
use Rector\Config\RectorConfig;
17+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
18+
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
19+
20+
return RectorConfig::configure()
21+
->withImportNames(removeUnusedImports: true)
22+
->withPaths([
23+
__DIR__ . '/_config.php',
24+
__DIR__ . '/src',
25+
])
26+
->withPhpSets()
27+
->withSets([
28+
SilverstripeLevelSetList::UP_TO_SILVERSTRIPE_60,
29+
SilverstripeSetList::CODE_QUALITY,
30+
])
31+
->withSkip([
32+
AddOverrideAttributeToOverriddenMethodsRector::class,
33+
ClosureToArrowFunctionRector::class,
34+
// Skip the add annotation rectors
35+
AddDBFieldPropertyAnnotationsToDataObjectRector::class,
36+
AddBelongsToPropertyAndMethodAnnotationsToDataObjectRector::class,
37+
AddHasOnePropertyAndMethodAnnotationsToDataObjectRector::class,
38+
AddHasManyMethodAnnotationsToDataObjectRector::class,
39+
AddBelongsManyManyMethodAnnotationsToDataObjectRector::class,
40+
AddManyManyMethodAnnotationsToDataObjectRector::class,
41+
AddExtensionMixinAnnotationsToExtensibleRector::class,
42+
AddExtendsAnnotationToContentControllerRector::class,
43+
AddExtendsAnnotationToExtensionRector::class,
44+
]);

src/Model/TaxonomyTerm.php

Lines changed: 92 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace DNADesign\Tagurit\Model;
44

5+
use DNADesign\Tagurit\Traits\TaxonomyTrait;
6+
use Override;
7+
use SilverStripe\Forms\DropdownField;
8+
use SilverStripe\Forms\FieldList;
9+
use SilverStripe\Forms\FormField;
10+
use SilverStripe\Forms\GridField\GridField;
11+
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
12+
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
13+
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
14+
use SilverStripe\Forms\ListboxField;
515
use SilverStripe\ORM\DataObject;
16+
use SilverStripe\Security\Permission;
617
use SilverStripe\ORM\FieldType\DBField;
718
use SilverStripe\ORM\FieldType\DBHTMLText;
8-
use SilverStripe\ORM\ValidationResult;
9-
use SilverStripe\Security\Permission;
1019
use SilverStripe\Security\PermissionProvider;
20+
use SilverStripe\Core\Validation\ValidationResult;
21+
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
22+
use Symfony\Component\String\Inflector\EnglishInflector;
1123

1224
/**
1325
* Represents a single taxonomy term. Can be re-ordered in the CMS, and the default sorting is to use the order as
@@ -21,19 +33,21 @@
2133
*/
2234
class TaxonomyTerm extends DataObject implements PermissionProvider
2335
{
36+
use TaxonomyTrait;
37+
2438
private static $singular_name = 'Term';
2539

2640
private static $table_name = 'TaxonomyTerm';
2741

28-
private static $db = array(
42+
private static $db = [
2943
'Name' => 'Varchar(255)',
3044
'Sort' => 'Int',
3145
'Protected' => 'Boolean'
32-
);
46+
];
3347

34-
private static $has_one = array(
48+
private static $has_one = [
3549
'Type' => TaxonomyType::class
36-
);
50+
];
3751

3852
private static $summary_fields = [
3953
'Title' => 'Title',
@@ -42,21 +56,23 @@ class TaxonomyTerm extends DataObject implements PermissionProvider
4256

4357
private static $default_sort = 'Sort';
4458

45-
public function getCMSFields()
46-
{
47-
$fields = parent::getCMSFields();
48-
49-
$fields->removeByName([
50-
'Protected',
59+
private static array $scaffold_cms_fields_settings = [
60+
'ignoreFields' => [
5161
'Sort',
52-
'TypeID'
53-
]);
62+
'Type',
63+
'Protected',
64+
],
65+
];
5466

55-
if ($this->isInDB() && $this->Protected > 0) {
56-
$fields->dataFieldByName('Name')->setReadonly(true);
57-
}
67+
public function getCMSFields(): FieldList
68+
{
69+
$this->beforeUpdateCMSFields(function (FieldList $fields): void {
70+
if ($this->isInDB() && $this->Protected > 0) {
71+
$fields->dataFieldByName('Name')?->setReadonly(true);
72+
}
73+
});
5874

59-
return $fields;
75+
return parent::getCMSFields();
6076
}
6177

6278
public function getIsProtected()
@@ -70,7 +86,7 @@ public function getIsProtected()
7086
return DBField::create_field(DBHTMLText::class, $label);
7187
}
7288

73-
public function validate()
89+
public function validate(): ValidationResult
7490
{
7591
$result = ValidationResult::create();
7692

@@ -119,7 +135,7 @@ public function canDelete($member = null)
119135
return Permission::check('TAXONOMYTERM_DELETE');
120136
}
121137

122-
public function canCreate($member = null, $context = array())
138+
public function canCreate($member = null, $context = [])
123139
{
124140
$extended = $this->extendedCan(__FUNCTION__, $member);
125141
if ($extended !== null) {
@@ -131,37 +147,80 @@ public function canCreate($member = null, $context = array())
131147

132148
public function providePermissions()
133149
{
134-
return array(
135-
'TAXONOMYTERM_EDIT' => array(
150+
return [
151+
'TAXONOMYTERM_EDIT' => [
136152
'name' => _t(
137-
__CLASS__ . '.EditPermissionLabel',
153+
self::class . '.EditPermissionLabel',
138154
'Edit a taxonomy term'
139155
),
140156
'category' => _t(
141-
__CLASS__ . '.Category',
157+
self::class . '.Category',
142158
'Taxonomy terms'
143159
),
144-
),
145-
'TAXONOMYTERM_DELETE' => array(
160+
],
161+
'TAXONOMYTERM_DELETE' => [
146162
'name' => _t(
147-
__CLASS__ . '.DeletePermissionLabel',
163+
self::class . '.DeletePermissionLabel',
148164
'Delete a taxonomy term and all nested terms'
149165
),
150166
'category' => _t(
151-
__CLASS__ . '.Category',
167+
self::class . '.Category',
152168
'Taxonomy terms'
153169
),
154-
),
155-
'TAXONOMYTERM_CREATE' => array(
170+
],
171+
'TAXONOMYTERM_CREATE' => [
156172
'name' => _t(
157-
__CLASS__ . '.CreatePermissionLabel',
173+
self::class . '.CreatePermissionLabel',
158174
'Create a taxonomy term'
159175
),
160176
'category' => _t(
161-
__CLASS__ . '.Category',
177+
self::class . '.Category',
162178
'Taxonomy terms'
163179
),
164-
)
180+
]
181+
];
182+
}
183+
184+
#[Override]
185+
public function scaffoldFormFieldForHasOne(string $fieldName, ?string $fieldTitle, string $relationName, DataObject $ownerRecord): FormField
186+
{
187+
return DropdownField::create(
188+
$relationName,
189+
$fieldTitle,
190+
$this->getTermsForType($fieldTitle ?? $fieldName)->map()
191+
);
192+
}
193+
194+
#[Override]
195+
public function scaffoldFormFieldForHasMany(string $relationName, ?string $fieldTitle, DataObject $ownerRecord, bool &$includeInOwnTab): FormField
196+
{
197+
$includeInOwnTab = true;
198+
199+
return GridField::create(
200+
$relationName,
201+
$fieldTitle,
202+
$ownerRecord->$relationName(),
203+
GridFieldConfig_RecordEditor::create()
204+
->addComponent(GridFieldOrderableRows::create('Sort'))
205+
->removeComponentsByType([
206+
GridFieldDeleteAction::class,
207+
GridFieldAddExistingAutocompleter::class
208+
])
209+
);
210+
}
211+
212+
#[Override]
213+
public function scaffoldFormFieldForManyMany(string $relationName, ?string $fieldTitle, DataObject $ownerRecord, bool &$includeInOwnTab): FormField
214+
{
215+
$includeInOwnTab = true;
216+
217+
// Term names are usually singular forms of the relation name
218+
$termName = (new EnglishInflector())->singularize($fieldTitle ?? $relationName)[0];
219+
220+
return ListboxField::create(
221+
$relationName,
222+
$fieldTitle,
223+
$this->getTermsForType($termName) // @phpstan-ignore argument.type
165224
);
166225
}
167226
}

0 commit comments

Comments
 (0)