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

Fix Blocks not rendering when block's name and component's name differs #813

Merged
merged 17 commits into from
Apr 25, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Create Blocks defined in config exclusively
ptrckvzn committed Apr 1, 2021

Verified

This commit was signed with the committer’s verified signature. The key has expired.
xelaint Elain T.
commit 76d583fc93262500412d3f7e5842481ba7e7a9ca
61 changes: 61 additions & 0 deletions src/Services/Blocks/BlockCollection.php
Original file line number Diff line number Diff line change
@@ -187,9 +187,70 @@ public function load()
})
->toArray();

$this
->addBlocksFromConfig(collect(config('twill.block_editor.repeaters')), Block::TYPE_REPEATER)
->addBlocksFromConfig(collect(config('twill.block_editor.blocks')), Block::TYPE_BLOCK);

return $this;
}

/**
* @param Collection $items
* @param string $type
* @return void
*/
public function addBlocksFromConfig(Collection $items, $type)
{
$items->reject(function ($value, $blockName) use ($type) {
return $this->contains(function ($block) use ($blockName, $type) {
return $block->name === $blockName && $block->type === $type;
}) ? [$blockName, $value] : false;
})
->each(function ($block, $blockName) use ($type) {
$this->push($this->blockFromComponentName(
$block['component'],
$blockName,
$type,
Block::SOURCE_APP
));
});

return $this;
}

/**
* @param string $componentName
* @param string $blockName
* @param string $type
* @param string $source
* @return Block
*/
public function blockFromComponentName($componentName, $blockName, $type, $source)
{
$file = $this->findFileByComponentName($componentName);
$block = new Block($file, $type, $source);
$block->name = $blockName;

return $block;
}

/**
* This function will try to find a view from the a component name
* (minus the 'a17-block-' namespace).
*
* For compatibility with 2.0.2 and lower
*
* @param string $componentName
* @return \Symfony\Component\Finder\SplFileInfo
*/
public function findFileByComponentName($componentName)
{
$filename = str_replace('a17-block-', '', $componentName) . '.blade.php';
$paths = $this->paths->pluck('path')->toArray();

return iterator_to_array(\Symfony\Component\Finder\Finder::create()->name($filename)->in($paths), false)[0];
}

/**
* @return array
*/