22
33namespace 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 ;
515use SilverStripe \ORM \DataObject ;
16+ use SilverStripe \Security \Permission ;
617use SilverStripe \ORM \FieldType \DBField ;
718use SilverStripe \ORM \FieldType \DBHTMLText ;
8- use SilverStripe \ORM \ValidationResult ;
9- use SilverStripe \Security \Permission ;
1019use 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
2133 */
2234class 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