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

Fixed error in Sage9 with multiple block folders #53

Open
wants to merge 2 commits 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ add_filter('sage/blocks/my-block/data', function ($block) { // Do your thing her
By default all your template files in `views/blocks` will be loaded. You can use the templates filter to add more folders if you wish. See an example below of how to add your own folders.

```php
add_filter('sage-acf-gutenberg-blocks-templates', function ($folders) {
$folders[] = 'views/your-folder'; // Adds your folder
add_filter('sage-acf-gutenberg-blocks-other-templates', function ($folders) {
$folders = ['views/your-folder', 'views/your-second-folder]; // Adds your folder
return $folders;
});
```
```
58 changes: 46 additions & 12 deletions sage-acf-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
return;
}

// Add the default blocks location, 'views/blocks', via filter
// Add the default blocks location
add_filter('sage-acf-gutenberg-blocks-templates', function () {
return ['views/blocks'];
});

// Add second filter for others locations, in filter add as array
add_filter('sage-acf-gutenberg-blocks-other-templates', function() {
return [];
});

/**
* Create blocks based on templates found in Sage's "views/blocks" directory
*/
Expand All @@ -30,9 +35,13 @@

// Get an array of directories containing blocks
$directories = apply_filters('sage-acf-gutenberg-blocks-templates', []);
$other_directories = apply_filters('sage-acf-gutenberg-blocks-other-templates', []);

// Merge both locations
$merge_directories = array_merge($directories, $other_directories);

// Check whether ACF exists before continuing
foreach ($directories as $directory) {
foreach ($merge_directories as $directory) {
$dir = isSage10() ? \Roots\resource_path($directory) : \locate_template($directory);

// Sanity check whether the directory we're iterating over exists first
Expand Down Expand Up @@ -189,21 +198,46 @@ function sage_blocks_callback($block, $content = '', $is_preview = false, $post_

// Get the template directories.
$directories = apply_filters('sage-acf-gutenberg-blocks-templates', []);
$other_directories = apply_filters('sage-acf-gutenberg-blocks-other-templates', []);

foreach ($directories as $directory) {
if (isSage10()) {
$view = Str::replaceFirst('views/', '', $directory) . '/' . $slug;
// Merge both arrays default and for other locations
$merge_directories = array_merge($directories, $other_directories);

if (\Roots\view()->exists($view)) {
// Use Sage's view() function to echo the block and populate it with data
echo \Roots\view($view, ['block' => $block]);
}
// Foreach merge direction arrays
foreach ($merge_directories as $directory) {

// get path for each block
$dirs = isSage10() ? \Roots\resource_path($directory) : \locate_template($directory);

$template_directory = new \DirectoryIterator($dirs);

// foreach all blocks paths
foreach ($template_directory as $template) {

if (!$template->isDot() && !$template->isDir()) {
$slug = removeBladeExtension($template->getFilename());

} else {
// Use Sage 9's template() function to echo the block and populate it with data
echo \App\template("${directory}/${slug}", ['block' => $block]);
// remove view/ folder and add slug
$view = Str::replaceFirst('views/', '', $directory) . '/' . $slug;

if (isSage10()) {

$view = Str::replaceFirst('views/', '', $directory) . '/' . $slug;

if (\Roots\view()->exists($view)) {
// Use Sage's view() function to echo the block and populate it with data
echo \Roots\view($view, ['block' => $block]);
}

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

}
}
}

}

/**
Expand Down