Skip to content

Commit

Permalink
Allow custom slug
Browse files Browse the repository at this point in the history
  • Loading branch information
Keven Lefebvre committed Jan 13, 2020
1 parent dcbee87 commit 474ba4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Add blade templates to `views/blocks` which get and use ACF data. Each template
```blade
{{--
Title:
Slug:
Description:
Category:
Icon:
Expand All @@ -37,6 +38,7 @@ Add blade templates to `views/blocks` which get and use ACF data. Each template
```blade
{{--
Title: Testimonial
Slug: testimonial
Description: Customer testimonial
Category: formatting
Icon: admin-comments
Expand Down Expand Up @@ -70,7 +72,8 @@ The options in the file header map to options in the [`acf_register_block_type`

| Field | Description | Values | Notes |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ------------------------------------- |
| `Title` | Title of the block in the gutenberg editor | i.e. `Testimonial` | _required_ |
| `Title` | Block's title in the gutenberg editor | i.e. `Testimonial` | _required_ |
| `Slug` | Block's slug that is automatically added to its HTML classes. The default value is set to the block's filename (eg. `testimonial.blade.php` = `testimonial`) | i.e. `testimonial` | _optional_ (defaults to filename) |
| `Description` | Description of the block in the gutenberg editor | i.e. `My testimonial block` | _optional_ |
| `Category` | Category to store the block in. Use these values or [register your own custom block categories](https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/#managing-block-categories) | `common`, `formatting`, `layout`, `widgets`, `embed` | _required_ |
| `Icon` | An icon property can be specified to make it easier to identify a block. Uses [dashicons](https://developer.wordpress.org/resource/dashicons/) | i.e. `book-alt` | _optional_ |
Expand Down
23 changes: 12 additions & 11 deletions sage-acf-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@
foreach ($template_directory as $template) {
if (!$template->isDot() && !$template->isDir()) {

// Strip the file extension to get the slug
$slug = removeBladeExtension($template->getFilename());
// Strip the file extension to get the name
$name = removeBladeExtension($template->getFilename());

// If there is no slug (most likely because the filename does
// If there is no name (most likely because the filename does
// not end with ".blade.php", move on to the next file.
if (!$slug) {
if (!$name) {
continue;
}

// Get header info from the found template file(s)
$file_path = locate_template($dir . "/${slug}.blade.php");
$file_path = locate_template($dir . "/${name}.blade.php");
$file_headers = get_file_data($file_path, [
'slug' => 'Slug',
'title' => 'Title',
'description' => 'Description',
'category' => 'Category',
Expand Down Expand Up @@ -95,7 +96,8 @@

// Set up block data for registration
$data = [
'name' => $slug,
'name' => $name,
'slug' => $file_headers['slug'] ?: $name,
'title' => $file_headers['title'],
'description' => $file_headers['description'],
'category' => $file_headers['category'],
Expand Down Expand Up @@ -156,15 +158,14 @@
*/
function sage_blocks_callback($block, $content = '', $is_preview = false, $post_id = 0)
{
// Set up the slug to be useful
$slug = str_replace('acf/', '', $block['name']);
// Set up the name to be useful
$name = str_replace('acf/', '', $block['name']);
$block = array_merge(['className' => ''], $block);

// Set up the block data
$block['post_id'] = $post_id;
$block['is_preview'] = $is_preview;
$block['content'] = $content;
$block['slug'] = $slug;
// Send classes as array to filter for easy manipulation.
$block['classes'] = [
$block['slug'],
Expand All @@ -174,13 +175,13 @@ function sage_blocks_callback($block, $content = '', $is_preview = false, $post_
];

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

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

// Use Acron's view() function to echo the block and populate it with data
echo view("blocks/${slug}", [
echo view("blocks/${name}", [
'block' => $block,
'content' => $content,
'is_preview' => $is_preview,
Expand Down

0 comments on commit 474ba4c

Please sign in to comment.