NEW Update FieldList::replaceField API to match removeByName (#8876)

* API Update FieldList::replaceField API to match removeByName

This specifically adds the parameter that `removeByName` has but `replaceField` does not. This parameter is set to `true` by default rather than inheriting the same default as `removeByField`. This is because the existing funtionality of `replaceField` was the same as if this parameter was set to `true`. This should be updated in SS5 to match the `removeByField` API.

* Add dataFieldOnly to CompositeField
This commit is contained in:
Guy Marriott 2019-04-15 16:22:54 +12:00 committed by Aaron Carlino
parent 66c372ce28
commit b1339f0d72
2 changed files with 14 additions and 4 deletions

View File

@ -379,9 +379,17 @@ class CompositeField extends FormField
$this->children->removeByName($fieldName, $dataFieldOnly); $this->children->removeByName($fieldName, $dataFieldOnly);
} }
public function replaceField($fieldName, $newField) /**
* @param $fieldName
* @param $newField
* @param boolean $dataFieldOnly If this is true, then a field will only
* be replaced if it's a data field. Dataless fields, such as tabs, will
* not be considered for replacement.
* @return bool
*/
public function replaceField($fieldName, $newField, $dataFieldOnly = true)
{ {
return $this->children->replaceField($fieldName, $newField); return $this->children->replaceField($fieldName, $newField, $dataFieldOnly);
} }
public function rootFieldList() public function rootFieldList()

View File

@ -379,14 +379,16 @@ class FieldList extends ArrayList
* *
* @param string $fieldName The name of the field to replace * @param string $fieldName The name of the field to replace
* @param FormField $newField The field object to replace with * @param FormField $newField The field object to replace with
* @param boolean $dataFieldOnly If this is true, then a field will only be replaced if it's a data field. Dataless
* fields, such as tabs, will be not be considered for replacement.
* @return boolean TRUE field was successfully replaced * @return boolean TRUE field was successfully replaced
* FALSE field wasn't found, nothing changed * FALSE field wasn't found, nothing changed
*/ */
public function replaceField($fieldName, $newField) public function replaceField($fieldName, $newField, $dataFieldOnly = true)
{ {
$this->flushFieldsCache(); $this->flushFieldsCache();
foreach ($this as $i => $field) { foreach ($this as $i => $field) {
if ($field->getName() == $fieldName && $field->hasData()) { if ($field->getName() == $fieldName && (!$dataFieldOnly || $field->hasData())) {
$this->items[$i] = $newField; $this->items[$i] = $newField;
return true; return true;
} elseif ($field instanceof CompositeField) { } elseif ($field instanceof CompositeField) {