silverstripe-framework/forms/LiteralField.php

126 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2015-04-27 06:00:53 +02:00
/**
* This field lets you put an arbitrary piece of HTML into your forms.
2015-04-27 06:00:53 +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
*
* @package forms
* @subpackage fields-dataless
*/
class LiteralField extends DatalessField {
private static $casting = [
'Value' => 'HTMLFragment',
];
/**
2015-04-27 06:00:53 +02:00
* @var string|FormField
*/
protected $content;
2015-04-27 06:00:53 +02:00
protected $schemaDataType = self::SCHEMA_DATA_TYPE_STRUCTURAL;
protected $schemaComponent = 'LiteralField';
2015-04-27 06:00:53 +02:00
/**
* @param string $name
* @param string|FormField $content
*/
public function __construct($name, $content) {
2015-04-27 06:00:53 +02:00
$this->setContent($content);
parent::__construct($name);
}
2015-04-27 06:00:53 +02:00
/**
* @param array $properties
*
* @return string
*/
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();
}
2015-04-27 06:00:53 +02:00
return $this->content;
}
2015-04-27 06:00:53 +02:00
/**
* @param array $properties
*
* @return string
*/
public function Field($properties = array()) {
return $this->FieldHolder($properties);
}
/**
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
*/
public function setContent($content) {
$this->content = $content;
2015-04-27 06:00:53 +02:00
return $this;
}
2015-04-27 06:00:53 +02:00
/**
* @return string
*/
public function getContent() {
return $this->content;
}
2015-04-27 06:00:53 +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
*/
2015-04-27 06:00:53 +02:00
public function setValue($content) {
$this->setContent($content);
return $this;
}
2015-04-27 06:00:53 +02:00
/**
* @return static
*/
public function performReadonlyTransformation() {
$clone = clone $this;
2015-04-27 06:00:53 +02:00
$clone->setReadonly(true);
2015-04-27 06:00:53 +02:00
return $clone;
}
/**
* Header fields support dynamic titles via schema state
*
* @return array
*/
public function getSchemaStateDefaults() {
$state = parent::getSchemaStateDefaults();
$state['data']['content'] = $this->FieldHolder();
return $state;
}
}