API CHANGE Changed constructor parameters for LabelField and Header: First parameter is now $name, not $title. Necessary change to consistently support labelled fields which can be identified and modified within a FieldSet, and in the DOM structure. Both HeaderField and LabelField still acceppt old notation through checking the parameter signature with func_get_args().

API CHANGE Moved setAllowHTML() to DataLessField
ENHANCEMENT Using createTag() to create HeaderField and LabelField, which adds support for HTML id attributes and extra css classes through addExtraClass()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64421 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-10-16 13:26:25 +00:00
parent ab891c67ec
commit e95c5657ff
4 changed files with 114 additions and 35 deletions

View File

@ -2,10 +2,31 @@
/** /**
* Abstract class for all fields without data. * Abstract class for all fields without data.
* Labels, headings and the like should extend from this. * Labels, headings and the like should extend from this.
*
* @package forms * @package forms
* @subpackage fields-dataless * @subpackage fields-dataless
*/ */
class DatalessField extends FormField { class DatalessField extends FormField {
/**
* @var bool $allowHTML
*/
protected $allowHTML;
/**
* @param string $name
* @param string $title The label itslef
* @param string $class An HTML class to apply to the label (Deprecated: use addExtraClass())
* @param boolean $allowHTML Determine if the tag content needs to be escaped (Deprecated: use setAllowHTML())
* @param Form $form
*/
function __construct($name, $title = null, $className = "", $allowHTML = false, $form = null) {
if($className) $this->extraClasses = array($className);
$this->allowHTML = $allowHTML;
parent::__construct($name, $title, null, $form);
}
/** /**
* Function that returns whether this field contains data. * Function that returns whether this field contains data.
* Always returns false. * Always returns false.
@ -36,11 +57,17 @@ class DatalessField extends FormField {
} }
/** /**
* As DatalessFields are not initialized with an ID/name, * @param bool $bool
* we keep the title for a unique reference.
*/ */
function Name() { function setAllowHTML($bool) {
return $this->title; $this->allowHTML = $bool;
}
/**
* @return bool
*/
function getAllowHTML() {
return $this->allowHTML;
} }
} }
?> ?>

View File

@ -6,21 +6,41 @@
* @subpackage fields-dataless * @subpackage fields-dataless
*/ */
class HeaderField extends DatalessField { class HeaderField extends DatalessField {
protected $headingLevel, $allowHTML;
function __construct($title, $headingLevel = 2, $allowHTML = false, $form = null) { /**
* @var int $headingLevel The level of the <h1> to <h6> HTML tag. Default: 2
*/
protected $headingLevel = 2;
function __construct($name, $title, $headingLevel = 2, $allowHTML = false, $form = null) {
// legacy handling for old parameters: $title, $heading, ...
// instead of new handling: $name, $title, $heading, ...
$args = func_get_args();
if(!isset($args[1]) || is_numeric($args[1])) {
$title = (isset($args[0])) ? $args[0] : null;
$name = $title; // this means i18nized fields won't be easily accessible through fieldByName()
$headingLevel = (isset($args[1])) ? $args[1] : null;
$allowHTML = (isset($args[2])) ? $args[2] : null;
$form = (isset($args[3])) ? $args[3] : null;
}
$this->headingLevel = $headingLevel; $this->headingLevel = $headingLevel;
$this->allowHTML = $allowHTML; $this->allowHTML = $allowHTML;
parent::__construct(null, $title, null, $form); parent::__construct($name, $title, null, $form);
} }
function Field() { function Field() {
$XML_title = ($this->allowHTML) ? $this->title : Convert::raw2xml($this->title); $attributes = array(
'class' => $this->extraClass(),
// extraclass 'id' => $this->id(),
$XML_class = ($this->extraClass()) ? " class=\"{$this->extraClass()}\"" : ''; 'name' => $this->Name(),
);
return "<h{$this->headingLevel}{$XML_class}>$XML_title</h$this->headingLevel>"; return $this->createTag(
"h{$this->headingLevel}",
$attributes,
($this->getAllowHTML() ? $this->title : Convert::raw2xml($this->title))
);
} }
} }
?> ?>

View File

@ -6,27 +6,46 @@
* @subpackage fields-dataless * @subpackage fields-dataless
*/ */
class LabelField extends DatalessField { class LabelField extends DatalessField {
protected $className;
protected $allowHTML;
/** /**
* Create a new label. * @param string $name
* @param title The label itslef * @param string $title
* @param class An HTML class to apply to the label. * @param string $className (Deprecated: use addExtraClass())
* @param bool $allowHTML (Deprecated: use setAllowHTML())
* @param Form $form
*/ */
function __construct($title, $className = "", $allowHTML = false, $form = null) { function __construct($name, $title, $className = null, $allowHTML = false, $form = null) {
$this->className = $className; // legacy handling for old parameters: $title, $heading, ...
// instead of new handling: $name, $title, $heading, ...
$args = func_get_args();
if(!isset($args[1])) {
$title = (isset($args[0])) ? $args[0] : null;
$name = $title;
$classname = (isset($args[1])) ? $args[1] : null;
$allowHTML = (isset($args[2])) ? $args[2] : null;
$form = (isset($args[3])) ? $args[3] : null;
}
$this->headingLevel = $headingLevel;
$this->allowHTML = $allowHTML; $this->allowHTML = $allowHTML;
parent::__construct(null, $title, null, $form); parent::__construct($name, $title, null, $form);
} }
/** /**
* Returns a label containing the title, and an HTML class if given. * Returns a label containing the title, and an HTML class if given.
*/ */
function Field() { function Field() {
$classClause = $this->className ? " class=\"$this->className\"" : ''; $attributes = array(
return "<label$classClause>" . ($this->allowHTML ? $this->title : htmlentities($this->title)) . "</label>"; 'class' => $this->extraClass(),
'id' => $this->id(),
'name' => $this->Name(),
);
return $this->createTag(
'label',
$attributes,
($this->getAllowHTML() ? $this->title : htmlentities($this->title))
);
} }
} }
?> ?>

View File

@ -1,32 +1,45 @@
<?php <?php
/** /**
* This field lets you put an arbitrary piece of HTML into your forms. * This field lets you put an arbitrary piece of HTML into your forms.
* If there's not much behaviour around the HTML, it might not be worth going to the effort of *
* making a special field type for it. So you can use LiteralField. If you pass it a viewabledata object,
* it will turn it into a string for you.
* @package forms * @package forms
* @subpackage fields-dataless * @subpackage fields-dataless
*/ */
class LiteralField extends DatalessField { class LiteralField extends DatalessField {
/**
* @var string $content
*/
protected $content; protected $content;
function __construct($name, $content) { function __construct($name, $content) {
parent::__construct($name);
$this->content = $content; $this->content = $content;
parent::__construct($name);
} }
function FieldHolder() { function FieldHolder() {
return is_object($this->content) ? $this->content->forTemplate() : $this->content; return is_object($this->content) ? $this->content->forTemplate() : $this->content;
} }
function Field() { function Field() {
return $this->FieldHolder(); return $this->FieldHolder();
} }
/** /**
* @desc Sets the content of this field to a new value * Sets the content of this field to a new value
*/ * @param string $content
function setContent($content) { */
$this->content = $content; function setContent($content) {
} $this->content = $content;
}
/**
* @return string
*/
function getContent() {
return $this->content;
}
function performReadonlyTransformation() { function performReadonlyTransformation() {
$this->setReadonly(true); $this->setReadonly(true);