Skip to content

Commit 49cb8d2

Browse files
author
Miika Arponen
committed
Better support for all Gutenberg block parameters
1 parent 8a8c30b commit 49cb8d2

File tree

3 files changed

+95
-18
lines changed

3 files changed

+95
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414
* `Geniem\ACF\Factory\Block`: A factory for creating ACF Gutenberg blocks with Codifier components.
1515
+ Component rendering functionality implementing the `Geniem\ACF\Interfaces\Renderer` interface.
1616
* `Geniem\ACF\Renderer\PHP`: Uses PHP files as templates.
17+
* `Geniem\ACF\Renderer\Dust`: Uses Dust.js template files.
1718

1819
## [1.16.0] - 2019-04-17
1920

src/Component.php

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,26 @@ abstract class Component implements ComponentInterface {
3939
protected $title = '';
4040

4141
/**
42-
* Getter for the category.
42+
* The block category
4343
*
4444
* @var string
4545
*/
4646
protected $category = '';
4747

4848
/**
49-
* Getter for the icon.
49+
* The block icon. Can hold both the string or array representation.
5050
*
51-
* @var string
51+
* @var string|array
5252
*/
5353
protected $icon = '';
5454

5555
/**
56-
* Getter for the keywords.
56+
* The block keywords.
5757
*
5858
* @var array
5959
*/
6060
protected $keywords = [];
6161

62-
6362
/**
6463
* Component description, shows to the user
6564
*
@@ -74,6 +73,40 @@ abstract class Component implements ComponentInterface {
7473
*/
7574
protected $fields = [];
7675

76+
/**
77+
* Array of post types to restrict this block type to.
78+
*
79+
* @var array
80+
*/
81+
protected $post_types = [];
82+
83+
/**
84+
* Display mode for the block.
85+
*
86+
* Options: auto, preview or edit.
87+
*
88+
* @var string
89+
*/
90+
protected $mode = 'preview';
91+
92+
/**
93+
* The default block alignment.
94+
*
95+
* Options: left, center, right, wide or full.
96+
*
97+
* @var string
98+
*/
99+
protected $align = '';
100+
101+
/**
102+
* An array of features for the block to support.
103+
*
104+
* @see https://wordpress.org/gutenberg/handbook/block-api/
105+
*
106+
* @var array
107+
*/
108+
protected $supports = [];
109+
77110
/**
78111
* The renderer for this component.
79112
*
@@ -156,9 +189,9 @@ public function get_category() : string {
156189
/**
157190
* Getter for the icon.
158191
*
159-
* @return string
192+
* @return string|array
160193
*/
161-
public function get_icon() : string {
194+
public function get_icon() {
162195
return $this->icon;
163196
}
164197

@@ -170,4 +203,40 @@ public function get_icon() : string {
170203
public function get_keywords() : array {
171204
return $this->keywords;
172205
}
206+
207+
/**
208+
* Getter for the post types.
209+
*
210+
* @return array
211+
*/
212+
public function get_post_types() : array {
213+
return $this->post_types;
214+
}
215+
216+
/**
217+
* Getter for the display mode.
218+
*
219+
* @return string
220+
*/
221+
public function get_mode() : string {
222+
return $this->mode;
223+
}
224+
225+
/**
226+
* Getter for the default block alignment.
227+
*
228+
* @return string
229+
*/
230+
public function get_align() : string {
231+
return $this->align;
232+
}
233+
234+
/**
235+
* Getter for the supported features of the block.
236+
*
237+
* @return array
238+
*/
239+
public function get_supports() : array {
240+
return $this->supports;
241+
}
173242
}

src/Factory/Block.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,25 @@ protected function register_field_group() {
8181
* @return array
8282
*/
8383
protected function register_block() {
84+
$args = [
85+
'name' => $this->component->get_name(),
86+
'title' => $this->component->get_title(),
87+
'description' => $this->component->get_description(),
88+
'render_callback' => [ $this, 'render' ],
89+
'category' => $this->component->get_category(),
90+
'icon' => $this->component->get_icon(),
91+
'keywords' => $this->component->get_keywords(),
92+
'mode' => $this->component->get_mode(),
93+
'align' => $this->component->get_align(),
94+
'supports' => $this->component->get_supports(),
95+
];
96+
97+
if ( ! empty( $this->component->get_post_types() ) ) {
98+
$args['post_types'] = $this->component->get_post_types();
99+
}
100+
84101
// Register the ACF Block.
85-
$block = acf_register_block(
86-
[
87-
'name' => $this->component->get_name(),
88-
'title' => $this->component->get_title(),
89-
'description' => $this->component->get_description(),
90-
'render_callback' => [ $this, 'render' ],
91-
'category' => $this->component->get_category(),
92-
'icon' => $this->component->get_icon(),
93-
'keywords' => $this->component->get_keywords(),
94-
]
95-
);
102+
$block = acf_register_block( $args );
96103

97104
return $block;
98105
}

0 commit comments

Comments
 (0)