Skip to content

Commit

Permalink
Merge branch 'release2.23.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
RicLeP committed May 15, 2023
2 parents 55b2919 + 1b5e1ee commit 4616b32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
47 changes: 35 additions & 12 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,24 @@ public function renderUsing(array|string $views, array $with = []): View
}

/**
* Returns an array of possible views for the current Block based on
* it’s $componentPath match the component prefixed by each of it’s
* ancestors in turn, starting with the closest, for example:
* Returns an array of views for the Block based on page’s content type and
* block’s $componentPath. First are page specific views starting with the
* page’s content type followed by those using the block’s component path
*
* $componentPath = ['page', 'parent', 'child', 'this_block'];
* Example:
*
* Becomes a list of possible views like so:
* ['child.this_block', 'parent.this_block', 'page.this_block'];
* $componentPath = ['page', 'product', 'heroes', 'hero'];
*
* Override this method with your custom implementation for
* ultimate control
* [
* "storyblok.pages.product.blocks.heroes.hero"
* "storyblok.pages.product.blocks.hero"
* "storyblok.blocks.heroes.hero"
* "storyblok.blocks.product.hero"
* "storyblok.blocks.hero"
* ]
*
* It is recommended to start with the most generic view and create more
* specific ones as and when required
*
* @return array
*/
Expand All @@ -210,15 +217,31 @@ public function views(): array
$componentPath = $this->_componentPath;
array_pop($componentPath);

$views = array_map(function($path) {
return config('storyblok.view_path') . 'blocks.' . $path . '.' . $this->component();
}, $componentPath);
$views = array_filter(array_map(function($path) {
if ($path !== 'page') {
return config('storyblok.view_path') . 'blocks.' . $path . '.' . $this->component();
}

return null;
}, $componentPath));

$views = array_reverse($views);

$views[] = config('storyblok.view_path') . 'blocks.' . $this->component();

return $views;
$themeViews = $views;

$themeViews = array_filter(array_map(function($view) {
$theme = $this->page()?->block()->component();

if (!strpos($view, $theme)) {
return str_replace('blocks.', 'pages.' . $theme . '.blocks.', $view);
}

return null;
}, $themeViews));

return array_merge($themeViews, $views);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,15 @@ public function can_get_views()
{
$page = $this->makePage('ep16.json');

$this->assertEquals(['blocks.layout_2-sections.text--titled', 'blocks.feature.text--titled', 'blocks.episode.text--titled', 'blocks.page.text--titled', 'blocks.text--titled'], $page->block()->features[0]->body[3]->section_1[0]->views());
$this->assertEquals([
'pages.episode.blocks.layout_2-sections.text--titled',
'pages.episode.blocks.feature.text--titled',
'pages.episode.blocks.text--titled',
'blocks.layout_2-sections.text--titled',
'blocks.feature.text--titled',
'blocks.episode.text--titled',
'blocks.text--titled'
], $page->block()->features[0]->body[3]->section_1[0]->views());
}

/** @test */
Expand Down

0 comments on commit 4616b32

Please sign in to comment.