silverstripe-framework/Forms/GridField/GridFieldConfig.php
Damian Mooyman 8dd644d25d
API Namespace all classes
Namespace all templates
Move difflib and BBCodeParser2 to thirdparty
Remove deprecated API marked for removal in 4.0
2016-09-08 10:23:17 +12:00

137 lines
3.4 KiB
PHP

<?php
namespace SilverStripe\Forms\GridField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Core\Object;
/**
* Encapsulates a collection of components following the
* {@link GridFieldComponent} interface. While the {@link GridField} itself
* has some configuration in the form of setters, most of the details are
* dealt with through components.
*
* For example, you would add a {@link GridFieldPaginator} component to enable
* pagination on the listed records, and configure it through
* {@link GridFieldPaginator->setItemsPerPage()}.
*
* In order to reduce the amount of custom code required, the framework
* provides some default configurations for common use cases:
*
* - {@link GridFieldConfig_Base} (added by default to GridField)
* - {@link GridFieldConfig_RecordEditor}
* - {@link GridFieldConfig_RelationEditor}
*/
class GridFieldConfig extends Object {
/**
* @var ArrayList
*/
protected $components = null;
/**
*
*/
public function __construct() {
parent::__construct();
$this->components = new ArrayList();
}
/**
* @param GridFieldComponent $component
* @param string $insertBefore The class of the component to insert this one before
* @return $this
*/
public function addComponent(GridFieldComponent $component, $insertBefore = null) {
if($insertBefore) {
$existingItems = $this->getComponents();
$this->components = new ArrayList;
$inserted = false;
foreach($existingItems as $existingItem) {
if(!$inserted && $existingItem instanceof $insertBefore) {
$this->components->push($component);
$inserted = true;
}
$this->components->push($existingItem);
}
if(!$inserted) $this->components->push($component);
} else {
$this->getComponents()->push($component);
}
return $this;
}
/**
* @param GridFieldComponent $component,... One or more components
* @return $this
*/
public function addComponents($component = null) {
$components = func_get_args();
foreach($components as $component) {
$this->addComponent($component);
}
return $this;
}
/**
* @param GridFieldComponent $component
* @return $this
*/
public function removeComponent(GridFieldComponent $component) {
$this->getComponents()->remove($component);
return $this;
}
/**
* @param string $type Class name or interface
* @return $this
*/
public function removeComponentsByType($type) {
$components = $this->getComponentsByType($type);
foreach($components as $component) {
$this->removeComponent($component);
}
return $this;
}
/**
* @return ArrayList Of GridFieldComponent
*/
public function getComponents() {
if(!$this->components) {
$this->components = new ArrayList();
}
return $this->components;
}
/**
* Returns all components extending a certain class, or implementing a certain interface.
*
* @param string $type Class name or interface
* @return ArrayList Of GridFieldComponent
*/
public function getComponentsByType($type) {
$components = new ArrayList();
foreach($this->components as $component) {
if($component instanceof $type) $components->push($component);
}
return $components;
}
/**
* Returns the first available component with the given class or interface.
*
* @param string $type ClassName
* @return GridFieldComponent
*/
public function getComponentByType($type) {
foreach($this->components as $component) {
if($component instanceof $type) {
return $component;
}
}
return null;
}
}