@@ -9,6 +9,8 @@ class ExtraPageFieldsExtension extends SiteTreeExtension
99
1010 private $ MetaDescriptionLength = 156 ;
1111
12+ protected static $ runs = 0 ;
13+
1214 private static $ db = array (
1315 'SubTitle ' => 'Text ' ,
1416 'MetaTitle ' => 'Text ' ,
@@ -17,7 +19,6 @@ class ExtraPageFieldsExtension extends SiteTreeExtension
1719
1820 public function updateCMSFields (FieldList $ fields )
1921 {
20-
2122 //change Page Name label to Primary Heading - H1 - Only if the title hasn't already been changed
2223 /** @var TextField $titleField */
2324 $ titleField = $ fields ->dataFieldByName ('Title ' );
@@ -28,23 +29,40 @@ public function updateCMSFields(FieldList $fields)
2829 //Add secondary heading - H2
2930 $ fields ->insertAfter (TextField::create ('SubTitle ' , 'Secondary Heading ' ), 'Title ' );
3031
31- if (!in_array ('HasOnAfterUpdateCMSFieldsExtensionPoint ' , class_uses ($ this ->owner ))) {
32+ $ traits = $ this ->traitsUsedRecursive ($ this ->owner ->ClassName );
33+ if (!in_array ('HasOnAfterUpdateCMSFieldsExtensionPoint ' , $ traits )) {
3234 $ this ->afterUpdateCMSFields ($ fields );
3335 }
3436 }
3537
38+ protected function traitsUsedRecursive ($ class , $ traitNames = [])
39+ {
40+ if (!$ class instanceof ReflectionClass) $ class = new ReflectionClass ($ class );
41+ $ traitNames = array_merge ($ traitNames , $ class ->getTraitNames ());
42+ if ($ class ->getParentClass () != false ) {
43+ return array_merge ($ traitNames , $ this ->traitsUsedRecursive ($ class ->getParentClass ()));
44+ }
45+
46+ return $ traitNames ;
47+ }
48+
3649 public function afterUpdateCMSFields (FieldList $ fields )
3750 {
51+ self ::$ runs ++;
52+
3853 /** @var ToggleCompositeField $metaDataChildren */
3954 $ metaDataChildren = $ fields ->fieldByName ('Root.Main.Metadata ' );
40- // ddd($metaDataChildren->fieldByName('MetaDescription'));
55+ $ length = $ this ->owner ->config ()->MetaDescriptionLength ?: $ this ->MetaDescriptionLength ;
56+ if (!$ metaDataChildren ->fieldByName ('MetaDescription ' )) {
57+ ddd (self ::$ runs );
58+ }
59+ $ metaDataChildren ->fieldByName ('MetaDescription ' )->setAttribute ('maxlength ' , $ length );
60+
4161 $ children = array_merge ([$ metaTitle = TextField::create ('MetaTitle ' )], $ metaDataChildren ->getChildren ()->toArray ());
4262 $ fields ->removeFieldFromTab ('Root.Main ' , 'Metadata ' );
4363 $ fields ->addFieldToTab ('Root ' , Tab::create ('Metadata ' ), 'Content ' );
4464
4565 //Add META Title tag to METADATA
46- $ length = $ this ->owner ->config ()->MetaDescriptionLength ?: $ this ->MetaDescriptionLength ;
47- $ metaDataChildren ->fieldByName ('MetaDescription ' )->setAttribute ('maxlength ' , $ length );
4866 $ fields ->addFieldsToTab ('Root.Metadata ' , $ children );
4967
5068 $ metaTitle ->setDescription ('Displayed as the tab/window name; Also displayed in search engine result listings as the page title.<br />
0 commit comments