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

feat: add support for registering block patterns with blade partials as content #31

Merged
merged 4 commits into from
Jul 22, 2021
Merged

Conversation

Jamiewarb
Copy link
Contributor

@Jamiewarb Jamiewarb commented Jul 21, 2021

This would close #13

Description

Following on from the discussion in #13, this PR adds support for registering Block Patterns.

A new BlockPatternModule is added and registered with Poet. It then takes config via config/poet.php under the block_pattern key, and registers the patterns in a similar way to how Blocks are registered.

Note: The namespace() method from the BlockModule is also required for Block Patterns. I've moved this method to a HasNamespace trait, which both the BlockModule and BlockPatternModule now use.

Docs

Registering a Block Pattern

Poet can also register Block Patterns for you, with an optional Blade view for the content.

Patterns are registered using the namespace/label defined when registering the pattern with the editor.

If no namespace is provided, the current theme's text domain will be used instead.

Registering a block in most cases is as simple as:

'block_pattern' => [
    'sage/hero' => [
        'title' => 'Page Hero',
        'description' => 'Draw attention to the main focus of the page, and highlight key CTAs',
    ],
],

You can register the actual content for the pattern here as well, using the content key. Or leave it blank to use a corresponding blade view.

'block_pattern' => [
    'sage/fake-paragraph' => [
        'title' => 'Fake Paragraph',
        'description' => 'Filler content used instead of actual content for testing purposes',
        'content' => '<!-- wp:paragraph --><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione nulla culpa repudiandae nisi nostrum et, labore earum repellendus porro, mollitia voluptas quam? Modi sint tempore deleniti nesciunt ab, perferendis et.</p><!-- /wp:paragraph -->',
    ],
],

Creating a Pattern View

Given the block sage/fake-paragraph, if no content key is defined, then your accompanying Blade view would be located at views/block-patterns/fake-paragraph.blade.php.

This Block Pattern view may look like this:

<!-- wp:paragraph -->
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione nulla culpa repudiandae nisi nostrum et, labore earum repellendus porro, mollitia voluptas quam? Modi sint tempore deleniti nesciunt ab, perferendis et.</p>
<!-- /wp:paragraph -->

@Jamiewarb
Copy link
Contributor Author

Jamiewarb commented Jul 21, 2021

It seems that if no Block Pattern Categories are registered at all (which is the default in Sage 10), then opening the patterns tab displays an error.
image

We may want to also add Block Pattern Category support in that case, and provide a default all pattern category, aka:

register_block_pattern_category('all', ['label' => __('All Patterns', 'sage')]);

@Log1x Log1x merged commit 48e5ffc into Log1x:master Jul 22, 2021
@Log1x
Copy link
Owner

Log1x commented Jul 22, 2021

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Block pattern support
2 participants