Comments (8)
Updated it to also respect Custom Block Classes instead of using the default Block Class.
<?php
namespace App\Storyblok\Fields;
use Riclep\Storyblok\Fields\RichText as BaseRichText;
use Storyblok\RichtextRender\Resolver;
use Riclep\Storyblok\Block;
use Riclep\Storyblok\Traits\HasChildClasses;
class Richtextfield extends BaseRichText
{
use HasChildClasses;
/**
* Converts the data to HTML when printed
*
* @return string
*/
public function __toString()
{
$richtextResolver = new Resolver();
$out = "";
// Loop through all nodes from the RichText Field
// If the node type is 'blok' it is an inline component -> render it as block
// else just use the richtextResolver as normally
foreach ($this->content['content'] as $node) {
if ($node['type'] == 'blok' && isset($node['attrs']['body']) && is_array($node['attrs']['body'])){
foreach ($node['attrs']['body'] as $blockContent) {
$class = $this->getChildClassName('Block', $blockContent['component']);
$block = new $class($blockContent, $this->block()->parent());
$out .= $block->render();
}
} else {
$out .= $richtextResolver->render(["content" => [$node]]);
}
}
return $out;
}
}
from laravel-storyblok.
This looks interesting. I’ve not tried using components in rich-text fields before and worried clients will go crazy with the editing power! Closing this ticket but will look to implement it as it is on my to-do list. Thank you.
from laravel-storyblok.
clients will go crazy with the editing power
Thats a valid point, had a good laugh ;-)
You can disable it within Storyblok.
from laravel-storyblok.
We are using it on a food blog / Recipe App.
The Blog Authors can include lists of Recipes within the article. Using the Rich-Text Fields allows to do so without splitting up the content in multiple richtext fields like
RichText
RecipeList
RichText
RecipeList
I also found it handy for more advanced control of inserting Images. The built-in method does not really support placement and size options. So i've created an ImageBlock that can now be placed directly within the Richtext Field.
from laravel-storyblok.
Inserting images is a really good idea. I will need to explore it more as until now I’ve made a containing Block that could take different child blocks (text, quote, image etc.) but what you suggested does sound interesting.
I’d love to see the site you’re working on when it’s ready
from laravel-storyblok.
Thanks for the code @papoms. I’ve tweaked it a bit to process the rich-text field’s content before the __toString
method. It creates a content
key in the field with a collection of all the HTML and Blocks. This way it can be manipulated further before rending in Blade. The toString
method them loops over the content
outputting HTML.
Do you have any thoughts on this as I’ve not used inline components much.
(Probably shouldn’t have closed the ticket yet either. I am new to maintaining open source!)
from laravel-storyblok.
@papoms just pushed version 2.5 live with inline rendering. Thanks for the help with this.
from laravel-storyblok.
Thanks for the update! I will look into it on sunday
from laravel-storyblok.
Related Issues (16)
- Scaffolding Empty Components Errors
- File Permission Issue with Console Commands HOT 1
- Call to undefined method App\Storyblok\Block::flatten() HOT 3
- Resolve global reference HOT 3
- Artisan command to create block template uses folder …/components while Block.php searches in …/blocks HOT 2
- Having trouble with the basics HOT 16
- Dealing with pagination HOT 3
- Question about serializing blocks HOT 4
- Detecting broken images to prevent calling transform() HOT 2
- Cache ignores $page in paginated fetching HOT 3
- [Bug] Multi-option Parsing HOT 2
- Dependency Dashboard
- Documentation about Folder HOT 1
- Fine tuning default configuration (public api key) HOT 1
- Undefined Variable after Generating Block HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-storyblok.