NEW: Implement unshift() in field list classes (closes #4834)

This commit is contained in:
Loz Calver 2015-12-14 16:18:57 +00:00
parent 9e2c429591
commit 9467ab9a7e
5 changed files with 81 additions and 4 deletions

View File

@ -213,6 +213,15 @@ class CompositeField extends FormField {
$this->children->push($field); $this->children->push($field);
} }
/**
* Add a new child field to the beginning of the set.
*
* @param FormField
*/
public function unshift(FormField $field) {
$this->children->unshift($field);
}
/** /**
* @uses FieldList->insertBefore() * @uses FieldList->insertBefore()
*/ */

View File

@ -412,16 +412,27 @@ class FieldList extends ArrayList {
} }
/** /**
* Push a single field into this FieldList instance. * Push a single field onto the end of this FieldList instance.
* *
* @param FormField $item The FormField to add * @param FormField $item The FormField to add
* @param string $key An option array key (field name)
*/ */
public function push($item, $key = null) { public function push($item) {
$this->onBeforeInsert($item); $this->onBeforeInsert($item);
$item->setContainerFieldList($this); $item->setContainerFieldList($this);
return parent::push($item, $key); return parent::push($item);
}
/**
* Push a single field onto the beginning of this FieldList instance.
*
* @param FormField $item The FormField to add
*/
public function unshift($item) {
$this->onBeforeInsert($item);
$item->setContainerFieldList($this);
return parent::unshift($item);
} }
/** /**

View File

@ -150,6 +150,14 @@ class TabSet extends CompositeField {
$field->setTabSet($this); $field->setTabSet($this);
} }
/**
* Add a new child field to the beginning of the set.
*/
public function unshift(FormField $field) {
parent::unshift($field);
$field->setTabSet($this);
}
/** /**
* Inserts a field before a particular field in a FieldList. * Inserts a field before a particular field in a FieldList.
* *

View File

@ -44,6 +44,24 @@ class CompositeFieldTest extends SapphireTest {
$this->assertStringEndsWith('/fieldset>', trim($fieldset->FieldHolder())); $this->assertStringEndsWith('/fieldset>', trim($fieldset->FieldHolder()));
} }
public function testPushAndUnshift() {
$composite = new CompositeField(
new TextField('Middle')
);
$composite->push(new TextField('End'));
/* There are now 2 fields in the set */
$this->assertEquals(2, $composite->getChildren()->Count());
/* The most recently added field is at the end of the set */
$this->assertEquals('End', $composite->getChildren()->Last()->getName());
$composite->unshift(new TextField('Beginning'));
/* There are now 3 fields in the set */
$this->assertEquals(3, $composite->getChildren()->Count());
/* The most recently added field is at the beginning of the set */
$this->assertEquals('Beginning', $composite->getChildren()->First()->getName());
}
public function testLegend() { public function testLegend() {
$composite = new CompositeField( $composite = new CompositeField(
new TextField('A'), new TextField('A'),

View File

@ -374,6 +374,37 @@ class FieldListTest extends SapphireTest {
$this->assertEquals(3, $fields->Count()); $this->assertEquals(3, $fields->Count());
} }
/**
* Test pushing a field to the beginning of a set.
*
* This tests {@link FieldList->unshift()}.
*/
public function testPushFieldToBeginningOfSet() {
$fields = new FieldList();
/* A field named Country is added to the set */
$fields->unshift(new TextField('Country'));
/* We only have 1 field in the set */
$this->assertEquals(1, $fields->Count());
/* Another field called Email is added to the set */
$fields->unshift(new EmailField('Email'));
/* There are now 2 fields in the set */
$this->assertEquals(2, $fields->Count());
/* The most recently added field is at the beginning of the set */
$this->assertEquals('Email', $fields->First()->getName());
// Test that pushing a composite field without a name onto the set works
$fields->unshift(new CompositeField(
new TextField('Test1'),
new TextField('Test2')
));
$this->assertEquals(3, $fields->Count());
}
/** /**
* Test inserting a field before another in a set. * Test inserting a field before another in a set.
* *