Skip to content

Commit 2a9673c

Browse files
authored
✨ Add support for block defaults (#327)
🧑‍💻 Improve block version handling
1 parent 9f8350b commit 2a9673c

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

config/acf.php

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@
3737
// 'phoneNumber' => 'phone_number',
3838
],
3939

40+
/*
41+
|--------------------------------------------------------------------------
42+
| Default Block Settings
43+
|--------------------------------------------------------------------------
44+
|
45+
| Here you may define default settings to merge with your block definition
46+
| during composition. Any settings defined on the block will take
47+
| precedence over these defaults.
48+
|
49+
*/
50+
51+
'blocks' => [
52+
'apiVersion' => 2,
53+
],
54+
4055
/*
4156
|--------------------------------------------------------------------------
4257
| Generators

src/Block.php

+24-17
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,11 @@ abstract class Block extends Composer implements BlockContract
271271
public $usePostMeta = false;
272272

273273
/**
274-
* The ACF block API version.
274+
* The block API version.
275275
*
276-
* @var int
276+
* @var int|null
277277
*/
278-
public $blockVersion = 2;
279-
280-
/**
281-
* The ACF block API version.
282-
*
283-
* @var int
284-
*/
285-
public $apiVersion = 2;
278+
public $apiVersion = null;
286279

287280
/**
288281
* Validate block fields as per the field group configuration.
@@ -304,12 +297,18 @@ public function attributes(): array
304297
*/
305298
public function mergeAttributes(): void
306299
{
307-
if (! $attributes = $this->attributes()) {
308-
return;
300+
foreach ($this->attributes() as $key => $value) {
301+
if (! property_exists($this, $key)) {
302+
continue;
303+
}
304+
305+
$this->{$key} = $value;
309306
}
310307

311-
foreach ($attributes as $key => $value) {
312-
if (! property_exists($this, $key)) {
308+
$defaults = config('acf.blocks', []);
309+
310+
foreach ($defaults as $key => $value) {
311+
if (! property_exists($this, $key) || filled($this->{$key})) {
313312
continue;
314313
}
315314

@@ -495,6 +494,14 @@ public function getClasses(): string
495494
);
496495
}
497496

497+
/**
498+
* Retrieve the block API version.
499+
*/
500+
public function getApiVersion(): int
501+
{
502+
return $this->apiVersion ?? 2;
503+
}
504+
498505
/**
499506
* Retrieve the block text domain.
500507
*/
@@ -638,8 +645,8 @@ public function settings(): Collection
638645
'styles' => $this->getStyles(),
639646
'supports' => $this->getSupports(),
640647
'textdomain' => $this->getTextDomain(),
641-
'acf_block_version' => $this->blockVersion,
642-
'api_version' => $this->apiVersion,
648+
'acf_block_version' => $this->getApiVersion(),
649+
'apiVersion' => $this->getApiVersion(),
643650
'validate' => $this->validate,
644651
'use_post_meta' => $this->usePostMeta,
645652
'render_callback' => function (
@@ -694,7 +701,7 @@ public function toJson(): string
694701
$settings = $this->settings()
695702
->put('name', $this->namespace)
696703
->put('acf', [
697-
'blockVersion' => $this->blockVersion,
704+
'blockVersion' => $this->getApiVersion(),
698705
'mode' => $this->mode,
699706
'postTypes' => $this->post_types,
700707
'renderTemplate' => $this::class,

0 commit comments

Comments
 (0)