Skip to content

Commit

Permalink
✨ Add support for registering multiple field groups in a single Field…
Browse files Browse the repository at this point in the history
… Composer (Fixes #182) (#214)
  • Loading branch information
Log1x authored Feb 29, 2024
1 parent 5d2ee37 commit c3fda03
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,71 @@

namespace Log1x\AcfComposer;

use Exception;
use StoutLogic\AcfBuilder\FieldsBuilder;

abstract class Field extends Composer
{
/**
* Register the field groups with Advanced Custom Fields.
*/
protected function register(?callable $callback = null): void
{
if (empty($this->fields)) {
return;
}

if ($callback) {
$callback();
}

foreach ($this->fields as $fields) {
acf_add_local_field_group(
$this->build($fields)
);
}
}

/**
* Retrieve the field group fields.
*/
public function getFields(bool $cache = true): array
{
if ($this->fields && $cache) {
return $this->fields;
}

if ($cache && $this->composer->hasCache($this)) {
return $this->composer->getCache($this);
}

$fields = $this->fields();

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

if (! is_array($fields)) {
throw new Exception('Fields must be an array or an instance of Builder.');
}

$fields = ! empty($fields['fields']) ? [$fields] : $fields;

foreach ($fields as $key => $field) {
$fields[$key] = is_a($field, FieldsBuilder::class)
? $field->build()
: $field;
}

if ($this->defaults->has('field_group')) {
foreach ($fields as $key => $field) {
$fields[$key] = array_merge($this->defaults->get('field_group'), $field);
}
}

return $fields;
}

/**
* Compose and register the defined field groups with ACF.
*
Expand Down

0 comments on commit c3fda03

Please sign in to comment.