silverstripe-framework/Forms/ToggleCompositeField.php
Ingo Schommer 8f23fa99a5 API Moved CMS-specific JavaScript to admin/thirdparty
The 'admin' module will be split off from 'framework',
where 'framework' only provides (mostly) frontend-agnostic PHP classes.
For example, HTMLEditorField.php has a TinyMCEConfig.php driver,
but doesn't come with its own JS includes.
2016-09-16 13:46:10 +12:00

114 lines
2.0 KiB
PHP

<?php
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;
/**
* @var int
*/
protected $headingLevel = 3;
/**
* @inheritdoc
*
* @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);
}
/**
* @inheritdoc
*
* @param array $properties
* @return string
*/
public function FieldHolder($properties = array()) {
$context = $this;
if(count($properties)) {
$context = $this->customise($properties);
}
return $context->renderWith($this->getTemplates());
}
/**
* @inheritdoc
*
* @return array
*/
public function getAttributes() {
$attributes = array(
'id' => $this->ID(),
'class' => $this->extraClass(),
);
if($this->getStartClosed()) {
$attributes['class'] .= ' ss-toggle ss-toggle-start-closed';
} else {
$attributes['class'] .= ' ss-toggle';
}
return array_merge(
$this->attributes,
$attributes
);
}
/**
* @return bool
*/
public function getStartClosed() {
return $this->startClosed;
}
/**
* Controls whether the field is open or closed by default. By default the field is closed.
*
* @param bool $startClosed
*
* @return $this
*/
public function setStartClosed($startClosed) {
$this->startClosed = (bool) $startClosed;
return $this;
}
/**
* @return int
*/
public function getHeadingLevel() {
return $this->headingLevel;
}
/**
* @param int $headingLevel
*
* @return $this
*/
public function setHeadingLevel($headingLevel) {
$this->headingLevel = (int) $headingLevel;
return $this;
}
}