/** * Transform the named field into a readonly feld. * * @param string|FormField * @return bool */ public function makeFieldReadonly($field) { $fieldName = $field instanceof FormField ? $field->getName() : $field; // Iterate on items, looking for the applicable field foreach ($this->children as $i => $item) { if ($item instanceof CompositeField) { if ($item->makeFieldReadonly($fieldName)) { return true; } } elseif ($item instanceof FormField && $item->getName() == $fieldName) { // Once it's found, use FormField::transform to turn the field into a readonly version of itself. $this->children->replaceField($fieldName, $item->transform(new ReadonlyTransformation())); // A true results indicates that the field was found return true; } } return false; }
/** * Test replacing a field with another one. */ public function testReplaceField() { $fields = new FieldList(); $tab = new Tab('Root'); $fields->push($tab); /* A field gets added to the set */ $fields->addFieldToTab('Root', new TextField('Country')); $this->assertSame($fields->dataFieldByName('Country'), $tab->fieldByName('Country')); $fields->replaceField('Country', new EmailField('Email')); $this->assertEquals(1, $tab->Fields()->Count()); $fields = new FieldList(); $fields->push(new TextField('Name', 'Your name')); $brack = new TextField('Name[Field]', 'Your name'); $fields->replaceField('Name', $brack); $this->assertEquals(1, $fields->Count()); $this->assertEquals('Name[Field]', $fields->first()->getName()); }