Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support restricting blocks to specific parents #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Add blade templates to `views/blocks` which get and use ACF data. Each template
EnqueueStyle:
EnqueueScript:
EnqueueAssets:
Parent:
--}}
```

Expand Down Expand Up @@ -83,6 +84,7 @@ The options in the file header map to options in the [`acf_register_block_type`
| `SupportsInnerBlocks` | This property allows the block to support the nesting of other blocks within it. | `true` or `false` |_optional_ (defaults to `false`) |
| `SupportsAlignText` | This property adds an alignment toolbar button similar to that seen when editing a paragraph of text. | `true` or `false` |_optional_ (defaults to `false`) |
| `SupportsAlignContent` | This property adds an alignment toolbar button similar to that seen when editing a core "Cover block" | `true` or `false` |_optional_ (defaults to `false`) |
| `Parent` | An array of block types to restrict where this block can be used. Separate values with a space. | e.g. `core/column acf/parent-block` |_optional_ (defaults to usable anywhere)

## 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.
Expand Down
31 changes: 31 additions & 0 deletions sage-acf-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'enqueue_style' => 'EnqueueStyle',
'enqueue_script' => 'EnqueueScript',
'enqueue_assets' => 'EnqueueAssets',
'parent' => 'Parent',
]);

if (empty($file_headers['title'])) {
Expand Down Expand Up @@ -153,6 +154,13 @@
$data['supports']['multiple'] = $file_headers['supports_multiple'] === 'true' ? true : false;
}

// If the Parent header is set in the template, restrict this block to specific parent blocks
if (!empty($file_headers['parent'])) {
$data['parent'] = array_map(function($name) {
return validateBlockName($name);
}, explode(' ', $file_headers['parent']));
}

// Register the block with ACF
\acf_register_block_type(apply_filters("sage/blocks/$slug/register-data", $data));
}
Expand Down Expand Up @@ -236,6 +244,29 @@ function checkAssetPath(&$path)
}
}

/**
* Validates the format of a block name string
*
* @param string $name
*
* @return void|string
*/
function validateBlockName($name) {
global $sage_error;

// A block name can only contain lowercase alphanumeric characters and dashes, and must begin with a letter.
// NOTE: this cannot check whether a block is valid and registered (since others may be registered after this),
// it just confirms the block name format is correct.
if (!preg_match('/^[a-z]+\/[a-z][a-z0-9-]+$/', $name)) {
$sage_error(__('Invalid parent block name format: ' . $name, 'sage'), __('Invalid parent block name', 'sage'));

// Return NULL for invalid block names.
return null;
}

return $name;
}

/**
* Check if Sage 10 is used.
*
Expand Down