Comments (4)
Hi @chaseconey
If I’m reading this correctly you basically want to get a JSON representation of the Block? There are couple of variations to that:
- JSON just like Storyblok provides
- JSON of content fields (potentially with mutators?)
- JSON of the block, with the package specific data (or sub set of) and the content
It’s definitely something I’ve thought about, being able to send JSON to the frontend more easily. There isn’t any way to handle any of the above yet.
Blocks could have a toJSON
method - and would this attach meta data? Maybe mapping the fields you want in the JSON.
Or something like Laravel’s API Resources? I’ve not used them much so am not that familiar with their pros and cons.
There could also be a benefit to being able to stop parsing the nested blocks and keeping some as JSON. Maybe a nested block doesn’t need to be transformed into a class and simply becomes a JSON or array ‘field’ on the parent?
Sorry, not really answering anything for you here, but is this the kind of thoughts you were having?
from laravel-storyblok.
Hi @chaseconey, do you happen to know exactly what Laravel is doing under the hood with calling @json'? I can’t see where this directive defined in the framework. I’ve been playing with a couple of ideas but need to know what
@json` does.
from laravel-storyblok.
@RicLeP looking at a view generated in storage/framework/views/
, I can see that @json($foo)
generates <?php echo json_encode($foo, 15, 512) ?>;
.
The 15 being these flags: JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
.
Edit: It is defined in Illuminate\View\Compilers\Concerns\CompilesJson
from laravel-storyblok.
Thanks @czuniga9 ! I didn’t think to look it the compiled view 😅
In 2.10.6 Block
now implements JsonSerializable
, see: https://www.php.net/manual/en/class.jsonserializable.php
By default it uses $this->content()
. If you need something more bespoke then you’ll need to override the jsonSerialize()
method on your Block
.
I did something similar recently before implementing JsonSerializable
which should be easy to migrate to the new style.
class Nav extends BaseBlock
{
public function toArray() {
return $this->content()->transform(function ($item) {
return $item->transform(function ($item) {
return $item->toArray();
});
})->first();
}
}
class NavDropdown extends BaseBlock
{
public function toArray() {
$dropDown = $this->content()->transform(function ($item) {
if (is_string($item)) {
return $item;
}
return $item->transform(function ($item) {
return $item->toArray();
});
});
$dropDown['isOpen'] = false;
return $dropDown;
}
}
class NavLink extends BaseBlock
{
public function toArray() {
return $this->content()->transform(function ($item) {
return is_string($item) ? $item : rtrim((string) $item, '/');
});
}
}
With the structure:
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
- Detecting broken images to prevent calling transform() HOT 2
- Cache ignores $page in paginated fetching HOT 3
- [Bug] Multi-option Parsing HOT 2
- Enable (rendering of) inline components within the RichText field HOT 8
- 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.