mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Support for <legend> tags in CompositeField
This commit is contained in:
parent
b417daf7af
commit
9190bc4893
@ -33,6 +33,13 @@ class CompositeField extends FormField {
|
||||
*/
|
||||
protected $tag = 'div';
|
||||
|
||||
/**
|
||||
* @var String Optional description for this set of fields.
|
||||
* If the {@link $tag} property is set to use a 'fieldset', this will be
|
||||
* rendered as a <legend> tag, otherwise its a 'title' attribute.
|
||||
*/
|
||||
protected $legend;
|
||||
|
||||
public function __construct($children = null) {
|
||||
if($children instanceof FieldList) {
|
||||
$this->children = $children;
|
||||
@ -70,10 +77,6 @@ class CompositeField extends FormField {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function Field() {
|
||||
return $this->FieldHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Accessor method for $this->children
|
||||
* @return FieldList
|
||||
@ -101,6 +104,17 @@ class CompositeField extends FormField {
|
||||
return $this->tag;
|
||||
}
|
||||
|
||||
/** @param String */
|
||||
public function setLegend($legend) {
|
||||
$this->legend = $legend;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @return String */
|
||||
public function getLegend() {
|
||||
return $this->legend;
|
||||
}
|
||||
|
||||
function extraClasses() {
|
||||
$classes = array('field', 'CompositeField', parent::extraClasses());
|
||||
if($this->columnCount) $classes[] = 'multicolumn';
|
||||
@ -110,15 +124,47 @@ class CompositeField extends FormField {
|
||||
function getAttributes() {
|
||||
return array_merge(
|
||||
parent::getAttributes(),
|
||||
array('tabindex' => null, 'type' => null, 'value' => null, 'type' => null)
|
||||
array(
|
||||
'tabindex' => null,
|
||||
'type' => null,
|
||||
'value' => null,
|
||||
'type' => null,
|
||||
'title' => ($this->tag == 'fieldset') ? null : $this->legend
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function Field() {
|
||||
$content = '';
|
||||
|
||||
if($this->tag == 'fieldset' && $this->legend) {
|
||||
$content .= '<legend>' . $this->legend . '<legend>';
|
||||
}
|
||||
|
||||
$fs = $this->FieldList();
|
||||
foreach($fs as $subfield) {
|
||||
if($this->columnCount) {
|
||||
$className = "column{$this->columnCount}";
|
||||
if(!next($fs)) $className .= " lastcolumn";
|
||||
$content .= "\n<div class=\"{$className}\">\n" . $subfield->Field() . "\n</div>\n";
|
||||
} else if($subfield){
|
||||
$content .= "\n" . $subfield->Field() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $this->createTag($this->getTag(), $this->getAttributes(), $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fields nested inside another DIV
|
||||
*/
|
||||
function FieldHolder() {
|
||||
$content = '';
|
||||
|
||||
if($this->tag == 'fieldset' && $this->legend) {
|
||||
$content .= '<legend>' . $this->legend . '<legend>';
|
||||
}
|
||||
|
||||
$fs = $this->FieldList();
|
||||
foreach($fs as $subfield) {
|
||||
if($this->columnCount) {
|
||||
@ -136,14 +182,18 @@ class CompositeField extends FormField {
|
||||
/**
|
||||
* Returns the fields in the restricted field holder inside a DIV.
|
||||
*/
|
||||
function SmallFieldHolder() {//return $this->FieldHolder();
|
||||
function SmallFieldHolder() {
|
||||
$fs = $this->FieldList();
|
||||
$tag = $this->getTag();
|
||||
$idAtt = isset($this->id) ? " id=\"{$this->id}\"" : '';
|
||||
$className = ($this->columnCount) ? "field CompositeField {$this->extraClass()} multicolumn" : "field CompositeField {$this->extraClass()}";
|
||||
$content = "<$tag class=\"$className\"$idAtt>";
|
||||
|
||||
if($this->tag == 'fieldset' && $this->legend) {
|
||||
$content .= '<legend>' . $this->legend . '<legend>';
|
||||
}
|
||||
|
||||
foreach($fs as $subfield) {//echo ' subf'.$subfield->getName();
|
||||
foreach($fs as $subfield) {
|
||||
if($this->columnCount) {
|
||||
$className = "column{$this->columnCount}";
|
||||
if(!next($fs)) $className .= " lastcolumn";
|
||||
|
@ -41,4 +41,25 @@ class CompositeFieldTest extends SapphireTest {
|
||||
$this->assertStringStartsWith('<fieldset', trim($composite->FieldHolder()));
|
||||
$this->assertStringEndsWith('/fieldset>', trim($composite->FieldHolder()));
|
||||
}
|
||||
|
||||
function testLegend() {
|
||||
$composite = new CompositeField(
|
||||
new TextField('A'),
|
||||
new TextField('B')
|
||||
);
|
||||
$composite->setLegend('My legend');
|
||||
$parser = new CSSContentParser($composite->Field());
|
||||
$root = $parser->getBySelector('div.composite');
|
||||
$this->assertObjectHasAttribute('title', $root[0]->attributes());
|
||||
$this->assertEquals('My legend', (string)$root[0]['title']);
|
||||
|
||||
$composite->setTag('fieldset');
|
||||
$composite->setLegend('My legend');
|
||||
$parser = new CSSContentParser($composite->Field());
|
||||
$root = $parser->getBySelector('fieldset.composite');
|
||||
$this->assertObjectNotHasAttribute('title', $root[0]->attributes());
|
||||
$legend = $parser->getBySelector('fieldset.composite legend');
|
||||
$this->assertNotNull($legend);
|
||||
$this->assertEquals('My legend', (string)$legend[0]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user