2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-04-27 06:00:53 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* This field lets you put an arbitrary piece of HTML into your forms.
|
2015-04-27 06:00:53 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new LiteralField (
|
|
|
|
* $name = "literalfield",
|
|
|
|
* $content = '<b>some bold text</b> and <a href="http://silverstripe.com">a link</a>'
|
|
|
|
* )
|
|
|
|
* </code>
|
2015-04-27 06:00:53 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-dataless
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class LiteralField extends DatalessField {
|
2016-06-03 10:51:02 +02:00
|
|
|
|
|
|
|
private static $casting = [
|
|
|
|
'Value' => 'HTMLFragment',
|
|
|
|
];
|
|
|
|
|
2008-10-16 15:26:25 +02:00
|
|
|
/**
|
2015-04-27 06:00:53 +02:00
|
|
|
* @var string|FormField
|
2008-10-16 15:26:25 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $content;
|
2015-04-27 06:00:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string|FormField $content
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $content) {
|
2015-04-27 06:00:53 +02:00
|
|
|
$this->setContent($content);
|
|
|
|
|
2008-10-16 15:26:25 +02:00
|
|
|
parent::__construct($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-04-27 06:00:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $properties
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2015-04-27 06:00:53 +02:00
|
|
|
if($this->content instanceof ViewableData) {
|
|
|
|
$context = $this->content;
|
|
|
|
|
|
|
|
if($properties) {
|
|
|
|
$context = $context->customise($properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context->forTemplate();
|
2012-04-11 08:07:55 +02:00
|
|
|
}
|
2015-04-27 06:00:53 +02:00
|
|
|
|
|
|
|
return $this->content;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2015-04-27 06:00:53 +02:00
|
|
|
/**
|
|
|
|
* @param array $properties
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-04-11 08:07:55 +02:00
|
|
|
return $this->FieldHolder($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2008-10-16 15:26:25 +02:00
|
|
|
/**
|
2015-04-27 06:00:53 +02:00
|
|
|
* Sets the content of this field to a new value.
|
|
|
|
*
|
|
|
|
* @param string|FormField $content
|
2011-08-30 02:41:50 +02:00
|
|
|
*
|
2015-04-27 06:00:53 +02:00
|
|
|
* @return $this
|
2008-10-16 15:26:25 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setContent($content) {
|
2008-10-16 15:26:25 +02:00
|
|
|
$this->content = $content;
|
2015-04-27 06:00:53 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-10-16 15:26:25 +02:00
|
|
|
}
|
2015-04-27 06:00:53 +02:00
|
|
|
|
2008-10-16 15:26:25 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getContent() {
|
2008-10-16 15:26:25 +02:00
|
|
|
return $this->content;
|
|
|
|
}
|
2015-04-27 06:00:53 +02:00
|
|
|
|
2009-05-31 13:01:30 +02:00
|
|
|
/**
|
|
|
|
* Synonym of {@link setContent()} so that LiteralField is more compatible with other field types.
|
2015-04-27 06:00:53 +02:00
|
|
|
*
|
|
|
|
* @param string|FormField $content
|
|
|
|
*
|
|
|
|
* @return $this
|
2009-05-31 13:01:30 +02:00
|
|
|
*/
|
2015-04-27 06:00:53 +02:00
|
|
|
public function setValue($content) {
|
|
|
|
$this->setContent($content);
|
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-05-31 13:01:30 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2015-04-27 06:00:53 +02:00
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
2015-04-27 06:00:53 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone->setReadonly(true);
|
2015-04-27 06:00:53 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|