Skip to content

Commit

Permalink
Merge pull request MWDelaney#23 from xaqrox/filter
Browse files Browse the repository at this point in the history
Add filter support for block data.
  • Loading branch information
MWDelaney authored Aug 18, 2019
2 parents 0f4b808 + 68bd897 commit de3567d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ Add blade templates to `views/blocks` which get and use ACF data. Each template

## Creating ACF fields
Once a block is created you'll be able to assign ACF fields to it using the standard Custom Fields interface in WordPress. We recommend using [sage-advanced-custom-fields](https://github.com/MWDelaney/sage-advanced-custom-fields) to keep your ACF fields in version control with Sage.

## Filter block data
Block data can be altered via the 'sage/blocks/[block-name]/data' filter. For example, if your block template is called `my-block.blade.php`, you can alter the data thusly:

```php
add_filter('sage/blocks/my-block/data', function ($block) { // Do your thing here. });
```
9 changes: 8 additions & 1 deletion sage-acf-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ function sage_blocks_callback($block, $content = '', $is_preview = false, $post_
$block['is_preview'] = $is_preview;
$block['content'] = $content;
$block['slug'] = $slug;
$block['classes'] = implode(' ', [$block['slug'], $block['className'], 'align'.$block['align']]);
// Send classes as array to filter for easy manipulation.
$block['classes'] = [$slug, $block['className'], 'align'.$block['align']];

// Filter the block data.
$block = apply_filters("sage/blocks/$slug/data", $block);

// Join up the classes.
$block['classes'] = implode(' ', array_filter($block['classes']));

// Use Sage's template() function to echo the block and populate it with data
echo \App\template("blocks/${slug}", ['block' => $block]);
Expand Down

0 comments on commit de3567d

Please sign in to comment.