Complex classes like FormField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FormField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class FormField extends RequestHandler { |
||
31 | |||
32 | /** @see $schemaDataType */ |
||
33 | const SCHEMA_DATA_TYPE_STRING = 'String'; |
||
34 | |||
35 | /** @see $schemaDataType */ |
||
36 | const SCHEMA_DATA_TYPE_HIDDEN = 'Hidden'; |
||
37 | |||
38 | /** @see $schemaDataType */ |
||
39 | const SCHEMA_DATA_TYPE_TEXT = 'Text'; |
||
40 | |||
41 | /** @see $schemaDataType */ |
||
42 | const SCHEMA_DATA_TYPE_HTML = 'HTML'; |
||
43 | |||
44 | /** @see $schemaDataType */ |
||
45 | const SCHEMA_DATA_TYPE_INTEGER = 'Integer'; |
||
46 | |||
47 | /** @see $schemaDataType */ |
||
48 | const SCHEMA_DATA_TYPE_DECIMAL = 'Decimal'; |
||
49 | |||
50 | /** @see $schemaDataType */ |
||
51 | const SCHEMA_DATA_TYPE_MULTISELECT = 'MultiSelect'; |
||
52 | |||
53 | /** @see $schemaDataType */ |
||
54 | const SCHEMA_DATA_TYPE_SINGLESELECT = 'SingleSelect'; |
||
55 | |||
56 | /** @see $schemaDataType */ |
||
57 | const SCHEMA_DATA_TYPE_DATE = 'Date'; |
||
58 | |||
59 | /** @see $schemaDataType */ |
||
60 | const SCHEMA_DATA_TYPE_DATETIME = 'DateTime'; |
||
61 | |||
62 | /** @see $schemaDataType */ |
||
63 | const SCHEMA_DATA_TYPE_TIME = 'Time'; |
||
64 | |||
65 | /** @see $schemaDataType */ |
||
66 | const SCHEMA_DATA_TYPE_BOOLEAN = 'Boolean'; |
||
67 | |||
68 | /** @see $schemaDataType */ |
||
69 | const SCHEMA_DATA_TYPE_CUSTOM = 'Custom'; |
||
70 | |||
71 | /** @see $schemaDataType */ |
||
72 | const SCHEMA_DATA_TYPE_STRUCTURAL = 'Structural'; |
||
73 | |||
74 | /** |
||
75 | * @var Form |
||
76 | */ |
||
77 | protected $form; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $name; |
||
83 | |||
84 | /** |
||
85 | * @var null|string |
||
86 | */ |
||
87 | protected $title; |
||
88 | |||
89 | /** |
||
90 | * @var mixed |
||
91 | */ |
||
92 | protected $value; |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $message; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $messageType; |
||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $extraClass; |
||
108 | |||
109 | /** |
||
110 | * Adds a title attribute to the markup. |
||
111 | * |
||
112 | * @var string |
||
113 | * |
||
114 | * @todo Implement in all subclasses |
||
115 | */ |
||
116 | protected $description; |
||
117 | |||
118 | /** |
||
119 | * Extra CSS classes for the FormField container. |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $extraClasses; |
||
124 | |||
125 | /** |
||
126 | * @config |
||
127 | * @var array $default_classes The default classes to apply to the FormField |
||
128 | */ |
||
129 | private static $default_classes = []; |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @var bool |
||
134 | */ |
||
135 | public $dontEscape; |
||
136 | |||
137 | /** |
||
138 | * Right-aligned, contextual label for the field. |
||
139 | * |
||
140 | * @var string |
||
141 | */ |
||
142 | protected $rightTitle; |
||
143 | |||
144 | /** |
||
145 | * Left-aligned, contextual label for the field. |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | protected $leftTitle; |
||
150 | |||
151 | /** |
||
152 | * Stores a reference to the FieldList that contains this object. |
||
153 | * |
||
154 | * @var FieldList |
||
155 | */ |
||
156 | protected $containerFieldList; |
||
157 | |||
158 | /** |
||
159 | * @var bool |
||
160 | */ |
||
161 | protected $readonly = false; |
||
162 | |||
163 | /** |
||
164 | * @var bool |
||
165 | */ |
||
166 | protected $disabled = false; |
||
167 | |||
168 | /** |
||
169 | * Custom validation message for the field. |
||
170 | * |
||
171 | * @var string |
||
172 | */ |
||
173 | protected $customValidationMessage = ''; |
||
174 | |||
175 | /** |
||
176 | * Name of the template used to render this form field. If not set, then will look up the class |
||
177 | * ancestry for the first matching template where the template name equals the class name. |
||
178 | * |
||
179 | * To explicitly use a custom template or one named other than the form field see |
||
180 | * {@link setTemplate()}. |
||
181 | * |
||
182 | * @var string |
||
183 | */ |
||
184 | protected $template; |
||
185 | |||
186 | /** |
||
187 | * Name of the template used to render this form field. If not set, then will look up the class |
||
188 | * ancestry for the first matching template where the template name equals the class name. |
||
189 | * |
||
190 | * To explicitly use a custom template or one named other than the form field see |
||
191 | * {@link setFieldHolderTemplate()}. |
||
192 | * |
||
193 | * @var string |
||
194 | */ |
||
195 | protected $fieldHolderTemplate; |
||
196 | |||
197 | /** |
||
198 | * @var string |
||
199 | */ |
||
200 | protected $smallFieldHolderTemplate; |
||
201 | |||
202 | /** |
||
203 | * All attributes on the form field (not the field holder). |
||
204 | * |
||
205 | * Partially determined based on other instance properties. |
||
206 | * |
||
207 | * @see getAttributes() |
||
208 | * |
||
209 | * @var array |
||
210 | */ |
||
211 | protected $attributes = []; |
||
212 | |||
213 | /** |
||
214 | * The data type backing the field. Represents the type of value the |
||
215 | * form expects to receive via a postback. Should be set in subclasses. |
||
216 | * |
||
217 | * The values allowed in this list include: |
||
218 | * |
||
219 | * - String: Single line text |
||
220 | * - Hidden: Hidden field which is posted back without modification |
||
221 | * - Text: Multi line text |
||
222 | * - HTML: Rich html text |
||
223 | * - Integer: Whole number value |
||
224 | * - Decimal: Decimal value |
||
225 | * - MultiSelect: Select many from source |
||
226 | * - SingleSelect: Select one from source |
||
227 | * - Date: Date only |
||
228 | * - DateTime: Date and time |
||
229 | * - Time: Time only |
||
230 | * - Boolean: Yes or no |
||
231 | * - Custom: Custom type declared by the front-end component. For fields with this type, |
||
232 | * the component property is mandatory, and will determine the posted value for this field. |
||
233 | * - Structural: Represents a field that is NOT posted back. This may contain other fields, |
||
234 | * or simply be a block of stand-alone content. As with 'Custom', |
||
235 | * the component property is mandatory if this is assigned. |
||
236 | * |
||
237 | * Each value has an equivalent constant, e.g. {@link self::SCHEMA_DATA_TYPE_STRING}. |
||
238 | * |
||
239 | * @var string |
||
240 | */ |
||
241 | protected $schemaDataType; |
||
242 | |||
243 | /** |
||
244 | * The type of front-end component to render the FormField as. |
||
245 | * |
||
246 | * @var string |
||
247 | */ |
||
248 | protected $schemaComponent; |
||
249 | |||
250 | /** |
||
251 | * Structured schema data representing the FormField. |
||
252 | * Used to render the FormField as a ReactJS Component on the front-end. |
||
253 | * |
||
254 | * @var array |
||
255 | */ |
||
256 | protected $schemaData = []; |
||
257 | |||
258 | /** |
||
259 | * Structured schema state representing the FormField's current data and validation. |
||
260 | * Used to render the FormField as a ReactJS Component on the front-end. |
||
261 | * |
||
262 | * @var array |
||
263 | */ |
||
264 | protected $schemaState = []; |
||
265 | |||
266 | /** |
||
267 | * Takes a field name and converts camelcase to spaced words. Also resolves combined field |
||
268 | * names with dot syntax to spaced words. |
||
269 | * |
||
270 | * Examples: |
||
271 | * |
||
272 | * - 'TotalAmount' will return 'Total Amount' |
||
273 | * - 'Organisation.ZipCode' will return 'Organisation Zip Code' |
||
274 | * |
||
275 | * @param string $fieldName |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public static function name_to_label($fieldName) { |
||
290 | |||
291 | /** |
||
292 | * Construct and return HTML tag. |
||
293 | * |
||
294 | * @param string $tag |
||
295 | * @param array $attributes |
||
296 | * @param null|string $content |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public static function create_tag($tag, $attributes, $content = null) { |
||
321 | |||
322 | /** |
||
323 | * Creates a new field. |
||
324 | * |
||
325 | * @param string $name The internal field name, passed to forms. |
||
326 | * @param null|string $title The human-readable field label. |
||
327 | * @param mixed $value The value of the field. |
||
328 | */ |
||
329 | public function __construct($name, $title = null, $value = null) { |
||
346 | |||
347 | /** |
||
348 | * Set up the default classes for the form. This is done on construct so that the default classes can be removed |
||
349 | * after instantiation |
||
350 | */ |
||
351 | protected function setupDefaultClasses() { |
||
359 | |||
360 | /** |
||
361 | * Return a link to this field. |
||
362 | * |
||
363 | * @param string $action |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | public function Link($action = null) { |
||
370 | |||
371 | /** |
||
372 | * Returns the HTML ID of the field. |
||
373 | * |
||
374 | * The ID is generated as FormName_FieldName. All Field functions should ensure that this ID is |
||
375 | * included in the field. |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | public function ID() { |
||
382 | |||
383 | /** |
||
384 | * Returns the HTML ID for the form field holder element. |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | public function HolderID() { |
||
391 | |||
392 | /** |
||
393 | * Returns the current {@link FormTemplateHelper} on either the parent |
||
394 | * Form or the global helper set through the {@link Injector} layout. |
||
395 | * |
||
396 | * To customize a single {@link FormField}, use {@link setTemplate} and |
||
397 | * provide a custom template name. |
||
398 | * |
||
399 | * @return FormTemplateHelper |
||
400 | */ |
||
401 | public function getTemplateHelper() { |
||
408 | |||
409 | /** |
||
410 | * Returns the field name. |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | public function getName() { |
||
417 | |||
418 | /** |
||
419 | * Returns the field message, used by form validation. |
||
420 | * |
||
421 | * Use {@link setError()} to set this property. |
||
422 | * |
||
423 | * @return string |
||
424 | */ |
||
425 | public function Message() { |
||
428 | |||
429 | /** |
||
430 | * Returns the field message type. |
||
431 | * |
||
432 | * Arbitrary value which is mostly used for CSS classes in the rendered HTML, e.g "required". |
||
433 | * |
||
434 | * Use {@link setError()} to set this property. |
||
435 | * |
||
436 | * @return string |
||
437 | */ |
||
438 | public function MessageType() { |
||
441 | |||
442 | /** |
||
443 | * Returns the field value. |
||
444 | * |
||
445 | * @return mixed |
||
446 | */ |
||
447 | public function Value() { |
||
450 | |||
451 | /** |
||
452 | * Method to save this form field into the given {@link DataObject}. |
||
453 | * |
||
454 | * By default, makes use of $this->dataValue() |
||
455 | * |
||
456 | * @param DataObjectInterface $record DataObject to save data into |
||
457 | */ |
||
458 | public function saveInto(DataObjectInterface $record) { |
||
463 | |||
464 | /** |
||
465 | * Returns the field value suitable for insertion into the data object. |
||
466 | * |
||
467 | * @return mixed |
||
468 | */ |
||
469 | public function dataValue() { |
||
472 | |||
473 | /** |
||
474 | * Returns the field label - used by templates. |
||
475 | * |
||
476 | * @return string |
||
477 | */ |
||
478 | public function Title() { |
||
481 | |||
482 | /** |
||
483 | * @param string $title |
||
484 | * |
||
485 | * @return $this |
||
486 | */ |
||
487 | public function setTitle($title) { |
||
492 | |||
493 | /** |
||
494 | * Gets the contextual label than can be used for additional field description. |
||
495 | * Can be shown to the right or under the field in question. |
||
496 | * |
||
497 | * @return string Contextual label text. |
||
498 | */ |
||
499 | public function RightTitle() { |
||
502 | |||
503 | /** |
||
504 | * @param string $rightTitle |
||
505 | * |
||
506 | * @return $this |
||
507 | */ |
||
508 | public function setRightTitle($rightTitle) { |
||
513 | |||
514 | /** |
||
515 | * @return string |
||
516 | */ |
||
517 | public function LeftTitle() { |
||
520 | |||
521 | /** |
||
522 | * @param string $leftTitle |
||
523 | * |
||
524 | * @return $this |
||
525 | */ |
||
526 | public function setLeftTitle($leftTitle) { |
||
531 | |||
532 | /** |
||
533 | * Compiles all CSS-classes. Optionally includes a "nolabel" class if no title was set on the |
||
534 | * FormField. |
||
535 | * |
||
536 | * Uses {@link Message()} and {@link MessageType()} to add validation error classes which can |
||
537 | * be used to style the contained tags. |
||
538 | * |
||
539 | * @return string |
||
540 | */ |
||
541 | public function extraClass() { |
||
567 | |||
568 | /** |
||
569 | * Add one or more CSS-classes to the FormField container. |
||
570 | * |
||
571 | * Multiple class names should be space delimited. |
||
572 | * |
||
573 | * @param string $class |
||
574 | * |
||
575 | * @return $this |
||
576 | */ |
||
577 | public function addExtraClass($class) { |
||
586 | |||
587 | /** |
||
588 | * Remove one or more CSS-classes from the FormField container. |
||
589 | * |
||
590 | * @param string $class |
||
591 | * |
||
592 | * @return $this |
||
593 | */ |
||
594 | public function removeExtraClass($class) { |
||
603 | |||
604 | /** |
||
605 | * Set an HTML attribute on the field element, mostly an <input> tag. |
||
606 | * |
||
607 | * Some attributes are best set through more specialized methods, to avoid interfering with |
||
608 | * built-in behaviour: |
||
609 | * |
||
610 | * - 'class': {@link addExtraClass()} |
||
611 | * - 'title': {@link setDescription()} |
||
612 | * - 'value': {@link setValue} |
||
613 | * - 'name': {@link setName} |
||
614 | * |
||
615 | * Caution: this doesn't work on most fields which are composed of more than one HTML form |
||
616 | * field. |
||
617 | * |
||
618 | * @param string $name |
||
619 | * @param string $value |
||
620 | * |
||
621 | * @return $this |
||
622 | */ |
||
623 | public function setAttribute($name, $value) { |
||
628 | |||
629 | /** |
||
630 | * Get an HTML attribute defined by the field, or added through {@link setAttribute()}. |
||
631 | * |
||
632 | * Caution: this doesn't work on all fields, see {@link setAttribute()}. |
||
633 | * |
||
634 | * @return null|string |
||
635 | */ |
||
636 | public function getAttribute($name) { |
||
645 | |||
646 | /** |
||
647 | * Allows customization through an 'updateAttributes' hook on the base class. |
||
648 | * Existing attributes are passed in as the first argument and can be manipulated, |
||
649 | * but any attributes added through a subclass implementation won't be included. |
||
650 | * |
||
651 | * @return array |
||
652 | */ |
||
653 | public function getAttributes() { |
||
675 | |||
676 | /** |
||
677 | * Custom attributes to process. Falls back to {@link getAttributes()}. |
||
678 | * |
||
679 | * If at least one argument is passed as a string, all arguments act as excludes, by name. |
||
680 | * |
||
681 | * @param array $attributes |
||
682 | * |
||
683 | * @return string |
||
684 | */ |
||
685 | public function getAttributesHTML($attributes = null) { |
||
722 | |||
723 | /** |
||
724 | * Returns a version of a title suitable for insertion into an HTML attribute. |
||
725 | * |
||
726 | * @return string |
||
727 | */ |
||
728 | public function attrTitle() { |
||
731 | |||
732 | /** |
||
733 | * Returns a version of a title suitable for insertion into an HTML attribute. |
||
734 | * |
||
735 | * @return string |
||
736 | */ |
||
737 | public function attrValue() { |
||
740 | |||
741 | /** |
||
742 | * Set the field value. |
||
743 | * |
||
744 | * @param mixed $value |
||
745 | * @param null|array|DataObject $data {@see Form::loadDataFrom} |
||
746 | * |
||
747 | * @return $this |
||
748 | */ |
||
749 | public function setValue($value) { |
||
753 | |||
754 | /** |
||
755 | * Set the field name. |
||
756 | * |
||
757 | * @param string $name |
||
758 | * |
||
759 | * @return $this |
||
760 | */ |
||
761 | public function setName($name) { |
||
766 | |||
767 | /** |
||
768 | * Set the container form. |
||
769 | * |
||
770 | * This is called automatically when fields are added to forms. |
||
771 | * |
||
772 | * @param Form $form |
||
773 | * |
||
774 | * @return $this |
||
775 | */ |
||
776 | public function setForm($form) { |
||
781 | |||
782 | /** |
||
783 | * Get the currently used form. |
||
784 | * |
||
785 | * @return Form |
||
786 | */ |
||
787 | public function getForm() { |
||
790 | |||
791 | /** |
||
792 | * Return true if security token protection is enabled on the parent {@link Form}. |
||
793 | * |
||
794 | * @return bool |
||
795 | */ |
||
796 | public function securityTokenEnabled() { |
||
805 | |||
806 | /** |
||
807 | * Sets the error message to be displayed on the form field. |
||
808 | * |
||
809 | * Allows HTML content, so remember to use Convert::raw2xml(). |
||
810 | * |
||
811 | * @param string $message |
||
812 | * @param string $messageType |
||
813 | * |
||
814 | * @return $this |
||
815 | */ |
||
816 | public function setError($message, $messageType) { |
||
822 | |||
823 | /** |
||
824 | * Set the custom error message to show instead of the default format. |
||
825 | * |
||
826 | * Different from setError() as that appends it to the standard error messaging. |
||
827 | * |
||
828 | * @param string $customValidationMessage |
||
829 | * |
||
830 | * @return $this |
||
831 | */ |
||
832 | public function setCustomValidationMessage($customValidationMessage) { |
||
837 | |||
838 | /** |
||
839 | * Get the custom error message for this form field. If a custom message has not been defined |
||
840 | * then just return blank. The default error is defined on {@link Validator}. |
||
841 | * |
||
842 | * @return string |
||
843 | */ |
||
844 | public function getCustomValidationMessage() { |
||
847 | |||
848 | /** |
||
849 | * Set name of template (without path or extension). |
||
850 | * |
||
851 | * Caution: Not consistently implemented in all subclasses, please check the {@link Field()} |
||
852 | * method on the subclass for support. |
||
853 | * |
||
854 | * @param string $template |
||
855 | * |
||
856 | * @return $this |
||
857 | */ |
||
858 | public function setTemplate($template) { |
||
863 | |||
864 | /** |
||
865 | * @return string |
||
866 | */ |
||
867 | public function getTemplate() { |
||
870 | |||
871 | /** |
||
872 | * @return string |
||
873 | */ |
||
874 | public function getFieldHolderTemplate() { |
||
877 | |||
878 | /** |
||
879 | * Set name of template (without path or extension) for the holder, which in turn is |
||
880 | * responsible for rendering {@link Field()}. |
||
881 | * |
||
882 | * Caution: Not consistently implemented in all subclasses, please check the {@link Field()} |
||
883 | * method on the subclass for support. |
||
884 | * |
||
885 | * @param string $fieldHolderTemplate |
||
886 | * |
||
887 | * @return $this |
||
888 | */ |
||
889 | public function setFieldHolderTemplate($fieldHolderTemplate) { |
||
894 | |||
895 | /** |
||
896 | * @return string |
||
897 | */ |
||
898 | public function getSmallFieldHolderTemplate() { |
||
901 | |||
902 | /** |
||
903 | * Set name of template (without path or extension) for the small holder, which in turn is |
||
904 | * responsible for rendering {@link Field()}. |
||
905 | * |
||
906 | * Caution: Not consistently implemented in all subclasses, please check the {@link Field()} |
||
907 | * method on the subclass for support. |
||
908 | * |
||
909 | * @param string $smallFieldHolderTemplate |
||
910 | * |
||
911 | * @return $this |
||
912 | */ |
||
913 | public function setSmallFieldHolderTemplate($smallFieldHolderTemplate) { |
||
918 | |||
919 | /** |
||
920 | * Returns the form field. |
||
921 | * |
||
922 | * Although FieldHolder is generally what is inserted into templates, all of the field holder |
||
923 | * templates make use of $Field. It's expected that FieldHolder will give you the "complete" |
||
924 | * representation of the field on the form, whereas Field will give you the core editing widget, |
||
925 | * such as an input tag. |
||
926 | * |
||
927 | * @param array $properties |
||
928 | * |
||
929 | * @return string |
||
930 | */ |
||
931 | public function Field($properties = array()) { |
||
951 | |||
952 | /** |
||
953 | * Returns a "field holder" for this field. |
||
954 | * |
||
955 | * Forms are constructed by concatenating a number of these field holders. |
||
956 | * |
||
957 | * The default field holder is a label and a form field inside a div. |
||
958 | * |
||
959 | * @see FieldHolder.ss |
||
960 | * |
||
961 | * @param array $properties |
||
962 | * |
||
963 | * @return string |
||
964 | */ |
||
965 | public function FieldHolder($properties = array()) { |
||
974 | |||
975 | /** |
||
976 | * Returns a restricted field holder used within things like FieldGroups. |
||
977 | * |
||
978 | * @param array $properties |
||
979 | * |
||
980 | * @return string |
||
981 | */ |
||
982 | public function SmallFieldHolder($properties = array()) { |
||
991 | |||
992 | /** |
||
993 | * Returns an array of templates to use for rendering {@link FieldHolder}. |
||
994 | * |
||
995 | * @return array |
||
996 | */ |
||
997 | public function getTemplates() { |
||
1000 | |||
1001 | /** |
||
1002 | * Returns an array of templates to use for rendering {@link FieldHolder}. |
||
1003 | * |
||
1004 | * @return array |
||
1005 | */ |
||
1006 | public function getFieldHolderTemplates() { |
||
1012 | |||
1013 | /** |
||
1014 | * Returns an array of templates to use for rendering {@link SmallFieldHolder}. |
||
1015 | * |
||
1016 | * @return array |
||
1017 | */ |
||
1018 | public function getSmallFieldHolderTemplates() { |
||
1024 | |||
1025 | |||
1026 | /** |
||
1027 | * Generate an array of class name strings to use for rendering this form field into HTML. |
||
1028 | * |
||
1029 | * @param string $customTemplate |
||
1030 | * @param string $customTemplateSuffix |
||
1031 | * |
||
1032 | * @return array |
||
1033 | */ |
||
1034 | private function _templates($customTemplate = null, $customTemplateSuffix = null) { |
||
1051 | |||
1052 | /** |
||
1053 | * Returns true if this field is a composite field. |
||
1054 | * |
||
1055 | * To create composite field types, you should subclass {@link CompositeField}. |
||
1056 | * |
||
1057 | * @return bool |
||
1058 | */ |
||
1059 | public function isComposite() { |
||
1062 | |||
1063 | /** |
||
1064 | * Returns true if this field has its own data. |
||
1065 | * |
||
1066 | * Some fields, such as titles and composite fields, don't actually have any data. It doesn't |
||
1067 | * make sense for data-focused methods to look at them. By overloading hasData() to return |
||
1068 | * false, you can prevent any data-focused methods from looking at it. |
||
1069 | * |
||
1070 | * @see FieldList::collateDataFields() |
||
1071 | * |
||
1072 | * @return bool |
||
1073 | */ |
||
1074 | public function hasData() { |
||
1077 | |||
1078 | /** |
||
1079 | * @return bool |
||
1080 | */ |
||
1081 | public function isReadonly() { |
||
1084 | |||
1085 | /** |
||
1086 | * Sets a read-only flag on this FormField. |
||
1087 | * |
||
1088 | * Use performReadonlyTransformation() to transform this instance. |
||
1089 | * |
||
1090 | * Setting this to false has no effect on the field. |
||
1091 | * |
||
1092 | * @param bool $readonly |
||
1093 | * |
||
1094 | * @return $this |
||
1095 | */ |
||
1096 | public function setReadonly($readonly) { |
||
1101 | |||
1102 | /** |
||
1103 | * @return bool |
||
1104 | */ |
||
1105 | public function isDisabled() { |
||
1108 | |||
1109 | /** |
||
1110 | * Sets a disabled flag on this FormField. |
||
1111 | * |
||
1112 | * Use performDisabledTransformation() to transform this instance. |
||
1113 | * |
||
1114 | * Setting this to false has no effect on the field. |
||
1115 | * |
||
1116 | * @param bool $disabled |
||
1117 | * |
||
1118 | * @return $this |
||
1119 | */ |
||
1120 | public function setDisabled($disabled) { |
||
1125 | |||
1126 | /** |
||
1127 | * Returns a read-only version of this field. |
||
1128 | * |
||
1129 | * @return FormField |
||
1130 | */ |
||
1131 | public function performReadonlyTransformation() { |
||
1144 | |||
1145 | /** |
||
1146 | * Return a disabled version of this field. |
||
1147 | * |
||
1148 | * Tries to find a class of the class name of this field suffixed with "_Disabled", failing |
||
1149 | * that, finds a method {@link setDisabled()}. |
||
1150 | * |
||
1151 | * @return FormField |
||
1152 | */ |
||
1153 | public function performDisabledTransformation() { |
||
1166 | |||
1167 | /** |
||
1168 | * @param FormTransformation $transformation |
||
1169 | * |
||
1170 | * @return mixed |
||
1171 | */ |
||
1172 | public function transform(FormTransformation $transformation) { |
||
1175 | |||
1176 | /** |
||
1177 | * @param string $class |
||
1178 | * |
||
1179 | * @return int |
||
1180 | */ |
||
1181 | public function hasClass($class) { |
||
1188 | |||
1189 | /** |
||
1190 | * Returns the field type. |
||
1191 | * |
||
1192 | * The field type is the class name with the word Field dropped off the end, all lowercase. |
||
1193 | * |
||
1194 | * It's handy for assigning HTML classes. Doesn't signify the <input type> attribute. |
||
1195 | * |
||
1196 | * @see {link getAttributes()}. |
||
1197 | * |
||
1198 | * @return string |
||
1199 | */ |
||
1200 | public function Type() { |
||
1203 | |||
1204 | /** |
||
1205 | * @deprecated 4.0 Use FormField::create_tag() |
||
1206 | * |
||
1207 | * @param string $tag |
||
1208 | * @param array $attributes |
||
1209 | * @param null|string $content |
||
1210 | * |
||
1211 | * @return string |
||
1212 | */ |
||
1213 | public function createTag($tag, $attributes, $content = null) { |
||
1218 | |||
1219 | /** |
||
1220 | * Abstract method each {@link FormField} subclass must implement, determines whether the field |
||
1221 | * is valid or not based on the value. |
||
1222 | * |
||
1223 | * @todo Make this abstract. |
||
1224 | * |
||
1225 | * @param Validator |
||
1226 | * |
||
1227 | * @return bool |
||
1228 | */ |
||
1229 | public function validate($validator) { |
||
1232 | |||
1233 | /** |
||
1234 | * Describe this field, provide help text for it. |
||
1235 | * |
||
1236 | * By default, renders as a <span class="description"> underneath the form field. |
||
1237 | * |
||
1238 | * @param string $description |
||
1239 | * |
||
1240 | * @return $this |
||
1241 | */ |
||
1242 | public function setDescription($description) { |
||
1247 | |||
1248 | /** |
||
1249 | * @return string |
||
1250 | */ |
||
1251 | public function getDescription() { |
||
1254 | |||
1255 | /** |
||
1256 | * @return string |
||
1257 | */ |
||
1258 | public function debug() { |
||
1268 | |||
1269 | /** |
||
1270 | * This function is used by the template processor. If you refer to a field as a $ variable, it |
||
1271 | * will return the $Field value. |
||
1272 | * |
||
1273 | * @return string |
||
1274 | */ |
||
1275 | public function forTemplate() { |
||
1278 | |||
1279 | /** |
||
1280 | * @return bool |
||
1281 | */ |
||
1282 | public function Required() { |
||
1289 | |||
1290 | /** |
||
1291 | * Set the FieldList that contains this field. |
||
1292 | * |
||
1293 | * @param FieldList $containerFieldList |
||
1294 | * |
||
1295 | * @return FieldList |
||
1296 | */ |
||
1297 | public function setContainerFieldList($containerFieldList) { |
||
1302 | |||
1303 | /** |
||
1304 | * Get the FieldList that contains this field. |
||
1305 | * |
||
1306 | * @return FieldList |
||
1307 | */ |
||
1308 | public function getContainerFieldList() { |
||
1311 | |||
1312 | /** |
||
1313 | * @return null|FieldList |
||
1314 | */ |
||
1315 | public function rootFieldList() { |
||
1327 | |||
1328 | /** |
||
1329 | * Returns another instance of this field, but "cast" to a different class. The logic tries to |
||
1330 | * retain all of the instance properties, and may be overloaded by subclasses to set additional |
||
1331 | * ones. |
||
1332 | * |
||
1333 | * Assumes the standard FormField parameter signature with its name as the only mandatory |
||
1334 | * argument. Mainly geared towards creating *_Readonly or *_Disabled subclasses of the same |
||
1335 | * type, or casting to a {@link ReadonlyField}. |
||
1336 | * |
||
1337 | * Does not copy custom field templates, since they probably won't apply to the new instance. |
||
1338 | * |
||
1339 | * @param mixed $classOrCopy Class name for copy, or existing copy instance to update |
||
1340 | * |
||
1341 | * @return FormField |
||
1342 | */ |
||
1343 | public function castedCopy($classOrCopy) { |
||
1369 | |||
1370 | /** |
||
1371 | * Determine if escaping of this field should be disabled |
||
1372 | * |
||
1373 | * @param bool $dontEscape |
||
1374 | * @return $this |
||
1375 | */ |
||
1376 | public function setDontEscape($dontEscape) { |
||
1380 | |||
1381 | /** |
||
1382 | * Determine if escaping is disabled |
||
1383 | * |
||
1384 | * @return bool |
||
1385 | */ |
||
1386 | public function getDontEscape() { |
||
1389 | |||
1390 | /** |
||
1391 | * Sets the component type the FormField will be rendered as on the front-end. |
||
1392 | * |
||
1393 | * @param string $componentType |
||
1394 | * @return FormField |
||
1395 | */ |
||
1396 | public function setSchemaComponent($componentType) { |
||
1400 | |||
1401 | /** |
||
1402 | * Gets the type of front-end component the FormField will be rendered as. |
||
1403 | * |
||
1404 | * @return string |
||
1405 | */ |
||
1406 | public function getSchemaComponent() { |
||
1409 | |||
1410 | /** |
||
1411 | * Sets the schema data used for rendering the field on the front-end. |
||
1412 | * Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}. |
||
1413 | * Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored. |
||
1414 | * If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`. |
||
1415 | * |
||
1416 | * @param array $schemaData - The data to be merged with $this->schemaData. |
||
1417 | * @return FormField |
||
1418 | * |
||
1419 | * @todo Add deep merging of arrays like `data` and `attributes`. |
||
1420 | */ |
||
1421 | public function setSchemaData($schemaData = []) { |
||
1427 | |||
1428 | /** |
||
1429 | * Gets the schema data used to render the FormField on the front-end. |
||
1430 | * |
||
1431 | * @return array |
||
1432 | */ |
||
1433 | public function getSchemaData() { |
||
1436 | |||
1437 | /** |
||
1438 | * @todo Throw exception if value is missing, once a form field schema is mandatory across the CMS |
||
1439 | * |
||
1440 | * @return string |
||
1441 | */ |
||
1442 | public function getSchemaDataType() { |
||
1445 | |||
1446 | /** |
||
1447 | * Gets the defaults for $schemaData. |
||
1448 | * The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaData()} are ignored. |
||
1449 | * Instead the `data` array should be used to pass around ad hoc data. |
||
1450 | * |
||
1451 | * @return array |
||
1452 | */ |
||
1453 | public function getSchemaDataDefaults() { |
||
1473 | |||
1474 | /** |
||
1475 | * Sets the schema data used for rendering the field on the front-end. |
||
1476 | * Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}. |
||
1477 | * Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored. |
||
1478 | * If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`. |
||
1479 | * |
||
1480 | * @param array $schemaData - The data to be merged with $this->schemaData. |
||
1481 | * @return FormField |
||
1482 | * |
||
1483 | * @todo Add deep merging of arrays like `data` and `attributes`. |
||
1484 | */ |
||
1485 | public function setSchemaState($schemaState = []) { |
||
1491 | |||
1492 | /** |
||
1493 | * Gets the schema state used to render the FormField on the front-end. |
||
1494 | * |
||
1495 | * @return array |
||
1496 | */ |
||
1497 | public function getSchemaState() { |
||
1500 | |||
1501 | /** |
||
1502 | * Gets the defaults for $schemaState. |
||
1503 | * The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaState()} are ignored. |
||
1504 | * Instead the `data` array should be used to pass around ad hoc data. |
||
1505 | * Includes validation data if the field is associated to a {@link Form}, |
||
1506 | * and {@link Form->validate()} has been called. |
||
1507 | * |
||
1508 | * @return array |
||
1509 | */ |
||
1510 | public function getSchemaStateDefaults() { |
||
1532 | |||
1533 | } |
||
1534 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: