@@ -357,7 +357,7 @@ public function getAttributes() {
357
357
'id ' => $ this ->ID (),
358
358
'disabled ' => $ this ->isDisabled (),
359
359
);
360
-
360
+
361
361
return array_merge ($ attrs , $ this ->attributes );
362
362
}
363
363
@@ -709,10 +709,9 @@ public function setDisabled($bool) {
709
709
* Returns a readonly version of this field
710
710
*/
711
711
public function performReadonlyTransformation () {
712
- $ field = new ReadonlyField ($ this ->name , $ this ->title , $ this ->value );
713
- $ field ->addExtraClass ($ this ->extraClass ());
714
- $ field ->setForm ($ this ->form );
715
- return $ field ;
712
+ $ copy = $ this ->castedCopy ('ReadonlyField ' );
713
+ $ copy ->setReadonly (true );
714
+ return $ copy ;
716
715
}
717
716
718
717
/**
@@ -723,14 +722,15 @@ public function performReadonlyTransformation() {
723
722
* @return FormField
724
723
*/
725
724
public function performDisabledTransformation () {
726
- $ clone = clone $ this ;
727
- $ disabledClassName = $ clone ->class . '_Disabled ' ;
725
+ $ disabledClassName = $ this ->class . '_Disabled ' ;
728
726
if (ClassInfo::exists ($ disabledClassName )) {
729
- return new $ disabledClassName ( $ this -> name , $ this -> title , $ this ->value );
727
+ $ clone = $ this ->castedCopy ( $ disabledClassName );
730
728
} else {
729
+ $ clone = clone $ this ;
731
730
$ clone ->setDisabled (true );
732
- return $ clone ;
733
731
}
732
+
733
+ return $ clone ;
734
734
}
735
735
736
736
public function transform (FormTransformation $ trans ) {
@@ -774,7 +774,8 @@ public function validate($validator) {
774
774
775
775
/**
776
776
* Describe this field, provide help text for it.
777
- * By default, renders as a "title" attribute on the form field.
777
+ * By default, renders as a <span class="description">
778
+ * underneath the form field.
778
779
*
779
780
* @return string Description
780
781
*/
@@ -828,5 +829,43 @@ public function rootFieldList() {
828
829
if (is_object ($ this ->containerFieldList )) return $ this ->containerFieldList ->rootFieldList ();
829
830
else user_error ("rootFieldList() called on $ this ->class object without a containerFieldList " , E_USER_ERROR );
830
831
}
832
+
833
+ /**
834
+ * Returns another instance of this field, but "cast" to a different class.
835
+ * The logic tries to retain all of the instance properties,
836
+ * and may be overloaded by subclasses to set additional ones.
837
+ *
838
+ * Assumes the standard FormField parameter signature with
839
+ * its name as the only mandatory argument. Mainly geared towards
840
+ * creating *_Readonly or *_Disabled subclasses of the same type,
841
+ * or casting to a {@link ReadonlyField}.
842
+ *
843
+ * Does not copy custom field templates, since they probably won't apply to
844
+ * the new instance.
845
+ *
846
+ * @param String $classOrCopy Class name for copy, or existing copy instance to update
847
+ * @return FormField
848
+ */
849
+ public function castedCopy ($ classOrCopy ) {
850
+ $ field = (is_object ($ classOrCopy )) ? $ classOrCopy : new $ classOrCopy ($ this ->name );
851
+ $ field
852
+ ->setValue ($ this ->value ) // get value directly from property, avoid any conversions
853
+ ->setForm ($ this ->form )
854
+ ->setTitle ($ this ->Title ())
855
+ ->setLeftTitle ($ this ->LeftTitle ())
856
+ ->setRightTitle ($ this ->RightTitle ())
857
+ ->addExtraClass ($ this ->extraClass ())
858
+ ->setDescription ($ this ->getDescription ());
859
+
860
+ // Only include built-in attributes, ignore anything
861
+ // set through getAttributes(), since those might change important characteristics
862
+ // of the field, e.g. its "type" attribute.
863
+ foreach ($ this ->attributes as $ k => $ v ) {
864
+ $ field ->setAttribute ($ k , $ v );
865
+ }
866
+ $ field ->dontEscape = $ this ->dontEscape ;
867
+
868
+ return $ field ;
869
+ }
831
870
832
871
}
0 commit comments