silverstripe-framework/Forms/ToggleCompositeField.php

114 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2015-04-27 05:08:56 +02:00
namespace SilverStripe\Forms;
use SilverStripe\View\Requirements;
/**
* Allows visibility of a group of fields to be toggled.
*
* Caution: The form field does not include any JavaScript or CSS when used outside of the CMS context,
* since the required frontend dependencies are included through CMS bundling.
*/
class ToggleCompositeField extends CompositeField {
/**
* @var bool
*/
protected $startClosed = true;
/**
2015-04-27 05:08:56 +02:00
* @var int
*/
protected $headingLevel = 3;
2015-04-27 05:08:56 +02:00
/**
* @inheritdoc
*
2015-04-27 05:08:56 +02:00
* @param string $name
* @param string $title
* @param array|FieldList $children
*/
public function __construct($name, $title, $children) {
parent::__construct($children);
$this->setName($name);
$this->setTitle($title);
}
2015-04-27 05:08:56 +02:00
/**
* @inheritdoc
*
2015-04-27 05:08:56 +02:00
* @param array $properties
* @return string
2015-04-27 05:08:56 +02:00
*/
public function FieldHolder($properties = array()) {
2015-04-27 05:08:56 +02:00
$context = $this;
if(count($properties)) {
$context = $this->customise($properties);
}
return $context->renderWith($this->getTemplates());
}
2015-04-27 05:08:56 +02:00
/**
* @inheritdoc
*
* @return array
2015-04-27 05:08:56 +02:00
*/
public function getAttributes() {
2015-04-27 05:08:56 +02:00
$attributes = array(
'id' => $this->ID(),
2015-04-27 05:08:56 +02:00
'class' => $this->extraClass(),
);
if($this->getStartClosed()) {
2015-04-27 05:08:56 +02:00
$attributes['class'] .= ' ss-toggle ss-toggle-start-closed';
} else {
2015-04-27 05:08:56 +02:00
$attributes['class'] .= ' ss-toggle';
}
return array_merge(
$this->attributes,
2015-04-27 05:08:56 +02:00
$attributes
);
}
/**
* @return bool
*/
public function getStartClosed() {
return $this->startClosed;
}
/**
2015-04-27 05:08:56 +02:00
* Controls whether the field is open or closed by default. By default the field is closed.
*
* @param bool $startClosed
*
2015-04-27 05:08:56 +02:00
* @return $this
*/
2015-04-27 05:08:56 +02:00
public function setStartClosed($startClosed) {
$this->startClosed = (bool) $startClosed;
return $this;
}
/**
* @return int
*/
public function getHeadingLevel() {
return $this->headingLevel;
}
/**
2015-04-27 05:08:56 +02:00
* @param int $headingLevel
*
* @return $this
*/
2015-04-27 05:08:56 +02:00
public function setHeadingLevel($headingLevel) {
$this->headingLevel = (int) $headingLevel;
2015-04-27 05:08:56 +02:00
return $this;
}
}