silverstripe-framework/forms/FieldGroup.php

174 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
* Lets you include a nested group of fields inside a template.
* This control gives you more flexibility over form layout.
2014-08-15 08:53:05 +02:00
*
* Note: the child fields within a field group aren't rendered using FieldHolder(). Instead,
* SmallFieldHolder() is called, which just prefixes $Field with a <label> tag, if the Title is set.
2014-08-15 08:53:05 +02:00
*
* <b>Usage</b>
2014-08-15 08:53:05 +02:00
*
* <code>
* FieldGroup::create(
* FieldGroup::create(
* HeaderField::create('FieldGroup 1'),
* TextField::create('Firstname')
* ),
* FieldGroup::create(
* HeaderField::create('FieldGroup 2'),
* TextField::create('Surname')
* )
* )
* </code>
2014-08-15 08:53:05 +02:00
*
* <b>Adding to existing FieldGroup instances</b>
2014-08-15 08:53:05 +02:00
*
* <code>
* function getCMSFields() {
* $fields = parent::getCMSFields();
2014-08-15 08:53:05 +02:00
*
* $fields->addFieldToTab(
2014-08-15 08:53:05 +02:00
* 'Root.Main',
* FieldGroup::create(
* TimeField::create("StartTime","What's the start time?"),
* TimeField::create("EndTime","What's the end time?")
* ),
* 'Content'
2014-08-15 08:53:05 +02:00
* );
*
* return $fields;
2014-08-15 08:53:05 +02:00
*
* }
* </code>
*
* <b>Setting a title to a FieldGroup</b>
2014-08-15 08:53:05 +02:00
*
* <code>
* $fields->addFieldToTab("Root.Main",
* FieldGroup::create(
* TimeField::create('StartTime','What's the start time?'),
* TimeField::create('EndTime', 'What's the end time?')
* )->setTitle('Time')
* );
* </code>
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-structural
*/
class FieldGroup extends CompositeField {
2014-08-15 08:53:05 +02:00
protected $zebra;
2014-08-15 08:53:05 +02:00
/**
* Create a new field group.
*
* Accepts any number of arguments.
*
* @param mixed $titleOrField Either the field title, list of fields, or first field
* @param mixed ...$otherFields Subsequent fields or field list (if passing in title to $titleOrField)
*/
public function __construct($titleOrField = null, $otherFields = null) {
$title = null;
if(is_array($titleOrField) || $titleOrField instanceof FieldList) {
$fields = $titleOrField;
// This would be discarded otherwise
if($otherFields) {
throw new InvalidArgumentException(
'$otherFields is not accepted if passing in field list to $titleOrField'
);
}
2014-08-15 08:53:05 +02:00
} else if(is_array($otherFields) || $otherFields instanceof FieldList) {
$title = $titleOrField;
$fields = $otherFields;
2014-08-15 08:53:05 +02:00
} else {
$fields = func_get_args();
if(!is_object(reset($fields))) {
$title = array_shift($fields);
}
}
2014-08-15 08:53:05 +02:00
parent::__construct($fields);
if($title) {
$this->setTitle($title);
}
}
2014-08-15 08:53:05 +02:00
/**
* Returns the name (ID) for the element.
2014-08-15 08:53:05 +02:00
* In some cases the FieldGroup doesn't have a title, but we still want
* the ID / name to be set. This code, generates the ID from the nested children
*/
public function getName(){
if($this->name) {
return $this->name;
}
if(!$this->title) {
$fs = $this->FieldList();
$compositeTitle = '';
$count = 0;
foreach($fs as $subfield){
/** @var FormField $subfield */
$compositeTitle .= $subfield->getName();
if($subfield->getName()) $count++;
}
2016-06-23 01:37:22 +02:00
/** @skipUpgrade */
if($count == 1) $compositeTitle .= 'Group';
2012-02-27 22:14:02 +01:00
return preg_replace("/[^a-zA-Z0-9]+/", "", $compositeTitle);
}
2012-02-27 22:14:02 +01:00
return preg_replace("/[^a-zA-Z0-9]+/", "", $this->title);
}
2012-02-27 22:14:02 +01:00
/**
* Set an odd/even class
*
* @param string $zebra one of odd or even.
* @return $this
*/
public function setZebra($zebra) {
if($zebra == 'odd' || $zebra == 'even') $this->zebra = $zebra;
else user_error("setZebra passed '$zebra'. It should be passed 'odd' or 'even'", E_USER_WARNING);
return $this;
}
/**
* @return string
*/
public function getZebra() {
return $this->zebra;
}
2014-08-15 08:53:05 +02:00
/**
* @return string
*/
public function Message() {
$fs = array();
$this->collateDataFields($fs);
2014-08-15 08:53:05 +02:00
foreach($fs as $subfield) {
if($m = $subfield->Message()) $message[] = rtrim($m, ".");
}
2014-08-15 08:53:05 +02:00
return (isset($message)) ? implode(", ", $message) . "." : "";
2014-08-15 08:53:05 +02:00
}
/**
* @return string
*/
public function MessageType() {
$fs = array();
$this->collateDataFields($fs);
2014-08-15 08:53:05 +02:00
foreach($fs as $subfield) {
if($m = $subfield->MessageType()) $MessageType[] = $m;
}
2014-08-15 08:53:05 +02:00
return (isset($MessageType)) ? implode(". ", $MessageType) : "";
}
}