|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This field lets you put an arbitrary piece of HTML into your forms. |
|
5
|
|
|
* |
|
6
|
|
|
* <code> |
|
7
|
|
|
* new LiteralField ( |
|
8
|
|
|
* $name = "literalfield", |
|
9
|
|
|
* $content = '<b>some bold text</b> and <a href="http://silverstripe.com">a link</a>' |
|
10
|
|
|
* ) |
|
11
|
|
|
* </code> |
|
12
|
|
|
* |
|
13
|
|
|
* @package forms |
|
14
|
|
|
* @subpackage fields-dataless |
|
15
|
|
|
*/ |
|
16
|
|
|
class LiteralField extends DatalessField { |
|
17
|
|
|
|
|
18
|
|
|
private static $casting = [ |
|
19
|
|
|
'Value' => 'HTMLFragment', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string|FormField |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $content; |
|
26
|
|
|
|
|
27
|
|
|
protected $schemaDataType = self::SCHEMA_DATA_TYPE_STRUCTURAL; |
|
28
|
|
|
|
|
29
|
|
|
protected $schemaComponent = 'LiteralField'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $name |
|
33
|
|
|
* @param string|FormField $content |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($name, $content) { |
|
36
|
|
|
$this->setContent($content); |
|
37
|
|
|
|
|
38
|
|
|
parent::__construct($name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $properties |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function FieldHolder($properties = array()) { |
|
47
|
|
|
if($this->content instanceof ViewableData) { |
|
48
|
|
|
$context = $this->content; |
|
49
|
|
|
|
|
50
|
|
|
if($properties) { |
|
|
|
|
|
|
51
|
|
|
$context = $context->customise($properties); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $context->forTemplate(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->content; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $properties |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function Field($properties = array()) { |
|
66
|
|
|
return $this->FieldHolder($properties); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets the content of this field to a new value. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string|FormField $content |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setContent($content) { |
|
77
|
|
|
$this->content = $content; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getContent() { |
|
86
|
|
|
return $this->content; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Synonym of {@link setContent()} so that LiteralField is more compatible with other field types. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string|FormField $content |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setValue($content) { |
|
97
|
|
|
$this->setContent($content); |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return static |
|
104
|
|
|
*/ |
|
105
|
|
|
public function performReadonlyTransformation() { |
|
106
|
|
|
$clone = clone $this; |
|
107
|
|
|
|
|
108
|
|
|
$clone->setReadonly(true); |
|
109
|
|
|
|
|
110
|
|
|
return $clone; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Header fields support dynamic titles via schema state |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getSchemaStateDefaults() { |
|
119
|
|
|
$state = parent::getSchemaStateDefaults(); |
|
120
|
|
|
|
|
121
|
|
|
$state['data']['content'] = $this->FieldHolder(); |
|
122
|
|
|
|
|
123
|
|
|
return $state; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.