Skip to content

Commit

Permalink
feat: Add getField and fieldExists to GroupBuilder in order to handle…
Browse files Browse the repository at this point in the history
… modifyField Nesting #142 #91 #141 #82
  • Loading branch information
stevep committed Sep 16, 2021
1 parent d9308c2 commit d7dce8b
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/FieldManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
36 changes: 16 additions & 20 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/GroupBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
170 changes: 165 additions & 5 deletions tests/GroupBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
]
Expand All @@ -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%'
]
Expand All @@ -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');
Expand Down

0 comments on commit d7dce8b

Please sign in to comment.