Following on from my previous question (https://forum.getkirby.com/t/attempting-to-display-values-from-multiselect-field-using-blueprint/ ) I was wondering if someone can help with the following.
On my blog posts page, I want to generate a list of categories that the user can then filter the posts by.
The place that these categories are defined is within my single post blueprint.
Is there a way that I can query this specific blueprint? I can now work out how to use the blueprint()
method in relation to the page I’m on, or in relation to a specific page/post, but I want to simply access the info contained within a specified blueprint file, independent of any given page/post.
I know that the name of the blueprint is pages/article
but can’t workout if there is a way of doing this.
Is this possible?
bnomei
February 15, 2023, 6:03pm
2
blueprints are always attached to page objects. if you need to read data from a specific blueprint file you could
fetch any existing page with that template to read the blueprint
read it manually with kirbys yaml class or
create a virtual page with same template as the blueprint and then read the blueprint data
bnomei:
fetch any existing page with that template to read the blueprint
I did consider that, but it would break if the page got deleted.
bnomei:
read it manually with kirbys yaml class or
This looks beyond my expertise.
bnomei:
create a virtual page with same template as the blueprint and then read the blueprint data
Hmm, this might be the solution. I’m having a read through.
This all seems like a lot of effort to avoid hard-coding a list though!
texnixe
February 15, 2023, 7:38pm
4
You can also load the blueprint
$bp = Kirby\Cms\Blueprint::load('pages/article');
This would give you an array though.
1 Like
bnomei
February 15, 2023, 7:39pm
5
why not store the categries in a page method (make it return an array of kirby objects) and use the query param of your select fields to populate the data. you could reuse the date from the page method elsewhere.
texnixe
February 15, 2023, 7:41pm
6
Or building on the above:
$props = Kirby\Cms\Blueprint::load('pages/note');
$props['model'] = page();
$bp = new Kirby\Cms\PageBlueprint($props);
dump($bp->fields());
Hi @texnixe
I ended up with the following:
<?php
$loadbp = Kirby\Cms\Blueprint::load('pages/article');
$loadbp['model'] = page();
$bp = new Kirby\Cms\PageBlueprint($loadbp);
$bpfields = $bp->fields();
$bpcats = $bpfields['articlecategories']['options'];
foreach ($bpcats as $bpcatk => $bpcatv) { ?>
<a href="<?= $page->url(); ?>/cat:<?= $bpcatk; ?>"><?= $bpcatv ?></a>
<?php } ?>
I’ll pretty up the HTML side of things, but the functionality is there now.
TBH, I’m not really sure what these bits do:
$loadbp['model'] = page();
$bp = new Kirby\Cms\PageBlueprint($loadbp);
Do they simply allow Kirby to create a page type object, apply the blueprint and then use the fields()
method on it?
As you can probably tell, I’m not really a developer, just an enthusiastic site builder that is always wanting to learn!
texnixe
February 17, 2023, 4:02pm
8
This adds the model property to the array that we previously stored in the $loadbp variable.
The page()
helper just create a page instance, it’s probably a bit of a hack, but we need a way to pass the correct model to the props.
The next step then creates a new PageBlueprint instance, which is the same as you you called $page->blueprint()
.
Great, thanks for clarifying that for me. Also, thanks to @bnomei for your help too - I appreciate it.