Skip to content

Commit

Permalink
enhance(defaults): Move field_group defaults out of build()
Browse files Browse the repository at this point in the history
Improve `acf.php` config documentation
  • Loading branch information
Log1x committed Jan 4, 2020
1 parent 5579322 commit 1aa4960
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
9 changes: 7 additions & 2 deletions config/acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@
| Default Field Type Settings
|--------------------------------------------------------------------------
|
| Here you can set default field type settings that are automatically
| applied to the field types when built.
| Here you can set default field group and field type configuration that
| is then merged with your field groups when they are composed.
|
| This allows you to avoid the repetitive process of setting common field
| configuration such as `ui` on every `trueFalse` field or
| `instruction_placement` on every `fieldGroup`.
|
*/

'defaults' => [
'trueFalse' => ['ui' => 1],
'select' => ['ui' => 1],
],
];
8 changes: 4 additions & 4 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public function compose()
]);

if (! empty($this->fields)) {
if ($this->defaults->has('field_group')) {
$this->fields = array_merge($this->fields, $this->defaults->get('field_group'));
}

if (! Arr::has($this->fields, 'location.0.0')) {
Arr::set($this->fields, 'location.0.0', [
'param' => 'block',
Expand All @@ -243,10 +247,6 @@ public function compose()
*/
protected function build($fields = [])
{
if (empty($fields) && $this->defaults->has('field_group')) {
$this->fields = array_merge($this->fields, $this->defaults->get('field_group'));
}

return collect($fields ?: $this->fields)->map(function ($value, $key) use ($fields) {
if (
! Str::contains($key, ['fields', 'sub_fields', 'layouts']) ||
Expand Down
17 changes: 10 additions & 7 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,22 @@ public function compose()
}

$this->fields = $this->fields();

$this->defaults = collect(
$this->app->config->get('acf.defaults')
)->merge($this->defaults)->mapWithKeys(function ($value, $key) {
return [Str::snake($key) => $value];
});

add_action('init', function () {
acf_add_local_field_group($this->build());
}, 20);
if (! empty($this->fields)) {
add_action('init', function () {
if ($this->defaults->has('field_group')) {
$this->fields = array_merge($this->fields, $this->defaults->get('field_group'));
}

acf_add_local_field_group($this->build());
}, 20);
}
}

/**
Expand All @@ -73,10 +80,6 @@ public function compose()
*/
protected function build($fields = [])
{
if (empty($fields) && $this->defaults->has('field_group')) {
$this->fields = array_merge($this->fields, $this->defaults->get('field_group'));
}

return collect($fields ?: $this->fields)->map(function ($value, $key) use ($fields) {
if (
! Str::contains($key, ['fields', 'sub_fields', 'layouts']) ||
Expand Down

0 comments on commit 1aa4960

Please sign in to comment.