diff --git a/src/FieldManager.php b/src/FieldManager.php index af1dcc7..a86798b 100644 --- a/src/FieldManager.php +++ b/src/FieldManager.php @@ -196,6 +196,7 @@ private function validateFieldName($field) public function getFieldIndex($name) { foreach ($this->getFields() as $index => $field) { +// echo $field->getName().' === '.$name; if ($field->getName() === $name) { return $index; } diff --git a/src/FieldsBuilder.php b/src/FieldsBuilder.php index c398fc1..6732d39 100644 --- a/src/FieldsBuilder.php +++ b/src/FieldsBuilder.php @@ -639,27 +639,23 @@ public function fieldExists($name) */ public function modifyField($name, $modify) { - if (is_array($modify)) { - if (strpos($name, '.') === false) { - $this->getFieldManager()->modifyField($name, $modify); - return $this; - } - $explodedName = explode('.', $name); - $field = $this->getField($explodedName[0]); - unset($explodedName[0]); - $nextField = $field; - $i = 0; - $length = count($explodedName); - foreach ($explodedName as $key => $name) { - $length = count($explodedName); - if ($i === $length - 1) { - $nextField->modifyField($name, $modify); - } else { - $nextField = $field->getField($name); - unset($explodedName[$key]); - } - $i++; + if (strpos($name, '->')) { + $fieldNames = explode('->', $name); + $fieldName = array_shift($fieldNames); + $field = $this->getField($fieldName); + + // Traverse down tree until last child field + while(($fieldName = array_shift($fieldNames)) && count($fieldNames) > 0) { + $field = $field->getField($fieldName); } + + $field->modifyField($fieldName, $modify); + return $this; + } + + if (is_array($modify)) { + $this->getFieldManager()->modifyField($name, $modify); + return $this; } elseif ($modify instanceof \Closure) { $field = $this->getField($name); diff --git a/src/GroupBuilder.php b/src/GroupBuilder.php index 146e10b..0dae2f9 100644 --- a/src/GroupBuilder.php +++ b/src/GroupBuilder.php @@ -124,4 +124,13 @@ public function modifyField($name, $modify) return $this; } + + public function getField($name) + { + return $this->fieldsBuilder->getField($name); + } + + public function fieldExists($name) { + return $this->fieldsBuilder->fieldExists($name); + } } \ No newline at end of file diff --git a/tests/GroupBuilderTest.php b/tests/GroupBuilderTest.php index fce23c4..32d1b93 100644 --- a/tests/GroupBuilderTest.php +++ b/tests/GroupBuilderTest.php @@ -96,12 +96,12 @@ public function testDeepModifyGroupWithArray() { $partial ->addRepeater('items') - ->addText('title') + ->addText('headline') ->endRepeater(); $subject->addFields($partial); - $subject->modifyField('items.title', [ + $subject->modifyField('items->headline', [ 'wrapper' => [ 'width' => '77%' ] @@ -120,9 +120,9 @@ public function testDeepModifyGroupWithArray() { 'sub_fields' => [ [ 'type' => 'text', - 'label' => 'Title', - 'name' => 'title', - 'key' => 'field_test_items_title', + 'label' => 'Headline', + 'name' => 'headline', + 'key' => 'field_test_items_headline', 'wrapper' => [ 'width' => '77%' ] @@ -134,6 +134,166 @@ public function testDeepModifyGroupWithArray() { ], $subject->build()); } + + + public function testDeepModifyThreeLevelsGroupWithArray() { + $subject = new FieldsBuilder('test'); + + $subject + ->addGroup('slides') + ->addRepeater('slide')->setWidth("25%") + ->addText('headline')->setWidth('100%') + ->addTextarea('content'); + + $subject->modifyField('slides->slide->headline', [ + 'wrapper' => [ + 'width' => '50%' + ] + ]); + + $this->assertArraySubset([ + 'key' => 'group_test', + 'title' => 'Test', + 'fields' => [ + [ + 'name' => 'slides', + 'type' => 'group', + 'sub_fields' => [ + [ + 'type' => 'repeater', + 'name' => 'slide', + 'wrapper' => [ + 'width' => '25%' + ], + 'sub_fields' => [ + [ + 'type' => 'text', + 'name' => 'headline', + 'wrapper' => [ + 'width' => '50%' + ] + ], + [ + 'type' => 'textarea', + 'name' => 'content', + ] + ] + ] + ] + ] + ] + ], $subject->build()); + } + + public function testDeepModifyGroupWithClosure() { + $subject = new FieldsBuilder('test'); + + $subject + ->addGroup('slides') + ->addRepeater('slide')->setWidth("25%") + ->addText('headline')->setWidth('100%') + ->addTextarea('content'); + + $subject->modifyField('slides->slide', function(FieldsBuilder $builder) { + $builder + ->getField('slide') + ->setWidth("50%"); + + $builder->addLink('cta'); + + return $builder; + }); + + $this->assertArraySubset([ + 'key' => 'group_test', + 'title' => 'Test', + 'fields' => [ + [ + 'name' => 'slides', + 'type' => 'group', + 'sub_fields' => [ + [ + 'type' => 'repeater', + 'name' => 'slide', + 'wrapper' => [ + 'width' => '50%' + ], + 'sub_fields' => [ + [ + 'type' => 'text', + 'name' => 'headline', + 'wrapper' => [ + 'width' => '100%' + ] + ], + [ + 'type' => 'textarea', + 'name' => 'content', + ], + ] + ], + [ + 'type' => 'link', + 'name' => 'cta', + ], + ] + ] + ] + ], $subject->build()); + } + + public function testDeepModifyThreeLevelsGroupWithClosure() { + $subject = new FieldsBuilder('test'); + + $subject + ->addGroup('slides') + ->addRepeater('slide')->setWidth("25%") + ->addText('headline')->setWidth('100%') + ->addTextarea('content'); + + $subject->modifyField('slides->slide->headline', function(FieldsBuilder $builder) { + $builder->addLink('cta'); + return $builder; + }); + + $this->assertArraySubset([ + 'key' => 'group_test', + 'title' => 'Test', + 'fields' => [ + [ + 'name' => 'slides', + 'type' => 'group', + 'sub_fields' => [ + [ + 'type' => 'repeater', + 'name' => 'slide', + 'wrapper' => [ + 'width' => '25%' + ], + 'sub_fields' => [ + [ + 'type' => 'text', + 'name' => 'headline', + 'wrapper' => [ + 'width' => '100%' + ] + ], + [ + 'type' => 'link', + 'name' => 'cta', + ], + [ + 'type' => 'textarea', + 'name' => 'content', + ], + ] + ] + ] + ] + ] + ], $subject->build()); + } + public function testRemovingGroup() { $subject = new GroupBuilder('test1');