flobin
January 17, 2018, 9:24am
1
I have a page template and a blueprint for projects. Each project has some data, like the year, status, client, etc. This is subject to change, but the basic idea will be the same.
So for the project info, I’ve set up a structure field. (By the way, is that actually the best way to do it?)
Ideally, I’d like to output every field in the structure field like this:
<?php foreach($page->info()->toStructure() as $info): ?>
<?= $info; ?>
<?php endforeach ?>
With the output being something like:
Jaar: 2011 - 2012
Status: opgeleverd
Cliënt: familie Peeters
etc.
Where the first part is the label and the second part is the content of the field in the structure field.
But my current setup only outputs the content of the field, not its label. Is there a handy way to output the label as well?
This is my template and this is my blueprint .
texnixe
January 17, 2018, 10:03am
2
There are two plugins that allow you to read the blueprints. Other than that, it is not possible to read the labels, only the field keys…
flobin
January 17, 2018, 10:04am
3
Thank you! Then for now I think I’ll just do it manually with a snippet.
texnixe
January 17, 2018, 10:26am
4
What you can do, is map the field names to their labels in your config:
c::set('info.map', [
'yaar' => 'Yaar',
'status' => 'Status'
]);
An then call the value by its key.
// get the array in a template
$infomap = c::get('info.map');
// access a single value
$year = $infomap[$field->fieldname()->key()];
Or, if your field names are the same as your labels, you can use the field names and uppercase them.
Edit:
Example using the contactoptions
field from the Starterkit:
<?php foreach(page('contact')->contactoptions()->structure() as $field): ?>
<p><?= str::ucfirst($field->title()->key()).': '.$field->title(); ?></p>
<?php endforeach; ?>
1 Like