From b1339f0d724396a39c1b5b1b7ae18239f63c812c Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Mon, 15 Apr 2019 16:22:54 +1200 Subject: [PATCH] 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 --- src/Forms/CompositeField.php | 12 ++++++++++-- src/Forms/FieldList.php | 6 ++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Forms/CompositeField.php b/src/Forms/CompositeField.php index e02a3c9df..001f81e1d 100644 --- a/src/Forms/CompositeField.php +++ b/src/Forms/CompositeField.php @@ -379,9 +379,17 @@ class CompositeField extends FormField $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() diff --git a/src/Forms/FieldList.php b/src/Forms/FieldList.php index 8bbd04979..027f4d8bc 100644 --- a/src/Forms/FieldList.php +++ b/src/Forms/FieldList.php @@ -379,14 +379,16 @@ class FieldList extends ArrayList * * @param string $fieldName The name of the field to replace * @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 * FALSE field wasn't found, nothing changed */ - public function replaceField($fieldName, $newField) + public function replaceField($fieldName, $newField, $dataFieldOnly = true) { $this->flushFieldsCache(); foreach ($this as $i => $field) { - if ($field->getName() == $fieldName && $field->hasData()) { + if ($field->getName() == $fieldName && (!$dataFieldOnly || $field->hasData())) { $this->items[$i] = $newField; return true; } elseif ($field instanceof CompositeField) {