diff --git a/README.md b/README.md index 229e25c..66bfb7b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Add blade templates to `views/blocks` which get and use ACF data. Each template EnqueueStyle: EnqueueScript: EnqueueAssets: + Parent: --}} ``` @@ -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. diff --git a/sage-acf-gutenberg-blocks.php b/sage-acf-gutenberg-blocks.php index 2b5561c..299235e 100644 --- a/sage-acf-gutenberg-blocks.php +++ b/sage-acf-gutenberg-blocks.php @@ -73,6 +73,7 @@ 'enqueue_style' => 'EnqueueStyle', 'enqueue_script' => 'EnqueueScript', 'enqueue_assets' => 'EnqueueAssets', + 'parent' => 'Parent', ]); if (empty($file_headers['title'])) { @@ -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)); } @@ -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. *