Completed
Push — master ( a6ff96...8afff1 )
by Daniel
10:22
created

ReadonlyField::getIncludeHiddenField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
/**
6
 * Read-only field to display a non-editable value with a label.
7
 * Consider using an {@link LabelField} if you just need a label-less
8
 * value display.
9
 */
10
class ReadonlyField extends FormField {
11
12
	protected $readonly = true;
13
14
	/**
15
	 * Include a hidden field in the HTML for the readonly field
16
	 * @var boolean
17
	 */
18
	protected $includeHiddenField = false;
19
20
	protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
21
22
	/**
23
	 * If true, a hidden field will be included in the HTML for the readonly field.
24
	 *
25
	 * This can be useful if you need to pass the data through on the form submission, as
26
	 * long as it's okay than an attacker could change the data before it's submitted.
27
	 *
28
	 * This is disabled by default as it can introduce security holes if the data is not
29
	 * allowed to be modified by the user.
30
	 *
31
	 * @param boolean $includeHiddenField
32
	 * @return $this
33
	 */
34
	public function setIncludeHiddenField($includeHiddenField) {
35
		$this->includeHiddenField = $includeHiddenField;
36
		return $this;
37
	}
38
39
	/**
40
	 * @return bool
41
	 */
42
	public function getIncludeHiddenField() {
43
		return $this->includeHiddenField;
44
	}
45
46
	public function performReadonlyTransformation() {
47
		return clone $this;
48
	}
49
50
	public function Type() {
51
		return 'readonly';
52
	}
53
54
	public function castingHelper($field) {
55
		// Get dynamic cast for 'Value' field
56
		if(strcasecmp($field, 'Value') === 0) {
57
			return $this->getValueCast();
58
		}
59
60
		// Fall back to default casting
61
		return parent::castingHelper($field);
62
	}
63
64
	public function getSchemaStateDefaults() {
65
		$values = parent::getSchemaStateDefaults();
66
		// Suppress `<i>('none')</i>` from appearing in react as a literal
67
		$values['value'] = $this->dataValue();
68
		return $values;
69
	}
70
71
72
	/**
73
	 * If $dontEscape is true the returned value will be plain text
74
	 * and should be escaped in templates via .XML
75
	 *
76
	 * If $dontEscape is false the returned value will be safely encoded,
77
	 * but should not be escaped by the frontend.
78
	 *
79
	 * @return mixed|string
80
	 */
81
	public function Value() {
82
		// Get raw value
83
		$value = $this->dataValue();
84
		if($value) {
85
			return $value;
86
		}
87
88
		// "none" text
89
		$label = _t('FormField.NONE', 'none');
90
		return "<i>('{$label}')</i>";
91
	}
92
93
	/**
94
	 * Get custom cating helper for Value() field
95
	 *
96
	 * @return string
97
	 */
98
	public function getValueCast() {
99
		// Casting class for 'none' text
100
		$value = $this->dataValue();
101
		if(empty($value)) {
102
			return 'HTMLFragment';
103
		}
104
105
		// Use default casting
106
		return $this->config()->casting['Value'];
107
	}
108
109
}
110