Skip to content

Commit

Permalink
Merge pull request #35 from palmiak/default_options
Browse files Browse the repository at this point in the history
Added default options
  • Loading branch information
palmiak authored Oct 6, 2020
2 parents 997d97c + 5953849 commit 3bf0dc0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ add_filter( 'timber/acf-gutenberg-blocks-preview-identifier', function( $sufix )
```

So the preview file would be called `slug-prev.twig`.

`timber/acf-gutenberg-blocks-default-data` - sets the default block parameters
Since version 1.13

```php
<?php
add_filter( 'timber/acf-gutenberg-blocks-default-data', function( $data ){
$data['default'] = array(
'post_type' => 'post',
);
$data['pages'] = array(
'post_type' => 'page',
);
return $data;
} );
```

With this filter added - by default each block will have **post** as a post type, but if we'll add:
`DefaultData: page` to the block - it will set post type to **page**.

Of course setting a `PostType` in a block will override the default-data settings.
1 change: 1 addition & 0 deletions docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
SupportsJSX: (true|false)
Example: (JSON format)
Parent: (space-separated)
DefaultData:
#}
```
36 changes: 36 additions & 0 deletions timber-acf-wp-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function () {
'example' => 'Example',
'supports_jsx' => 'SupportsJSX',
'parent' => 'Parent',
'default_data' => 'DefaultData',
)
);

Expand Down Expand Up @@ -98,7 +99,12 @@ function () {
'enqueue_assets' => $file_headers['enqueue_assets'],
'supports_custom_class_name' => 'SupportsCustomClassName',
'supports_reusable' => 'SupportsReusable',
'default_data' => $file_headers['default_data'],
);

// Removes empty defaults.
$data = array_filter( $data );

// If the PostTypes header is set in the template, restrict this block
// to those types.
if ( ! empty( $file_headers['post_types'] ) ) {
Expand Down Expand Up @@ -183,6 +189,9 @@ function () {
$data['parent'] = str_getcsv( $file_headers['parent'], ' ', '"' );
}

// Merges the default options.
$data = timber_block_default_data( $data );

// Register the block with ACF.
acf_register_block_type( $data );
}
Expand Down Expand Up @@ -312,3 +321,30 @@ function timber_block_directory_getter() {

return $directories;
}

/**
* Default options setter.
*
* @param [array] $data - header set data.
* @return [array]
*/
function timber_block_default_data( $data ) {
$default_data = apply_filters( 'timber/acf-gutenberg-blocks-default-data', array() );
$data_array = array();

if ( ! empty( $data['default_data'] ) ) {
$default_data_key = $data['default_data'];
}

if ( isset( $default_data_key ) && ! empty( $default_data[ $default_data_key ] ) ) {
$data_array = $default_data[ $default_data_key ];
} elseif ( ! empty( $default_data['default'] ) ) {
$data_array = $default_data['default'];
}

if ( is_array( $data_array ) ) {
$data = array_merge( $data_array, $data );
}

return $data;
}

0 comments on commit 3bf0dc0

Please sign in to comment.