I’m looking for a way to retrieve a Blueprint object in a plugin.
Currently I use kirby()->blueprints('blocks') wich returns an array of strings with all blueprints for blocks. If i then want to get a blueprints title for example I would need to get the object for each blueprint.
Using Blueprint::load() returns the specified Blueprint, but as an array where the values are not resolved. This means that retrieving $res['title'] might not always return the title; it could also be absent if the field is extended in the blueprint.yml or be a reference to another field, such as field.something.else.
I’m looking for either a way to get the full Blueprint object (preferred) or a method to “resolve” these blueprint arrays.
This topic isn’t a duplicate because it’s resolved with a workaround that involves creating a new blueprint object from an existing model. However, this workaround doesn’t solve the “getting the name” part since it would require passing it during the creation of the new object.
It’s a little hard to follow without knowing the blueprint in question etc. - to understand what exactly you’re trying to retrieve.
But a first thought was that I’d think not to use Blueprint::load() which really just loads the blueprint file and returns an array, but Blueprint::factory() which returns a blueprint object and maybe then has what you need resolved?
Thank you for looking into it.
An example blueprint could be this one:
name: Text
icon: text
wysiwyg: true
preview: fields
tabs:
main: `Preformatted text`
extends: tabs/block-main
fields:
text: fields/text
display:
extends: tabs/block-display
animation: tabs/animate
It is a Block blueprint that has multiple extends. If I now want to access the features of that block in my plugin and I use Blueprint::load('blocks/text) I get the following:
Array
(
[name] => field.blocks.text.name
[icon] => text
[wysiwyg] => 1
[preview] => text
[fields] => Array
(
[text] => Array
(
[type] => writer
[nodes] =>
[placeholder] => field.blocks.text.placeholder
)
)
[title] => Field.blocks.text.name
)
That is not useable because a lot of the values are not resolved but keys to other places to look them up. That is not helpful. Blueprint::factory() would indeed solve the problem but I think the documentation on the whole ModelWithContent is a bit thin so I do not know where to get the model from when just working with the blueprint file. Somehow the kirby panel uses the blueprint when displaying the fields for the block and stuff but I had no luck “reverse engineering” that stuff.
So to put it simple:
How can I get a blueprint Object of an existing Blueprint just with its name and file?
A lot of parts of a blueprint depend on a specific model, e.g. Kirby queries, many field options etc. - this is why a specific model is needed to make a sensible object out of the blueprint array.
For your case, maybe using Blueprint::load() is fine but then using I18n::translate($blueprint['titlle']) or so to resolve the i18n strings.
Sounds great.
Array
(
[name] => Button
[icon] => url
[wysiwyg] => 1
[preview] => fields
[tabs] => Array
(
[main] => Array
(
[extends] => tabs/block-main
[fields] => Array
(
[link] => fields/link
)
...
Can I also use a similar approach when retrieving the extends and the link field in the above example?