Skip to content

Commit

Permalink
🐛 Fix parent context inside of Repeaters and Groups (Fixes #219) (#220)
Browse files Browse the repository at this point in the history
* 🎨 Return an empty array if `$fields` is empty
  • Loading branch information
Log1x authored Feb 29, 2024
1 parent 9d14f55 commit 07780a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Builder/Concerns/HasParentContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Log1x\AcfComposer\Builder\Concerns;

trait HasParentContext
{
public function __call($method, $args)
{
if (
preg_match('/^add.+/', $method) &&
(method_exists($this->fieldsBuilder, $method) || $this->fieldsBuilder->getFieldType($method))
) {
$field = $this->handleCall($method, $args);
$field->setParentContext($this);

return $field;
}

return parent::__call($method, $args);
}

/**
* Hanlde the Fields Builder method call.
*
* @param string $method
* @param array $args
* @return mixed
*/
protected function handleCall($method, $args)
{
return call_user_func_array([$this->fieldsBuilder, $method], $args);
}
}
3 changes: 3 additions & 0 deletions src/Builder/GroupBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Log1x\AcfComposer\Builder;

use Log1x\AcfComposer\Builder;
use Log1x\AcfComposer\Builder\Concerns\HasParentContext;
use StoutLogic\AcfBuilder\GroupBuilder as GroupBuilderBase;

/**
* @method FieldBuilder addPartial(string $partial)
*/
class GroupBuilder extends GroupBuilderBase
{
use HasParentContext;

/**
* The fields builder instance.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Builder/RepeaterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Log1x\AcfComposer\Builder;

use Log1x\AcfComposer\Builder;
use Log1x\AcfComposer\Builder\Concerns\HasParentContext;
use StoutLogic\AcfBuilder\RepeaterBuilder as GroupBuilder;

/**
* @method FieldBuilder addPartial(string $partial)
*/
class RepeaterBuilder extends GroupBuilder
{
use HasParentContext;

/**
* The fields builder instance.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function getFields(bool $cache = true): array

$fields = $this->fields();

if (empty($fields)) {
return [];
}

$fields = is_a($fields, FieldsBuilder::class)
? [$fields]
: $fields;
Expand Down

0 comments on commit 07780a4

Please sign in to comment.